hive数据导入ElasticSearch坑记录

本文介绍了如何解决Elasticsearch中拼音分词和IK分词出现的非法参数异常问题,包括修改拼音分词源码并重新编译打包,以及清空IK分词词库来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:

CDH5.16.2(hive1.1.0)、ES6.7.2

1、 关于拼音分词

    org.elasticsearch.hadoop.rest.EsHadoopRemoteException: illegal_argument_exception: startOffset must be non-negative, and endOffset must be >= startOffset, and offsets must not go backwards startOffset=0,endOffset=10,lastStartOffset=9 for field 'enterprise_name.pinyin' (这里报的是拼音分词)

在网上搜了很多 ,还是有很靠谱的文章:

https://github.com/medcl/elasticsearch-analysis-pinyin/pull/206/commits/7cbc3d8926c8549b1049b90e90fce415097990be

根据里面的修改了拼音分词的源码,重新使用maven编译打包,将elasticsearch-analysis-pinyin-6.3.0.jar改为elasticsearch-analysis-pinyin-6.7.2.jar,然后将拼音分词的zip包打开,将这个新打包的jar替换进去,重新在线上把旧的拼音分词remove掉再install新的zip,重启,ok。

2、关于IK分词

org.elasticsearch.hadoop.rest.EsHadoopRemoteException: illegal_argument_exception: startOffset must be non-negative, and endOffset must be >= startOffset, and offsets must not go backwards startOffset=6,endOffset=7,lastStartOffset=7 for field 'enterprise_name'(这里报的是这个字段,这个字段我用的ik_max_word分词)

将IK分词词库 extra_new_word.dic 里的词先全部清空(移到其他地方),然后正常导入,导入数据后再把分词词库移回去就ok了。

在hive中数据导入ES,需要一个包:add jar /root/work/elasticsearch-hadoop-6.7.2.jar;

mapping片段:

PUT /enterprise_credit_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "index": {
      "analysis": {
        "analyzer": {
          "pinyin_analyzer": {
            "tokenizer": "my_pinyin"
          }
        },
        "tokenizer": {
          "my_pinyin": {
            "type" : "pinyin",
            "keep_first_letter":true,
            "keep_separate_first_letter" : true,
            "keep_full_pinyin" : true,
            "keep_original" : true,
            "limit_first_letter_length" : 20,
            "lowercase" : true
          }
        }
      }
    }
  },
  "mappings": {
    "enterprise_credit_type": {
      "properties": {
        "enterprise_name": {
          "type": "text",
          "index": true,
          "analyzer": "ik_max_word",
          "fields": {
            "pinyin": {
              "type": "text",
              "store": false,
              "term_vector": "with_offsets",
              "analyzer": "pinyin_analyzer",
              "boost": 10
            }
          }
        },
        "operators": {
          "type": "keyword",
          "index": true,
          "fields": {
            "pinyin": {
              "type": "text",
              "store": false,
              "analyzer": "pinyin_analyzer",
              "boost": 10
            }
          }
        },
        "registered_money":{
          "type": "keyword",
          "ignore_above": 256
        },

。。。。。。

在hive中建表:

create external table APP_json_result_external(
enterprise_name         string           ,
operators               string   ,                       

。。。。。

)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES('es.resource' = '/enterprise_credit_index/enterprise_credit_type',
'es.mapping.id' = 'unified_social_credit_code',
'es.nodes'='10.10.10.10:9200,10.10.10.10:9201,10.10.10.10:9202',
'es.nodes.wan.only'='true',
'es.index.auto.create' = 'false',
'es.write.operation' = 'upsert',
'es.batch.write.refresh'='true',
'es.index.read.missing.as.empty'='false');

将数据导入:

insert OVERWRITE table APP_json_result_external XXX,XXX,XXX from tableName;

this all, have fun !

 

### ElasticsearchHive 的集成 Elasticsearch 可以通过 `elasticsearch-hadoop` 组件与 Hive 进行深度集成,实现数据Hive 表和 Elasticsearch 之间的双向读写。这种集成方式使得用户可以利用 Hive 的 SQL 查询能力对 Elasticsearch 中的数据进行分析,并支持将数据持久化到 HDFS 生态中 [^2]。 #### 数据Hive 写入 Elasticsearch 使用 `elasticsearch-hadoop` 提供的 Hive 集成功能,可以通过创建外部表的方式将 Hive 表映射到 Elasticsearch 索引。以下是一个典型的 HiveQL 示例: ```sql CREATE EXTERNAL TABLE es_hive_table ( id STRING, name STRING, age INT ) STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler' LOCATION '/user/hive/es_data' TBLPROPERTIES( 'es.resource' = 'my_index/my_type', 'es.nodes' = 'localhost:9200', 'es.input.json' = 'true' ); ``` 在此配置下,向 `es_hive_table` 插入数据会自动将记录同步到 Elasticsearch 的 `my_index` 索引中 [^1]。 #### 从 Elasticsearch 查询数据并通过 Hive 分析 除了写入操作,Hive 也可以作为查询引擎,直接从 Elasticsearch 中读取数据。通过类似的外部表定义方式,Hive 可以透明地访问 Elasticsearch 数据并执行 SQL 查询: ```sql SELECT * FROM es_hive_table WHERE age > 30; ``` 该查询会由 Hive 转换为 Elasticsearch 的 DSL 查询语句,并返回匹配的结果集 [^2]。 #### 字段映射与类型转换 由于 HiveElasticsearch数据模型存在差异,字段映射是集成过程中的关键环节。例如,Elasticsearch 中的 `date` 类型字段需要在 Hive 表中对应为 `STRING` 或 `TIMESTAMP` 类型,并确保数据格式一致。此外,嵌套对象(`nested`)或地理位置(`geo_point`)等复杂类型也需要特殊处理 [^2]。 #### 常见问题与异常处理 在实际部署过程中,可能会遇到通信异常,例如 HiveServer2 在查询 Elasticsearch 映射表时抛出 `SocketException`。这类问题通常与网络配置、Thrift 服务状态或 `elasticsearch-hadoop` 的版本兼容性有关。建议检查 HiveHadoop 集群的配置,并参考官方文档进行调优 [^4]。 #### 跨数据源查询的限制与替代方案 虽然 Presto 支持分别查询 HiveElasticsearch,但它不支持跨数据源的 JOIN 操作。因此,一种常见的解决方案是先将 Elasticsearch 数据导入 Hive,再进行关联查询。这可以通过 `elasticsearch-hadoop` 的批量导入功能实现 [^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值