从文件读取数据,保存到ElasticSearch,使用flink框架


package com.bigdata.flink.Flink_Sink

import java.util
import java.util._

import com.bigdata.flink.Tranform_Operator.SensorReading
import org.apache.flink.api.common.functions.RuntimeContext
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.api.scala._
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.connectors.elasticsearch.{ElasticsearchSinkFunction, RequestIndexer}
import org.apache.flink.streaming.connectors.elasticsearch6.ElasticsearchSink
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011
import org.apache.http.HttpHost
import org.elasticsearch.client.Requests


/**
  *
  * 将 flink 处理过的 数据 保存到 Elastic Search
  */
object ESSinkClusterDataFromFile {
  def main(args: Array[String]): Unit = {

    val env = StreamExecutionEnvironment.getExecutionEnvironment
    // 如果不设,默认为本机核数的并行度,即读取数据安顺序一条条读取
    env.setParallelism(1)

    // 1、读取数据,从文件读取
    val inputStream = env.readTextFile("E:\\Project\\flinkProject\\data.txt")

    // 3、转换操作
    val dataStream = inputStream.map(data => {
      val daraArray = data.split(",")
      val sensorId = daraArray(0).trim
      val timeStamp = daraArray(1).trim.toLong
      val tempperture = daraArray(2).trim.toDouble

      //  包装成样例类
      SensorReading(sensorId, timeStamp, tempperture)
    })


    // 4、从文件读取数据,sink 到 ES
    // ES 配置
    val httpHosts = new util.ArrayList[HttpHost]()
    httpHosts.add(new HttpHost("mini1", 9200))
    httpHosts.add(new HttpHost("mini2", 9200))
    httpHosts.add(new HttpHost("mini3", 9200))

    val esSinkBuilder = new ElasticsearchSink.Builder[SensorReading](httpHosts,
      new ElasticsearchSinkFunction[SensorReading] {

        override def process(t: SensorReading, runtimeContext: RuntimeContext, requestIndexer: RequestIndexer): Unit = {
          println("saving data " + t)

          // 1. 创建一个map 包装 jsonObjet
          val json = new util.HashMap[String, String]()
          json.put("sensor_id", t.id)
          json.put("temperature", t.tempPerture.toString)
          json.put("timeStamp", t.timeStamp.toString)

          // 2.创建index 索引,为 sensor
          val indexRequest = Requests.indexRequest()
            .index("sensor")
            .`type`("readingData")
            .source(json)

          // 3.利用index 发送请求
          requestIndexer.add(indexRequest)

          println("saved successfully")
        }
      })

    // 4、sink 到ES
    dataStream.addSink(esSinkBuilder.build())



    //    5 、启动流,不停止
    env.execute("ESSinkClusterDataFromFile ")

  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值