我在《使用Hive读取ElasticSearch中的数据》文章中介绍了如何使用Hive读取ElasticSearch中的数据,本文将接着上文继续介绍如何使用Hive将数据写入到ElasticSearch中。在使用前同样需要加入 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
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> 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://www.iteblog.com/)

通过Hive将数据写入到ElasticSearch的更多相关文章

  1. logstash5安装并实现mariadb数据写入到elasticsearch

    java环境这里默认安装了 ,一般源码安装,这里就不说了 一.安装logstash 安装logstash可以用yum安装,也可以用源码安装: yum安装: 1.导入GPG: rpm --import ...

  2. Logstash读取Kafka数据写入HDFS详解

    强大的功能,丰富的插件,让logstash在数据处理的行列中出类拔萃 通常日志数据除了要入ES提供实时展示和简单统计外,还需要写入大数据集群来提供更为深入的逻辑处理,前边几篇ELK的文章介绍过利用lo ...

  3. SQL数据同步到ElasticSearch(三)- 使用Logstash+LastModifyTime同步数据

    在系列开篇,我提到了四种将SQL SERVER数据同步到ES中的方案,本文将采用最简单的一种方案,即使用LastModifyTime来追踪DB中在最近一段时间发生了变更的数据. 安装Java 安装部分 ...

  4. flume将数据写入各个组件

    一.flume集成hdfs,将数据写入到hdfs           a1.sources = r1           a1.sinks = k1           a1.channels = c ...

  5. 【原创】大叔经验分享(26)hive通过外部表读写elasticsearch数据

    hive通过外部表读写elasticsearch数据,和读写hbase数据差不多,差别是需要下载elasticsearch-hadoop-hive-6.6.2.jar,然后使用其中的EsStorage ...

  6. spark 将dataframe数据写入Hive分区表

    从spark1.2 到spark1.3,spark SQL中的SchemaRDD变为了DataFrame,DataFrame相对于SchemaRDD有了较大改变,同时提供了更多好用且方便的API.Da ...

  7. elasticsearch备份与恢复4_使用ES-Hadoop将ES中的索引数据写入HDFS中

    背景知识见链接:elasticsearch备份与恢复3_使用ES-Hadoop将HDFS数据写入Elasticsearch中 项目参考<Elasticsearch集成Hadoop最佳实践> ...

  8. Elasticsearch Lucene 数据写入原理 | ES 核心篇

    前言 最近 TL 分享了下 <Elasticsearch基础整理>https://www.jianshu.com/p/e8226138485d ,蹭着这个机会.写个小文巩固下,本文主要讲 ...

  9. Elasticsearch(GEO)数据写入和空间检索

    Elasticsearch简介 什么是 Elasticsearch? Elasticsearch 是一个开源的分布式 RESTful搜索和分析引擎,能够解决越来越多不同的应用场景. 本文内容 本文主要 ...

随机推荐

  1. IQ调制原理

    现代通信中,IQ调制基本上属于是标准配置,因为利用IQ调制可以做出所有的调制方式. 但是IQ调制到底是怎么工作的,为什么需要星座映射,成型滤波又是用来干嘛的.这个呢,讲通信原理的时候倒是都会泛泛的提到 ...

  2. 获取APP和设备相关信息

    APP NAME: [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] APP ...

  3. php使用solr全文搜索引擎

    前言 本来以为网上已经有了类似博文,不想重复,可是一圈搜下来,都是一些内容不甚明了的文章,或者solr版本太过老,参考价值不高,更有甚者,直接拷贝的别人的内容.一篇博客,各大平台都能看到,也不见转载链 ...

  4. keras学习简单线性回归【1】

    转自:https://morvanzhou.github.io/tutorials/machine-learning/keras/2-1-regressor/ 总的代码的过程就是: 1.导入模块+创建 ...

  5. malloc调用后经历了什么?

    进程生成虚拟地址空间,有堆地址,由于是虚拟地址,所以没有做内存碎片化处理,只是在虚拟内存不够的时候调用brk,进行堆大小的调整,然后申请到虚拟内存是页,同MMU映射到物理地址,然后并不是每个页都预先加 ...

  6. plt.contour 与 plt.contourf

    contour:轮廓,等高线 1.为等高线上注明等高线的含义: cs = plt.contour(x, y, z) plt.clabel(cs, inline=True, fontsize=10)#i ...

  7. 如何学Python

    如何学习Python? Python上手很容易, 基本有其他语言编程经验的人可以在1周内学会Python最基本的内容.它们包括:1.常用内置类型(int, float, bool, bytes, st ...

  8. Dart- move html element

    今天给出一个例程,像是个小游戏!哈哈 一 html //anagram.html <!DOCTYPE HTML> <html> <head> <title&g ...

  9. Java编写email实现内容换行

    用java编写的发送邮件要使用<br>来实现换行,而不能使用\n,或者\r\n // 5. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发 ...

  10. vim自动格式化

    ,gg 跳转到第一行 ,shift+v 转到可视模式 ,shift+g 全选 ,按下神奇的 = 你会惊奇的发现代码自动缩进了,呵呵,当然也可能是悲剧了.