【问题标题】:How to write datetime with microseconds to Cassandra with Spark?如何使用 Spark 将具有微秒的日期时间写入 Cassandra?
【发布时间】:2021-08-07 15:57:56
【问题描述】:

我想将特定日期格式流式传输到 Cassandra datetime 列。

我的传入日期格式为以下日期格式:

"%Y-%m-%dT%H:%M:%S.%f"

e.g. "2021-05-18T11:12:13.123456"

我的 Cassandra 表是:

CREATE TABLE table_name (

  id text,
  timestamp timestamp,
  PRIMARY KEY (id)

)

我的 Spark 作业的摄取如下:

val df = spark.readStream
    .format("kafka")
    .option("kafka.bootstrap.servers", kafkahost)
    .option("subscribe", kafkatopic)
    .option("startingOffsets", "latest")
    .load()

val jsonSchema = StructType(
      List(
        StructField("id", StringType, true),
        StructField("timestamp", StringType, true)
      )
    )

val cassandraDF = df
    .withColumn("value", col("value").cast(StringType)),
    .withColumn("json", from_json(col("value"), jsonSchema))
    .select("json.*")

此时cassandraDF有两列id和timestamp,都是StringType。

那么,写 Cassandra 也很简单:

cassandraDF
    .writeStream
    .outputMode(OutputMode.Append)
    .format("org.apache.spark.sql.cassandra")
    .option("keyspace", cassandra_keyspace)
    .option("table", cassandra_table)
    .option("checkpointLocation", "/tmp/checkpoints/cassandra")
    .start()
    .awaitTermination()

当我流式传输数据时,我得到以下ERROR:

在 org.apache.spark.sql.execution.streaming.StreamExecution.org$apache$spark$sql$execution$streaming$StreamExecution$$runStream(StreamExecution.scala:297)

在 org.apache.spark.sql.execution.streaming.StreamExecution$$anon$1.run(StreamExecution.scala:193)

原因:org.apache.spark.SparkException:作业因阶段失败而中止:阶段 0.0 中的任务 0 失败 1 次,最近一次失败:阶段 0.0 中丢失任务 0.0(TID 0,本地主机,执行程序驱动程序): java.lang.IllegalArgumentException: 无效日期:2021-05-18T11:12:13.123456

所以我尝试流式传输其他日期格式。例如没有微秒:

"%Y-%m-%dT%H:%M:%S"

e.g. "2021-05-18T11:12:13"

并且流没有任何问题。此日期日期格式已被接受。

我的问题是:

  • 如何格式化我的日期,以便我可以通过 Spark 将微秒流式传输到 Cassandra?

【问题讨论】:

    标签: scala apache-spark cassandra timestamp spark-cassandra-connector


    【解决方案1】:

    Cassandra 仅支持时间戳类型的毫秒分辨率。默认情况下,不允许将字符串写入时间戳字段,但 Spark 连接器具有隐式转换,如 this。如果你查看this source code,你会发现它只支持以毫秒为单位的时间戳解析。

    因此,解决方案是将您的 timestamp 列从 string 转换为 Spark timestamp(仅适用于 Spark 3.x where support for microseconds resolution was added):

    val cassandraDF = df
        .withColumn("value", col("value").cast(StringType)),
        .withColumn("json", from_json(col("value"), jsonSchema))
        .select("json.*")
        .withColumn("timestamp"), $"timestamp".cast("timestamp")
    

    但无论如何,它只会在几毫秒内存储在 Cassandra 中。

    【讨论】:

    • 我使用的是 Spark 2.4.x。我尝试了只包含毫秒而不是微秒的字符串的解决方案,它起作用了。对于我的用例来说,这就足够了。有趣的是,当我在 Cassandra 时间戳列中执行 SELECT 时,它会显示微秒。所以现在即使我摄取毫秒,SELECT 的输出也类似于2021-05-19 08:23:16.858000+0000。
    • 关于显示 - cqlsh 的默认设置非常糟糕,因为 Python 中的 %f 格式以微秒为单位打印:github.com/apache/cassandra/blob/trunk/pylib/cqlshlib/…
    • cassandra.apache.org/doc/latest/cql/… - 关于时间戳的文档,说明它以毫秒为单位。有时会令人困惑,因为分配给值(写入时间)的时间戳以微秒为单位
    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2016-12-14
    • 2015-09-20
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多