hive数据写入elasticsearch
2019独角兽企业重金招聘Python工程师标准>>>
在使用前同样需要加入 elasticsearch-hadoop-2.3.4.jar
依赖,具体请参见前文介绍。我们先在Hive里面建个名为iteblog
的表,如下:
CREATE EXTERNAL TABLE iteblog (id bigint,name STRING)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.resource' = 'iteblog/iteblog', 'es.nodes'='www.iteblog.com','es.port'='9003');
建完表之后我们可以看下Hive是怎么存储这样的表格:
hive> show create table iteblog;
OK
CREATE EXTERNAL TABLE `iteblog`(`id` bigint COMMENT 'from deserializer', `name` string COMMENT 'from deserializer')
ROW FORMAT SERDE 'org.elasticsearch.hadoop.hive.EsSerDe'
STORED BY'org.elasticsearch.hadoop.hive.EsStorageHandler'
WITH SERDEPROPERTIES ( 'serialization.format'='1')
LOCATION'hdfs://user/iteblog/hive/warehouse/iteblog.db/iteblog'
TBLPROPERTIES ('COLUMN_STATS_ACCURATE'='false', 'es.nodes'='www.iteblog.com', 'es.port'='9003', 'es.resource'='iteblog/iteblog', 'numFiles'='0', 'numRows'='-1', 'rawDataSize'='-1', 'totalSize'='0', 'transient_lastDdlTime'='1478248148')
Time taken: 0.148 seconds, Fetched: 21 row(s)
我们可以看到Hive对里面的字段注释是 from deserializer,如果是正常的Hive表将没有这些信息;而且我们可以发现 ROW FORMAT SERDE
已经变成了 org.elasticsearch.hadoop.hive.EsSerDe
,在TBLPROPERTIES
里面记录了一些链接ElasticSearch需要的参数配置(这里跟hive数据写入phoenix很像)。好了,现在我们在这个表里面导一些数据:
hive> insert into table iteblog select * from test limit 100;
上面的SQL运行完之后我们可以看到表所在的HDFS目录是没有数据的:
hive > dfs -ls /user/iteblog/hive/warehouse/iteblog.db/iteblog;
hive >
我们到ElasticSearch里面可以发现已经多了一个index和type,就是我们在建表时指定的 es.resource
,而且ElasticSearch为我们生成type的mapping如下:
{"iteblog": {"properties": {"name": {"type": "string"}, "id": {"type": "long"}}}
}
这就Hive表里面的字段,类型都对应了。但是我们发现ElasticSearch中的 iteblog/iteblog
每行数据对应的id都是随机生成的,不过我们可以在建Hive表的时候加上 es.mapping.id
参数来指定我们自定义的id如下:
CREATE EXTERNAL TABLE iteblog (id bigint,name STRING)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.resource' = 'iteblog/iteblog', 'es.nodes'='www.iteblog.com','es.port'='9003','es.mapping.id' = 'id');
这样ElasticSearch中的 iteblog/iteblog
对应的id将会和Hive中的id字段一一对应。当然其他的字段也可以设置相应的mapping,可以通过 es.mapping.names
参数实现。
如何存Json数据
如果我们Hive里面的字段是Json数据,我们希望在ElasticSearch中解析这个json数据,然后在ElasticSearch中将解析的数据存起来,比如我们的Json数据格式为:{"id":"123","name":"iteblog"}
,我们可以在建Hive表的时候加上 es.input.json
参数,这样ElasticSearch会解析这个json数据,如下:
CREATE EXTERNAL TABLE iteblog (json STRING)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.resource' = 'iteblog/iteblog', 'es.nodes'='www.iteblog.com','es.port'='9003','es.input.json' = 'yes');
这样ElasticSearch为我们生成的mapping为:
{"iteblog": {"properties": {"name": {"type": "string"}, "id": {"type": "string"}}}
}
而不是
{"iteblog": {"properties": {"json": {"type": "string"}}}
}
如果Hive中的数据是Json字段,但是在写ElasticSearch的时候使用了 es.input.json
配置,这时候在Hive里面查数会发现数据都是NULL:
hive > select * from iteblog limit 10;
OK
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
Time taken: 0.057 seconds, Fetched: 10 row(s)
数据为json的时候我们同样可以指定ElasticSearch的id生成的规则,如下:
CREATE EXTERNAL TABLE iteblog (json STRING)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.resource' = 'iteblog/iteblog', 'es.nodes'='www.iteblog.com','es.port'='9003','es.input.json' = 'yes','es.mapping.id' = 'id');
这样就会把Json里面的id当作ElasticSearch中的id。
动态处理type
有时候我们可能希望根据数据的类别不一样来将数据存放到ElasticSearch中不同的type中,我们可以通过如下设置实现
CREATE EXTERNAL TABLE iteblog (id bigint,name STRING,type STRING)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.resource' = 'iteblog/{type}', 'es.nodes'='www.iteblog.com','es.port'='9003');
这样ElasticSearch会自动获取Hive中的type字段的值,然后将不同type的数据存放到ElasticSearch中不同的type中。如果Hive中的字段是json格式,比如 {"id":"123","name":"iteblog","type":"A"}
,我们同样可以通过下面设置实现:
CREATE EXTERNAL TABLE iteblog (json STRING)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.resource' = 'iteblog/{type}', 'es.nodes'='www.iteblog.com','es.port'='9003','es.input.json' = 'yes');
这样ElasticSearch会自动为我们解析json中的type字段的值,然后决定将这条记录放到ElasticSearch中对应的type中。
Hive类型和ElasticSearch类型映射
Hive类型 | Elasticsearch类型 |
---|---|
void | null |
boolean | boolean |
tinyint | byte |
smallint | short |
int | int |
bigint | long |
double | double |
float | float |
string | string |
binary | binary |
timestamp | date |
struct | map |
map | map |
array | array |
union | 目前不支持 |
decimal | string |
date | date |
varchar | string |
char | string |
转载于:https://my.oschina.net/u/2000675/blog/1573604
hive数据写入elasticsearch相关推荐
- Hive数据导入Elasticsearch
Elasticsearch Jar包准备 所有节点导入elasticsearch-hadoop-5.5.1.jar /opt/cloudera/parcels/CDH-5.12.0-1.cdh5.12 ...
- flink 读取文件数据写入ElasticSearch
前言 es是大数据存储的必备中间件之一,通过flink可以读取来自日志文件,kafka等外部数据源的数据,然后写入到es中,本篇将通过实例演示下完整的操作过程: 一.前置准备 1.提前搭建并开启es服 ...
- elasticsearch备份与恢复4_使用ES-Hadoop将ES中的索引数据写入HDFS中
背景知识见链接:elasticsearch备份与恢复3_使用ES-Hadoop将HDFS数据写入Elasticsearch中 项目参考<Elasticsearch集成Hadoop最佳实践> ...
- Elasticsearch和Hive整合,将hive数据同步到ES中
1 Elasticsearch整合Hive 1.1 软件环境 Hadoop软件环境 Hive软件环境 ES软件环境 1.2 ES-Hadoop介绍 1.2.1 官网 https://www.elast ...
- 利用SparkSQL(java版)将离线数据或实时流数据写入hive的用法及坑点
1. 通常利用SparkSQL将离线或实时流数据的SparkRDD数据写入Hive,一般有两种方法.第一种是利用org.apache.spark.sql.types.StructType和org.ap ...
- 《从0到1学习Flink》—— Flink 写入数据到 ElasticSearch
前言 前面 FLink 的文章中我们已经介绍了说 Flink 已经有很多自带的 Connector. 1.<从0到1学习Flink>-- Data Source 介绍 2.<从0到1 ...
- spark抽取mysql数据到hive_使用spark将内存中的数据写入到hive表中
使用spark将内存中的数据写入到hive表中 hive-site.xml hive.metastore.uris thrift://master:9083 Thrift URI for the re ...
- Elasticsearch 架构原理—— 新数据写入过程
前言 在分布式日志服务架构中,我们只需要将logstash的output指向ES就可以了,但是,写入的数据是如何变成Elasticsearch里可以被检索和聚合的索引内容的呢?本文重点介绍数据在写入E ...
- flink源码分析_Flink源码分析之深度解读流式数据写入hive
前言 前段时间我们讲解了flink1.11中如何将流式数据写入文件系统和hive [flink 1.11 使用sql将流式数据写入hive],今天我们来从源码的角度深入分析一下.以便朋友们对flink ...
最新文章
- 通过代码动态创建Windows服务
- P5829 【模板】失配树
- 只需几分钟即可安装Red Hat Container Development Kit(视频)
- java类只读怎么办_如何在Java中制作一个只读类?
- ZZULIOJ069:向z同学学习
- 【记录】jenkins 安装及环境配置(二)
- python常用模块:pickle、shelve、json、xml、configparser
- linux 日志定时轮询流程详解(logrotate)
- Notepad++ 大小写转换
- 95-36-030-ChannelHandler-ChannelInboundHandler
- 基于webview的选择滑动控件(PC和wap版)
- 【单片机】简单的时钟代码
- 给予树莓派的linux登陆密码,新手教程:如何 SSH 进入树莓派
- csSEnet注意力网络
- 返利平台php,MallWWI新模式返利商城系统 php版 v1.2.7
- 11.	Zigbee应用程序框架开发指南 - 命令行接口(CLI)
- 4位加法器四级流水线、4位加法器两级流水线以及32位加法器八级流水线设计
- 基于微信小程序的毕业设计题目(29)php家政服务预约小程序(含开题报告、任务书、中期报告、答辩PPT、论文模板)
- 短语匹配-match_phrase以及slop参数
- 流量回放框架jvm-sandbox-repeater的实践
热门文章
- 电脑微信扫一扫在哪_怎么进入和管理企业微信的后台?
- 增加一行减少一行_【第2171期】一行 CSS 代码的魅力
- python中 if __name__ == ‘__main__‘
- 大一计算机应用的实验报告,大一《计算机应用基础》实验报告1.doc
- 计算机英语翻译3000字,英语专四作文满分范文(二十八):计算机翻译
- 怎么结束linux里的redis进程,linux 怎么结束redis的monitor命令
- Java案例:读取XML文档
- SSM框架笔记10:Spring AOP基础
- 【BZOJ4300】绝世好题,位运算相关DP
- html5酷炫表白代码_七夕表白代码,樱花特效+爱心特效+花瓣+评论留言功能等