Hive中自定义序列化器(带编码)
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;
}
继续查看org.apache.hadoop.hive.serde2.RegexSerDe,发现并没有用到serialization.encoding,难怪设置了也没有用,源码就不贴了
解决
解决方法也很简单,自定义类EncodingAwareRegexSerDe继承RegexSerDe,实现转UTF8的功能,代码如下:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.serde2.RegexSerDe;
import org.apache.hadoop.hive.serde2.SerDeException;
import org.apache.hadoop.hive.serde2.SerDeSpec;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
@SerDeSpec(
schemaProps = {"columns", "columns.types", "input.regex", "input.regex.case.insensitive","serialization.encoding"}
)
public class EncodingAwareRegexSerDe extends RegexSerDe {
public static final Logger LOG = LoggerFactory.getLogger(EncodingAwareRegexSerDe.class.getName());
protected String charsetName;
public EncodingAwareRegexSerDe(){
super();
}
@Override
public void initialize(Configuration conf, Properties tbl) throws SerDeException {
super.initialize(conf, tbl);
this.charsetName = tbl.getProperty("serialization.encoding", "UTF-8").trim();
}
@Override
public Object deserialize(Writable blob) throws SerDeException {
Text rowText = (Text) blob;
Text utf8Text = transformTextToYTF8(rowText,this.charsetName);
return super.deserialize(utf8Text);
}
private Text transformTextToYTF8(Text text,String encoding){
String value = "";
try{
value = new String(text.getBytes(),0,text.getLength(),encoding);
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
return new Text(value);
}
}
使用自定义序列化器
将上述自定义的类打成jar包后,即可使用
操作hive shell
hive> add jar /home/dw_hbkal/przhang/hive-custom-serdes-1.0-SNAPSHOT.jar;
CREATE EXTERNAL TABLE IF NOT EXISTS test_tooldb.ind01acoma_tmp(
acq_ins_id_cd STRING,
fwd_settle_at DECIMAL(12, 0),
repl_at DECIMAL(12, 0),
......
card_accptr_nm_addr STRING,
resv5 STRING
)PARTITIONED BY(ins_id_cd STRING, hp_settle_dt STRING)
ROW FORMAT SERDE 'com.unionpay.bigdataTest.hive.serdes.EncodingAwareRegexSerDe'
with serdeproperties (
"input.regex"="(.{11}) (.{11}) (.{6}) (.{10}) (.{19}) (.{12}) (.{12}) (.{12}) (.{4}) (.{6}) (.{4}) (.{8}) (.{15}) (.{12}) (.{2}) (.{6}) (.{11}) (.{6}) (.{2}) (.{3}) (.{12}) (.{12}) (.{12}) (.{1}) (.{3}) (.{1}) (.{1}) (.{10}) (.{11}) (.{1}) (.{2}) (.{2}) (.{12}) (.{1})(.{2})(.{1})(.{1})(.{2})(.{1})(.{1})(.{2})(.{1})(.{2}) (.{11}) (.{11}) (.{1}) (.{1}) (.{4}) (.{2}) (.{1,40}) (.{3}) (.{9}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{11}) (.{9}) (.{9}) (.{9}) (.{9}) (.{19}) (.{2}) (.{40}) (.{4}) (.{1}) (.{2}) (.{10}) (.{6}) (.{1}) (.{12}) (.{193})",
"serialization.encoding"="GBK"
)
STORED AS TEXTFILE
LOCATION '/user/dw_hbkal/db/test_tooldb/ind01acoma_tmp';
load data local inpath '/home/dw_hbkal/przhang/IND18071032ACOMA' overwrite into table test_tooldb.ind01acoma_tmp partition(ins_id_cd='01055800',hp_settle_dt='20180710');
Hive中自定义序列化器(带编码)的更多相关文章
- DRF中的序列化器
DRF中的序列化器详细应用 视图的功能:说白了就是接收前端请求,进行数据处理 (这里的处理包括:如果前端是GET请求,则构造查询集,将结果返回,这个过程为序列化:如果前端是POST请求,假如要对数 ...
- kafka自定义序列化器
<kafka权威指南> Customer.java public class Customer { private int customId; private String custome ...
- Hive中自定义函数
Hive的自定义的函数的步骤: 1°.自定义UDF extends org.apache.hadoop.hive.ql.exec.UDF 2°.需要实现evaluate函数,evaluate函数支持重 ...
- Django REST framework 中的序列化器
在此之前定义一个序列化工具: views中的的代码 from rest_framework.viewsets import ModelViewSet from .models import B ...
- Spring MVC中自定义拦截器的简单示例
1. 引言 拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似于Servlet的Filter. 我们可以让普通的Bean实现HandlerIntercpetor接口或继承 ...
- Hive中自定义Map/Reduce示例 In Java
Hive支持自定义map与reduce script.接下来我用一个简单的wordcount例子加以说明. 如果自己使用Java开发,需要处理System.in,System,out以及key/val ...
- hive中修改序列化格式分隔符
标签: hadoophivealtertable 2014-11-19 10:45 4219人阅读 评论(0) 收藏 举报 分类: hadoop(6) 版权声明:本文为博主原创文章,未经博主允许不 ...
- Hive中自定义Map/Reduce示例 In Python
Hive支持自定义map与reduce script.接下来我用一个简单的wordcount例子加以说明.使用Python开发(如果使用Java开发,请看这里). 开发环境: python:2.7.5 ...
- [转]hive中自定义函数(UDAF)实现多行字符串拼接为一行
函数如何使用: hive> desc concat_test;OKa intb string hive> select * from concat_test;OK1 ...
随机推荐
- python-网络安全编程第三天(正则表达式)
python 正则表达式 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被编译成一系列的字节码,然后由用 ...
- sqlilab less28 less28a
less-28 less-28a 二者相差不大 单引号小括号包裹,黑名单过滤--,#,空格,union空格select(不区分大小写) less-28的黑名单 less-28a的黑名单 %a0,不被 ...
- 计算机&编程语言发展史
计算机&编程语言发展史 编辑于2020-11-18 计算机的基本组成 计算机的发展经历了哪几代? 第一代 电子管计算机 第二代 晶体管计算机 第三代 集成电路计算机 第四代 大规模和超大规模集 ...
- 闭关苦修半年,最终靠着这份面试题,我成功拿到了阿里的offer!
前言 我大概我是从去年12月份开始看书学习,到今年的6月份,一直学到看大家的面经基本上百分之90以上都会,我就在5月份开始投简历,边面试边补充基础知识等.也是有些辛苦.终于是在前不久拿到了阿里的off ...
- C语言讲义——错误处理
errno C语言不提供对错误处理的直接支持. 以返回值的形式表示是否出错. 在发生错误时,大多数的C函数调用返回1或NULL. 同时设置一个错误代码errno(全局变量),表示在函数调用期间发生了错 ...
- 03_Service的绑定
Start Service启动的Service,Application退出,Service也不会退出: Bind Service启动的Service,Application退出,Service就会退出 ...
- 简单RTSCamera实现
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TopCamer ...
- 测试开发工程必备技能之一:Mock的使用
1. 背景 在实际产品开发过程中,某个服务或前端依赖一个服务接口,该接口可能依赖多个底层服务或模块,或第三方接口,比如说服务 A 依赖服务B,服务B又依赖服务 C,如下图所示: 这种依赖的问题会导致原 ...
- uwsgi+nginx的三种配置方式
第一种 vi /etc/uwsgi.ini uwsgi --reload uwsgi.pid vi /etc/nginx/conf.d/iot.conf service nginx restart 第 ...
- 第5.4节 Python函数中的变量及作用域
一.函数中的变量使用规则 函数执行时,使用的全局空间是调用方的全局空间,参数及函数使用的局部变量存储在函数单独的局部名字空间内: 函数的形参在函数中修改了值时,并不影响调用方本身的数据,但如果形参是一 ...