spark读取ES数据

时间:2024-03-29 14:30:47

maven pom依赖配置:

<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch-spark-13_2.10</artifactId>
  <version>6.1.2</version>
</dependency>


code:

import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark._

object ReadEs {
  def main(args: Array[String]){

    System.setProperty("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    System.setProperty("hadoop.home.dir", "C:\\hadoop-2.6.0-cdh5.11.0")
    val conf = new SparkConf().setAppName("READES").setMaster("local").set("es.nodes","172.18.10.34")

    val sc = new SparkContext(conf)


//    val query = """{"query" : { "match" : { "content" : "上海" }}}"""
//    val query = """{"query" : { "match" : { "_id" : 1 }}}"""
//    val RDD = sc.esRDD("tracy/fulltext",query)

   
val RDD = sc.esRDD("tracy/fulltext")

    RDD.collect().foreach(x=> (println(x._1,x._2)))

    sc.stop()

  }

}


执行测试:

spark读取ES数据