用于创建弹性搜索的复合键索引

时间:2017-09-13 08:44:51

标签: apache-spark elasticsearch apache-spark-sql

我正在通过SparkSQL从事HDFS到Elastic搜索集成。我能够从HDFS读取csv数据并创建弹性搜索索引。要创建弹性搜索索引ID,我将使用csv数据中的一个唯一列。现在我的要求是弹性搜索索引ID应该是2个CSV列的组合。有谁知道我将如何实现这一目标?我正在使用elasticsearch-spark库来创建索引。以下是示例代码。

SparkSession sparkSession = SparkSession.builder().config(config).getOrCreate();
SQLContext ctx = sparkSession.sqlContext();
HashMap<String, String> options = new HashMap<String, String>();
options.put("header", "true");
options.put("path", "hdfs://localhost:9000/test");
Dataset df = ctx.read().format("com.databricks.spark.csv").options(options).load();
JavaEsSparkSQL.saveToEs(df, "spark/test", ImmutableMap.of("es.mapping.id", "Id"));

1 个答案:

答案 0 :(得分:0)

将Id值更改为复合键,然后将数据集保存到弹性搜索:

df.registerTempTable("tmp");
Dataset ds= spark.sql("select concat(Id,<another composite key column>) as Id ,<rest of the columns> from tmp");
JavaEsSparkSQL.saveToEs(df, "spark/test", ImmutableMap.of("es.mapping.id", "Id"));
相关问题