将日期更改为 Instant 类型的 Cassandra 脚本

Cassandra script to change date to Instant type

是否可以更改cassandra中的所有数据值 来自:2020-05-18T14:18:45.878Z1593402243336(如 Instant Java 类型)

此列中的所有数据都是text

类型

我想知道如何编写将日期从 2020-05-18T14:18:45.878Z 更改为 1593402243336

的脚本

在 Cassandra 中,有一个单独的 timestamp 类型来保存此类信息。在内部,它将数据存储为 8 字节长的值,表示以毫秒为单位的时间。该值通过驱动程序访问,并且可以转换为特定于所用编程语言的类型的值。如果您通过 cqlsh 访问这些值,您需要将它们打印为 2020-05-18T14:18:45.878Z,但实际上它仍然是引擎盖下的 long 类型。

要执行此类转换,您需要两件事:

  1. 您需要添加另一列 timestamp 类型 - 您无法更改现有列的类型
  2. 您需要使用一些工具来执行此类转换,但这实际上取决于您的要求。你可以这样做,例如:
val data = { spark.read.format("org.apache.spark.sql.cassandra")
    .options(Map( "table" -> "", "keyspace" -> ""))
    .load().withColumnRenamed("text_column", "date_column")}
data.write.format("org.apache.spark.sql.cassandra")
   .options(Map("table" -> "", "keyspace" -> "")).mode("append").save()
  • DSBulk. You can unload data from your database onto the disk, and then load back, but use timestamp column instead of the text column by providing the custom mapping with -m option. There is a serie of blog posts about DSBulk, that could provide more information & examples: 1, 2, 3, 4, 5, 6