我从 jdbc 源读取数据并将其直接写入 Elasticsearch 索引。 当我查询 ES 中的数据时,我看到数据框中的所有时间戳字段都转换为 long
查看下面的代码
val appName="ExractToolEngine"
val master = "local[2]"
val conf = new SparkConf().setAppName(appName).setMaster(master)
conf.set("es.write.operation", "index")
conf.set("es.mapping.id", "user_id")
conf.set("index.mapper.dynamic", "true")
conf.set("es.mapping.rich.date", "true")
def main(args: Array[String]): Unit = {
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
import sqlContext.implicits._
val srcData = sqlContext.read.format("jdbc").
options(Map("driver"->"com.jdbc.Driver",
"url" -> "jdbc...",
"dbtable"-> "tbl",
"partitionColumn"-> "user_id",
"lowerBound"-> "1",
"upperBound"-> "1000000",
"numPartitions"-> "50"
)
).load()
srcData.filter("user_id>=1 and user_id<=1000000").saveToEs("test_users/sm_1")
当我运行 srcData.printSchema()
我得到:
|-- dwh_insert_ts: timestamp (nullable = true)
|-- dwh_update_ts: timestamp (nullable = true)
当我在 http://localhost:9200/test_users/_mapping/sm_1 上查询索引映射时
我明白了
"properties": {
"dwh_insert_ts": {
"type": "long"
},
"dwh_update_ts": {
"type": "long"
},
是否有办法强制 elasticsearch 保留时间戳并进行转换?
最佳答案
您可以使用多种日期格式 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
关于elasticsearch - spark 在 elasticsearch 中写入时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38080330/