Spark整合ElasticSearch

spark整合elasticsearch兩種方式

1.本身生成_id等元數據
2.使用ES默認生成html

引入對應依賴

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

生成元數據方式

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

import scala.collection.immutable
import scala.collection.mutable.ListBuffer
object Spark_ES_WithMeta {

  val buffer = new ListBuffer[Tuple2[String,immutable.Map[String,String]]]
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("Custmer_Statistics").setMaster("local[2]")
    conf.set("es.nodes","rmhadoop01,rmhadoop02,rmhadoop03");
    conf.set("es.port","9200");
    conf.set("es.index.auto.create", "true");
    val sc = new SparkContext(conf)
    //讀取本地文件

    val result = sc.textFile("C:/work/ideabench/SparkSQL/data/es/gd_py_corp_sharehd_info.txt")
      .map(_.split("\\t"))
      .foreach(d =>{
          if(PropertiesUtils.getStringByKey("gd_py_corp_sharehd_info").equals("one2many")){

            val map = Map("id"->d(0),
              "batch_seq_num"->d(1),
              "name"->d(2),
              "contributiveFund"->d(3),
              "contributivePercent"->d(4),
              "currency"->d(5),
              "contributiveDate"->d(6),
              "corp_basic_info_id"->d(7),
              "query_time"->d(8)
            )

            buffer.append((d(0),map))
            //buffer

          }else if(PropertiesUtils.getStringByKey("gd_py_corp_sharehd_info").equals("one2one")){
            //Map(d(1) ->gd_py_corp_sharehd_info(d(0), d(1), d(2), d(3), d(4), d(5), d(6), d(7), d(8)))
          }

      } )

    sc.makeRDD(buffer).saveToEsWithMeta("spark/guofei_gd_py_corp_sharehd_info")
  }



  /**
    * 使用模板類描述表元數據信息
    *
    */
  case class gd_py_corp_sharehd_info(id:String,batch_seq_num:String,
                                     name:String,contributiveFund:String,
                                     contributivePercent:String,currency:String,
                                     contributiveDate:String,corp_basic_info_id:String,
                                     query_time:String)

}

ES-UI界面node

ES.pngsql

使用ES默認元數據方式

import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark.sql._
object SparkSQL_ES {

  /**
    * 使用模板類描述表元數據信息
    * 
    */
  case class gd_py_corp_sharehd_info(id:String,batch_seq_num:String,
                                     name:String,contributiveFund:String,
                                     contributivePercent:String,currency:String,
                                     contributiveDate:String,corp_basic_info_id:String,
                                     query_time:String)

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("Custmer_Statistics").setMaster("local[2]")
    conf.set("es.nodes","192.168.20.128");
    conf.set("es.port","9200");
    conf.set("es.index.auto.create", "true");
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    //RDD隱式轉換成DataFrame
    import sqlContext.implicits._
    //讀取本地文件
    val gd_py_corp_sharehd_infoDF = sc.textFile("C:/work/ideabench/SparkSQL/data/es/gd_py_corp_sharehd_info.txt")
      .map(_.split("\\t"))
      .map(d => gd_py_corp_sharehd_info(d(0), d(1), d(2), d(3), d(4), d(5), d(6), d(7), d(8)))
      .toDF()

    //註冊表
    gd_py_corp_sharehd_infoDF.registerTempTable("gd_py_corp_sharehd_info")

    /**
      * 
      */
    val result = sqlContext
      .sql("select * from gd_py_corp_sharehd_info limit 10")
      .toDF()

    result.saveToEs("spark/gd_py_corp_sharehd_info")
  }

}

參考文章

官網:https://www.elastic.co/guide/en/elasticsearch/hadoop/current/spark.htmlapache



做者:MichaelFly
連接:https://www.jianshu.com/p/a5c669d0ceba
來源:簡書
簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。app

相關文章
相關標籤/搜索