我在 使用 Hive 读取 ElasticSearch 中的数据 文章中介绍了如何使用 Hive 读取 ElasticSearch 中的数据, 本文将接着上文继续介绍如何使用 Hive 将数据写入到 ElasticSearch 中 在使用前同样需要加入 elasticsearch-hadoop-2.3.4.jar 依赖, 具体请参见前文介绍 我们先在 Hive 里面建个名为 iteblog 的表, 如下 : id bigint, name STRING) 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` COMMENT 'from deserializer') 1 / 6
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 里面记录了一些链接 Ela sticsearch 需要的参数配置 好了, 现在我们在这个表里面导一些数据 : 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": { 2 / 6
"properties": { "name": {, "id": { "type": "long" 这就 Hive 表里面的字段, 类型都对应了 但是我们发现 ElasticSearch 中的 iteblog/iteblog 每行数据对应的 id 都是随机生成的, 不过我们可以在建 Hive 表的时候加上 es.mapping.id 参数来指定我们自定义的 id 如下 : id bigint, name STRING) 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 数据, 然后在 Elas ticsearch 中将解析的数据存起来, 比如我们的 Json 数据格式为 :{"id":"123","name":"iteblog", 我们可以在建 Hive 表的时候加上 es.input.json 参数, 这样 ElasticSearch 会解析这个 json 数据, 如下 : json STRING) TBLPROPERTIES('es.resource' = 'iteblog/iteblog', 'es.nodes'='www.iteblog.com','es.port'='9003',' es.input.json' = 'yes'); 3 / 6
这样 ElasticSearch 为我们生成的 mapping 为 : { "iteblog": { "properties": { "name": {, "id": { 而不是 { "iteblog": { "properties": { "json": { 如果 Hive 中的数据是 Json 字段, 但是在写 ElasticSearch 的时候使用了 es.input.json 配置, 这时候在 Hive 里面查数会发现数据都是 : hive > select * from iteblog limit 10; OK 4 / 6
Time taken: 0.057 seconds, Fetched: 10 row(s) 数据为 json 的时候我们同样可以指定 ElasticSearch 的 id 生成的规则, 如下 : json STRING) 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 中, 我们可以通过如下设置实现 id bigint, name STRING, type STRING) TBLPROPERTIES('es.resource' = 'iteblog/{type', 'es.nodes'='www.iteblog.com','es.port'='9003'); 这样 ElasticSearch 会自动获取 Hive 中的 type 字段的值, 然后将不同 type 的数据存放到 ElasticSearc h 中不同的 type 中 如果 Hive 中的字段是 json 格式, 比如 {"id":"123","name":"iteblog","type":"a", 我们同样可以通过下面设置实现 : json STRING) TBLPROPERTIES('es.resource' = 'iteblog/{type', 'es.nodes'='www.iteblog.com','es.port'='9003',' es.input.json' = 'yes'); 5 / 6
Powered by TCPDF (www.tcpdf.org) 这样 ElasticSearch 会自动为我们解析 json 中的 type 字段的值, 然后决定将这条记录放到 ElasticSear ch 中对应的 type 中 Hive 类型和 ElasticSearch 类型映射 Hive 类型 Elasticsearch 类型 void null boolean boolean tinyint byte smallint short int int bigint long double double float float binary binary timestamp date struct map map map array array union 目前不支持 decimal date date varchar char 本博客文章除特别声明, 全部都是原创! 转载本文请加上 : 转载自过往记忆 (https://www.iteblog.com/) 本文链接 : () 6 / 6