es与hive整合

在hive classpath中添加elasticsearch-hadoop.jar,以下方法任一种均可:

1、启动hiveserver2 前,在hive-site.xml文件中更改hive.aux.jars.path属性值

<property>
  <name>hive.reloadable.aux.jars.path</name>
  <value>/path/elasticsearch-hadoop.jar</value>
  <description>A comma separated list (with no spaces) of the jar files</description>
</property>

2、启动hiveserver2 时,指定hive.reloadable.aux.jars.path属性值

nohup  hive --service hiveserver2 --hiveconf hive.reloadable.aux.jars.path=/path/elasticsearch-hadoop.jar &

3、启动hiveserver2 后,在hive 命令行中执行add jar /path/elasticsearch-hadoop.jar; 命令。

实测,以上三种方法,前两种方法中jar包的路径必须是本地路径,第三种方法中的路径既可以是本地路径,也可以是hdfs路径。

配置

hive在建表时,创建external表,并使用tblproperties指定一些es相关的属性

create external table artists (...)
stored by 'org.elasticsearch.hadoop.hive.EsStorageHandler'
tblproperties('es.nodes' = 'linux-node9:9200', 'es.resource' = 'artists/_doc', 'es.index.auto.create' = 'false', 'es.index.read.missing.as.empty' = 'false');

org.elasticsearch.hadoop.hive.EsStorageHandler 是elasticsearch-hadoop.jar中的类,所以从这里就可以看出,确实是需要elasticsearch-hadoop.jar,而该jar在$HIVE_HOME/lib 下是没有的,所以需要外部添加。从该jar的groupId可以看出,jar包维护者是ES公司,所以不可能预装在hive安装包中。

es.nodes指定es集群地址

es.resource指定关联的index及其type

es.index.auto.create表示在表插入数据的时候,如果索引还没有建,是否自动创建索引。强烈建议不要自动创建索引,因为自动创建索引时会自动映射字段类型,而hive字段类型和es字段类型不是一一对应的,如果自动映射字段类型的话,在hive查询表数据的时候很可能会报类型转换错误。

es.index.read.missing.as.empty表示当索引不存在时,在hive查询表数据时是否报错。为true则不报错,为false时会报错。

映射

默认情况下,elasticsearch-hadoop 使用hive表的字段名和类型映射es中的数据。但有些情况下,在hive中可以使用的名称在es中不能使用,比如一些es的关键字。对于这种情况,可以在建hive表时指定es.mapping.names属性,值是以逗号分隔的映射名称列表,映射格式是hive字段名称:es字段名称。如下:

create external table artists (...)
stored by 'org.elasticsearch.hadoop.hive.EsStorageHandler'
tblproperties('es.nodes' = 'linux-node9:9200', 'es.resource' = 'artists/_doc', 'es.mapping.names' = 'date:@timestamp , url:url_123 ');

上例中,hive外部表artists的date列、url列分别对应es中artists索引的@timestamp字段、url_123字段。

hive不区分大小写,但是es区分。为了避免列名大小写对不上造成的信息丢失,elasticsearch-hadoop会将hive列名称全转为小写。

写数据到es

操作这个hive外部表就可对es中对应索引进行操作。

create external table artists (
    id bigint,
    name string,
    links struct<url:string, picture:string>)
    stored by 'org.elasticsearch.hadoop.hive.EsStorageHandler'
tblproperties('es.resource' = 'artists/_doc');

从另一个hive表source查询数据并将结果集插入到es中

insert overwrite table artists select null, s.name, named_struct('url', s.url, 'picture', s.picture) from source s;

假如要指定es 文档的id,则可以用tblproperties(es.mapping.id)属性。例如,假如想用hive表的id作为es文档的id,则可以这样建表:

create external table artists (
id bigint,
...)
stored by 'org.elasticsearch.hadoop.hive.EsStorageHandler'
tblproperties('es.mapping.id' = 'id', ...);

把现有的json写到es中

对于输入数据是json的情况,es-hadoop允许直接索引而不应用任何转换,数据按原样直接发送给es。在这种情况下,需要使用tblproperties(es.input.json)属性,es-hadoop期望输出表只包含一个字段,字段类型是string/varchar(如果json数据是字符串表示的话)或者是binary(如果json数据是byte[]的话),字段值就是json数据。建表语句如下:

create external table json (data string)
stored by 'org.elasticsearch.hadoop.hive.EsStorageHandler'
tblproperties('es.resource' = '...', 'es.input.json` = 'yes');

从es读取

在建表时可以指定从es读的查询语句,这样表的数据就是查询结果。如下:

create external table artists (
    id bigint,
    name string
)
stored by 'org.elasticsearch.hadoop.hive.EsStorageHandler'
tblproperties('es.resource' = 'artists/_doc', 'es.query' = '?q=me*');

es.query的值即是查询语句。

类型转换

Hive typeElasticsearch type

boolean

boolean

tinyint

byte

smallint

short

int

int

bigint

long

double

double

float

float

char/varchar/string

text/keyword

binary

binary

timestamp

date

struct

map

map

map

array

array

decimal

string

date

date

在es中创建索引时,需根据以上表格指定映射类型。

需要知道的是,如果一个hive表在建表时关联了es,则表数据其实是放在es数据目录中的,而且该hive 表不能被truncate,会报Cannot truncate non-native table artists 错误,但是可以被drop。表drop之后,es中索引不会受影响,数据也不会被删除,只有建表时location指定的目录会被删除,如果没有使用location 关键字,则会删除hive.metastore.warehouse.dir 指定的目录中的数据库名目录中的表名目录。如果再按照原来表定义语句重建表,则查询该表还是可以正常查出数据的,就好像表没有被drop 过一样。

疑问三:表或索引字段的新增、删除如何影响对方?

不会影响对方。

关联es的hive表,在建好后是不能添加或者删除字段的,会报ALTER TABLE cannot be used for a non-native table artists 错误。

es索引可以新增字段,但是这个字段永远不会有值,因为关联es的hive表新增不了字段,故es索引中这个字段不会有有效值插入。

es索引不能删除字段。

转载于:https://www.cnblogs.com/koushr/p/9505435.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值