hive serde 序列化与反序列化 - 一行数据写入hive表
Hive-0.5中SerDe概述
一、背景
1、当进程在进行远程通信时,彼此可以发送各种类型的数据,无论是什么类型的数据都会以二进制序列的形式在网络上传送。发送方需要把对象转化为字节序列才可在网络上传输,称为对象序列化;接收方则需要把字节序列恢复为对象,称为对象的反序列化。
2、Hive的反序列化是对key/value反序列化成hive table的每个列的值。
3、Hive可以方便的将数据加载到表中而不需要对数据进行转换,这样在处理海量数据时可以节省大量的时间。
二、技术细节
1、SerDe是Serialize/Deserilize的简称,目的是用于序列化和反序列化。
2、用户在建表时可以用自定义的SerDe或使用Hive自带的SerDe,SerDe能为表指定列,且对列指定相应的数据。
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type
[COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)]
INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
创建指定SerDe表时,使用row format row_format参数,例如:
a、添加jar包。在hive客户端输入:hive>add jar /run/serde_test.jar;
或者在linux shell端执行命令:${HIVE_HOME}/bin/hive -auxpath /run/serde_test.jar
b、建表:create table serde_table row format serde 'hive.connect.TestDeserializer';
3、编写序列化类TestDeserializer。实现Deserializer接口的三个函数:
a)初始化:initialize(Configuration conf, Properties tb1)。
b)反序列化Writable类型返回Object:deserialize(Writable blob)。
c)获取deserialize(Writable blob)返回值Object的inspector:getObjectInspector()。
public interface Deserializer {
/**
* Initialize the HiveDeserializer.
* @param conf System properties
* @param tbl table properties
* @throws SerDeException
*/
public void initialize(Configuration conf, Properties tbl) throws SerDeException;
/**
* Deserialize an object out of a Writable blob.
* In most cases, the return value of this function will be constant since the function
* will reuse the returned object.
* If the client wants to keep a copy of the object, the client needs to clone the
* returned value by calling ObjectInspectorUtils.getStandardObject().
* @param blob The Writable object containing a serialized object
* @return A Java object representing the contents in the blob.
*/
public Object deserialize(Writable blob) throws SerDeException;
/**
* Get the object inspector that can be used to navigate through the internal
* structure of the Object returned from deserialize(...).
*/
public ObjectInspector getObjectInspector() throws SerDeException;
}
实现一行数据
划分成hive表的time,userid,host,path四个字段
的反序列化类。例如:
package hive.connect;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.serde2.Deserializer;
import org.apache.hadoop.hive.serde2.SerDeException;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.-
ObjectInspectorFactory.ObjectInspectorOptions;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
public class TestDeserializer implements Deserializer {
private static List<String> FieldNames = new ArrayList<String>();
private static List<ObjectInspector> FieldNamesObjectInspectors = new ArrayList<ObjectInspector>();
static {
FieldNames.add("time");
FieldNamesObjectInspectors.add(ObjectInspectorFactory
.getReflectionObjectInspector(Long.class,
ObjectInspectorOptions.JAVA));
FieldNames.add("userid");
FieldNamesObjectInspectors.add(ObjectInspectorFactory
.getReflectionObjectInspector(Integer.class,
ObjectInspectorOptions.JAVA));
FieldNames.add("host");
FieldNamesObjectInspectors.add(ObjectInspectorFactory
.getReflectionObjectInspector(String.class,
ObjectInspectorOptions.JAVA));
FieldNames.add("path");
FieldNamesObjectInspectors.add(ObjectInspectorFactory
.getReflectionObjectInspector(String.class,
ObjectInspectorOptions.JAVA));
}
@Override
public Object deserialize(Writable blob) {
try {
if (blob instanceof Text) {
String line = ((Text) blob).toString();
if (line == null)
return null;
String[] field = line.split("\t");
if (field.length != 3) {
return null;
}
List<Object> result = new ArrayList<Object>();
URL url = new URL(field[2]);
Long time = Long.valueOf(field[0]);
Integer userid = Integer.valueOf(field[1]);
//简易地对一行数据进行格式转换。
result.add(time);
result.add(userid);
result.add(url.getHost());
result.add(url.getPath());
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
@Override
public ObjectInspector getObjectInspector() throws SerDeException {
return ObjectInspectorFactory.getStandardStructObjectInspector(
FieldNames, FieldNamesObjectInspectors);
}
@Override
public void initialize(Configuration arg0, Properties arg1)
throws SerDeException {
}
}
测试HDFS上hive表数据,如下为一条测试数据:
1234567891012 123456 http://wiki.apache.org/hadoop/Hive/LanguageManual/UDF
hive> add jar /run/jar/merg_hua.jar;
Added /run/jar/merg_hua.jar to class path
hive> create table serde_table row format serde 'hive.connect.TestDeserializer';
Found class for hive.connect.TestDeserializer
OK
Time taken: 0.028 seconds
hive> describe serde_table;
OK
time bigint from deserializer
userid int from deserializer
host string from deserializer
path string from deserializer
Time taken: 0.042 seconds
hive> select * from serde_table;
OK
1234567891012 123456 wiki.apache.org /hadoop/Hive/LanguageManual/UDF
Time taken: 0.039 seconds
三、总结
1、创建Hive表使用序列化时,需要自写一个实现Deserializer的类,并且选用create命令的row format参数。
2、在处理海量数据的时候,如果数据的格式与表结构吻合,可以用到Hive的反序列化而不需要对数据进行转换,可以节省大量的时间。
转载于:https://blog.51cto.com/houjt/1605442
hive serde 序列化与反序列化 - 一行数据写入hive表相关推荐
- spark写表指定外部表_spark 将dataframe数据写入Hive分区表
从spark1.2 到spark1.3,spark SQL中的SchemaRDD变为了DataFrame,DataFrame相对于SchemaRDD有了较大改变,同时提供了更多好用且方便的API. D ...
- 利用SparkSQL(java版)将离线数据或实时流数据写入hive的用法及坑点
1. 通常利用SparkSQL将离线或实时流数据的SparkRDD数据写入Hive,一般有两种方法.第一种是利用org.apache.spark.sql.types.StructType和org.ap ...
- flink源码分析_Flink源码分析之深度解读流式数据写入hive
前言 前段时间我们讲解了flink1.11中如何将流式数据写入文件系统和hive [flink 1.11 使用sql将流式数据写入hive],今天我们来从源码的角度深入分析一下.以便朋友们对flink ...
- 1.30.Flink SQL案例将Kafka数据写入hive
1.30.Flink SQL案例将Kafka数据写入hive 1.30.1.1.场景,环境,配置准备 1.30.1.2.案例代码 1.30.1.2.1.编写pom.xml文件 1.30.1.2.2.M ...
- 按照下面的页面做一个用户注册的Sevlet,要求自己设计表,并将表单的数据写入到表中。
按照下面的页面做一个用户注册的Sevlet,要求自己设计表,并将表单的数据写入到表中. 表单代码如下:(register.html) <!DOCTYPE html> <html la ...
- 外部文件数据写入hive
hive数据库是基于HDFS的一个数据库,是对hdfs数据的一个映射关系. 注意:hive数据库存入数据的时候不建议使用insert into语句来进行插入,这样的的操作方式在效率上会很低效. 会出现 ...
- 如何将excel表格导入word_如何将Excel中的数据写入Word表?
之前我们分享了一期小代码,内容是如何将word中表格的数据读入excel-- 之后有朋友表示知道了,又问如何将excel中的数据写入word-- 此时此刻,我再一次清醒的意识到,这世界上像我这样好的人 ...
- Python 读pdf数据写入Excel表中
活动地址:CSDN21天学习挑战赛 目录 一.Python操作PDF的库有很多 二.pdflumber作为案例讲解使用 2.安装配置 2.加载PDF 3.读取pdf文档信息 1)读取pdf文档 ...
- 新的比较详细的hive安装教程,包含:hadoop的安装配置,hive安装详细教程,大数据下hive详细配置和简单使用和hive元数据迁移相关安装配置操作
简介:这是大数据里安装hive比较详细的教程,包括安装hive时hadoop的相关配置,hive安装,hive 之后的mysql安装,hive的元素配置到mysql相关配置,hive常见属性的相关配置 ...
最新文章
- 刚刚,2022 USNews全美大学排行榜出炉!普林斯顿霸榜,哥大哈佛MIT并列第二
- 【SAP技术】SAP MM 如何看一个自定义移动类型是复制哪个标准移动类型而创建的?
- python tkinter Listbox用法
- Qt工作笔记-图形视图框架中的分组,以及添加平行拖动图元
- cnblogs客户端发贴解析,Post,Get分析
- HDU-2525 Clone Wars 模拟
- 大数据学习笔记29:Hadoop压缩机制演示
- 文件操作命令(replace)
- SQL Server商业智能功能–创建简单的OLAP多维数据集
- 2021年SWPUACM暑假集训day1二分算法
- 解决apache启动错误httpd:Could not reliably determine...
- java 面单模板_顺丰电子面单JSON请求格式
- Android外部存储设备管理——vold挂载大容量存储设备
- 设置idea的ant工具的代理
- html5请假页面,请假模版。.html
- PPT文件不能编辑是什么原因
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
- 人工智能畅想ps海报教程_如何战胜鲁班人工智能做图?大战鲁班海报制作教程【PS教程】...
- 微信小程序实现五星评分效果
- 西门子plc怎样实现远程调试、远程上下载程序?