【问题标题】:Invalid timestamp when reading Elasticsearch records with Spark使用 Spark 读取 Elasticsearch 记录时时间戳无效
【发布时间】:2021-04-27 17:10:11
【问题描述】:

在使用带有 elasticsearch-hadoop 库的 Spark 读取 Elasticsearch 记录时,我得到了无效的时间戳。我正在使用以下 Spark 代码进行记录读取:

val sc = spark.sqlContext
  val elasticFields = Seq(
    "start_time",
    "action",
    "category",
    "attack_category"
  )

  sc.sql(
    "CREATE TEMPORARY TABLE myIndex " +
      "USING org.elasticsearch.spark.sql " +
      "OPTIONS (resource 'aggattack-2021.01')" )

  val all = sc.sql(
    s"""
      |SELECT ${elasticFields.mkString(",")}
      |FROM myIndex
      |""".stripMargin)
  all.show(2)

这导致以下结果:

+-----------------------+------+---------+---------------+
|start_time             |action|category |attack_category|
+-----------------------+------+---------+---------------+
|1970-01-19 16:04:27.228|drop  |udp-flood|DoS            |
|1970-01-19 16:04:24.027|drop  |others   |DoS            |
+-----------------------+------+---------+---------------+

但我期待当前年份的时间戳,例如2021-01-19 16:04:27.228。在弹性中,start_time 字段具有以毫秒为单位的 unixtime 格式 -> start_time": 1611314773.641

【问题讨论】:

  • 尝试读取为字符串并将其转换为 Spark 中的时间戳。在选项中添加:OPTIONS('resource'='aggattack-2021.01', 'es.mapping.date.rich'= 'false') 然后使用:from_unixtime(start_time) as start_time
  • 你好,我试过了,但它导致另一个异常 => The value (1609467228) of the type (java.lang.Long) cannot be converted to the string type

标签: apache-spark elasticsearch elasticsearch-hadoop


【解决方案1】:

问题在于 ElasticSearch 中的数据。 start_time 字段被映射为epoch_seconds 并包含具有三个小数位的值纪元秒(例如1611583978.684)。在我们将纪元时间转换为没有任何小数位的毫秒后,一切正常

【讨论】:

    猜你喜欢
    • 2017-09-08
    • 2020-04-30
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多