当前位置 博文首页 > 文章内容

    Hive中自定义序列化器(带编码)

    作者: 栏目:未分类 时间:2020-09-18 14:01:08

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    hive SerDe的简介

    https://www.jianshu.com/p/afee9acba686

    问题

    数据文件为文本文件,每一行为固定格式,每一列的长度都是定长或是有限制范围,考虑采用hive提供的RegexSerDe来实现记录解析,使用后发现hive查询出的数据中文字段乱码

    解决过程

    serialization.encoding=GBK

    Hadoop中文件默认utf8编码,hive序列化操作时,默认按照utf8来解析,所以肯定会乱码,从网上查了下,解决方案是建表是指定serde的"serialization.encoding"="GBK",然而并没有解决我的问题

    源码

    Hive建表格式为ROW FORMAT,不指定SerDe时,默认用的是org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe,继承了org.apache.hadoop.hive.serde2.AbstractEncodingAwareSerDe,而该类确实可以通过设置"serialization.encoding"="GBK"来解决hive读取gbk文件乱码的问题,代码如下:

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package org.apache.hadoop.hive.serde2;
    
    import com.google.common.base.Charsets;
    import java.nio.charset.Charset;
    import java.util.Properties;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
    import org.apache.hadoop.io.Writable;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public abstract class AbstractEncodingAwareSerDe extends AbstractSerDe {
        private static final Logger LOG = LoggerFactory.getLogger(AbstractEncodingAwareSerDe.class);
        protected Charset charset;
    
        public AbstractEncodingAwareSerDe() {
        }
    
        /** @deprecated */
        @Deprecated
        public void initialize(Configuration conf, Properties tbl) throws SerDeException {
            this.charset = Charset.forName(tbl.getProperty("serialization.encoding", "UTF-8"));
            if (this.charset.equals(Charsets.ISO_8859_1) || this.charset.equals(Charsets.US_ASCII)) {
                LOG.warn("The data may not be properly converted to target charset " + this.charset);
            }
    
        }
    
        public final Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException {
            Writable result = this.doSerialize(obj, objInspector);
            if (!this.charset.equals(Charsets.UTF_8)) {
                result = this.transformFromUTF8(result);
            }
    
            return result;
        }
    
        protected abstract Writable transformFromUTF8(Writable var1);
    
        protected abstract Writable doSerialize(Object var1, ObjectInspector var2) throws SerDeException;
    
        public final Object deserialize(Writable blob) throws SerDeException {
            if (!this.charset.equals(Charsets.UTF_8)) {
                blob = this.transformToUTF8(blob);
            }
    
            return this.doDeserialize(blob);
        }
    
        protected abstract Writable transformToUTF8(Writable var1);
    
        protected abstract Object doDeserialize(Writable var1) throws SerDeException;
    }