spark查询elasticsearch

83 篇文章 7 订阅
17 篇文章 0 订阅

下载elasticsearch的spark相关包 
https://www.elastic.co/downloads/hadoop

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>helloScala</groupId>
    <artifactId>helloScala</artifactId>
    <version>1</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- http://mvnrepository.com/artifact/org.apache.spark/spark-mllib_2.10 -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-mllib_2.11</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- http://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-spark_2.10 -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch-spark_2.11</artifactId>
            <version>5.0.0-alpha3</version>
        </dependency>



    </dependencies>
</project>
import org.elasticsearch.spark._
import org.elasticsearch.spark.sql._
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.hadoop.conf.Configuration
import org.elasticsearch.hadoop.mr.EsInputFormat
import org.apache.hadoop.io.Text
import org.apache.hadoop.io.MapWritable

object EsMain {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("estest").setMaster("local[*]")

//  conf.set("es.resource", "nm_1606/news") //指定读取的索引名称
    conf.set("es.nodes", "127.0.0.1")
    conf.set("es.port","9200")
    conf.set("es.query", " {  \"query\": {    \"match_all\": {    }  }}")  //使用query字符串对结果进行过滤
    val sc = new SparkContext(conf) 
    val rdd = sc.esRDD("nm_1606/news") 
    rdd.foreach( line => println(line._1+"\n"+line._2) )
    println(rdd.count)

  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值