在ElasticSearch中将Spark DateType字段索引为日期

时间:2018-09-10 07:30:49

标签: apache-spark elasticsearch

我正在尝试使用elasticsearch-hadoop连接器在ElasticSearch中为以下模式的DataFrame编制索引。

 |-- ROW_ID: long (nullable = false)
 |-- SUBJECT_ID: long (nullable = false)
 |-- HADM_ID: long (nullable = true)
 |-- CHARTDATE: date (nullable = false)
 |-- CATEGORY: string (nullable = false)
 |-- DESCRIPTION: string (nullable = false)
 |-- CGID: integer (nullable = true)
 |-- ISERROR: integer (nullable = true)
 |-- TEXT: string (nullable = true)

将此DataFrame写入ElasticSearch时,“ CHARTDATE”字段被写入为long。根据我正在使用的连接器的文档(如下所示),Spark中的DateType字段应在ElasticSearch中写为字符串格式的日期。当我希望利用日期字段在Kibana中建立一些可视化功能时,由于久而久之,它们被写成问题。

https://www.elastic.co/guide/en/elasticsearch/hadoop/6.4/spark.html

用于产生错误的代码

val elasticOptions = Map(
      "es.nodes"              -> esIP,
      "es.port"               -> esPort,
      "es.mapping.id"         -> primaryKey,
      "es.index.auto.create"  -> "yes",
      "es.nodes.wan.only"     -> "true",
      "es.write.operation"    -> "upsert",
      "es.net.http.auth.user" -> esUser,
      "es.net.http.auth.pass" -> esPassword,
      "es.spark.dataframe.write.null" -> "true",
      "es.mapping.date.rich" -> "true"
    )
castedDF.saveToEs(index, elasticOptions)

是否缺少将这些值写为ES日期的步骤?

2 个答案:

答案 0 :(得分:1)

很长一段时间我没有用ElasticSearch进行搜索;但是这个DateType问题对我来说真的很烦。

我要做的是: *在Spark中将DateType转换为纪元时间戳(不确定此处是否必要) *在初始化索引方案时,在Kibana中指定或使用curL PUT请求指定字段CHARTDATE的日期类型如下:

    let storyboard = UIStoryboard(name: STORYBOARD_NAME, bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: VC_IDENTIFIER) as! YourViewController
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)

我不知道Elastic 6.4所做的任何更改,如果您找到更好的解决方案,请稍后再与我们分享!

我知道这并不是最好的解决方案,必须在运行Spark的saveToEs操作之前先放入索引。但这确实是为我解决的问题。

答案 1 :(得分:1)