es-hadoop-hive 时间格式问题记录

来源:互联网 发布:淘宝网板鞋 编辑:程序博客网 时间:2024/05/29 10:49

Elasticsearch for Apache Hadoop
使用 es-hive 同步数据,插入 hive 表即插入 es,读取 hive 表数据即读取 es 数据


创建 es-hive 测试表
drop TABLE IF EXISTS `es_dim_s.es_test_time`;CREATE TABLE IF NOT EXISTS `es_dim_s.es_test_time` (  `service_date` STRING comment 'yyyy-MM-dd',  `service_date_time` STRING comment 'yyyy-MM-ddTHH:mm:ss')comment '时间测试'STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'TBLPROPERTIES('es.nodes' = 'es1:9201,es2:9202,es2:9203','es.index.auto.create' = 'true','es.resource' = 'bi_m_sbkcq_test-{service_date:YYYY.MM}/es_test_time','es.index.read.missing.as.empty' = 'true','es.batch.size.bytes' = '10mb','es.batch.size.entries' = '3000','es.mapping.names' = '');

如果 hive 中时间类型指定为 date ,对于 yyyy-MM-dd 类型的数据会默认添加时区 例如自动变为 2017-05-04+08:00


插入测试数据
>hiveinsert into table es_dim_s.es_test_time  select '2016-10-01','2016-10-01 10:00:00’;



执行后报错 hive 客户端报错


找到 es-hadoop 源码中报错位置line:105





报错原因分析
es-hadoop jar 默认使用 ISO 进行时间格式化,2016-10-01 10:00:00 不属于 ISO标准,默认改写为 2016-10-01T10:00:00 格式进行 datetime 类型数据插入
修改导入Hive测试语句的 date-time 格式为 ISO 标准格式即可正确导入 hive-es

重新执行导入操作即可成功
>hiveinsert into table es_dim_s.es_test_time  select '2016-10-01','2016-10-01T10:00:00’;



es 时间字段动态 mapping-template 如下
"dynamic_templates": [        {          "date_fields": {            "match": "*_date",            "mapping": {              "type": "date"            }          }        },        {          "time_fields": {            "match": "*_time",            "mapping": {              "type": "date"            }          }        },


date 不指定 format,指定具体 format 后es 客户端会抛出错误
Caused by: java.lang.IllegalArgumentException: Invalid format: [yyyy-MM-ddTHH:mm:ss]: Illegal pattern component: T



hive 时间格式转换
select from_unixtime(unix_timestamp("2014-11-10 08:00:00","yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd'T'HH:mm:ss") as a from es_dim_sbkcq.es_third_relation;



2014-11-10 和 20141110相互转化的办法:
#from_unixtime && unix_timestamp-- 20141110select from_unixtime(unix_timestamp('2014-11-10','yyyy-mm-dd'),'yyyymmdd') from default.dual;-- 2014-11-10select from_unixtime(unix_timestamp('20141110','yyyymmdd'),'yyyy-mm-dd') from default.dual;#substr + concat-- 20141110select concat(substr('2014-11-10',1,4),substr('2014-11-10',6,2),substr('2014-11-10',9,2)) from default.dual;-- 2014-11-10select concat(substr('20141110',1,4),'-',substr('20141110',5,2),'-',substr('20141110',7,2)) from default.dual;hive 获取当前时间select from_unixtime(unix_timestamp(),"yyyy-MM-dd'T'HH:mm:ss") from es_dim_sbkcq.es_third_relation;


ISO时间格式探索 - 相关干货

关于“时间”的一次探索
2 0
原创粉丝点击