弹性搜索 - 在 Spark 中将多个字段作为映射 ID



我对弹性搜索很陌生。我正在使用elasticsearch-hadoop 6.2.4版本,我正在从HDFS读取文件,转换为 bean 对象并写入弹性搜索。我正在使用Spark Structured Streaming。

StreamingQuery query = dataSet
.writeStream()
.format("org.elasticsearch.spark.sql")
//.outputMode(OutputMode.Append())
.option("checkpointLocation", "tmpckpt1")
.option("es.nodes","abc.dev.cm.par.xy.hp")
.option("es.port","9200")
.option("es.mapping.id", "CustomerID")
.option("es.resource", "testIndex/testType")
.start();

在写作时,我给出了pojo类中的一个字段(CustomerID(作为映射iD。我们可以给出多个字段或字段组合作为映射 ID 吗?例如,我的文件包含客户 ID 和订单 ID 字段。我们可以将这两个字段组合为CustomerID + OrderID吗?

不,不能将多个属性设置为"es.mapping.id"。您可以做的一件事是,无论您想要什么复合 ID,创建它并将其附加到数据帧并使用相同的复合 ID。

根据弹性文档; 映射 id 选项是取 1 列名,所以; 您不能将多个列设置为 id。 但是您可以通过使用此值创建一个新列来解决此问题,如下所示:

dataSet.withColumn('id', CustomerID + OrderID)

或者,您可以通过在连接多个列后生成哈希 id 来使用 sha2 函数。

相关内容

最新更新