Flink输出到Elasticsearch
Posted wddqy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flink输出到Elasticsearch相关的知识,希望对你有一定的参考价值。
1.代码
import java.util
import org.apache.flink.api.common.functions.RuntimeContext
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.http.HttpHost
import org.elasticsearch.client.Requests
//温度传感器读取样例类
case class SensorReading(id: String, timestamp: Long, temperature: Double)
object EsSinkTest {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
//source
val inputStream = env.readTextFile("sensor1.txt")
//transform
import org.apache.flink.api.scala._
val dataStream = inputStream.map(x => {
val arr = x.split(",")
SensorReading(arr(0).trim, arr(1).trim.toLong, arr(2).trim.toDouble)
})
//sink
val httpHosts = new util.ArrayList[HttpHost]()
httpHosts.add(new HttpHost("localhost", 9200))
//创建一个esSink的Builder
val esSinkBuilder = new ElasticsearchSink.Builder[SensorReading] (
httpHosts,
new ElasticsearchSinkFunction[SensorReading] {
override def process(t: SensorReading, runtimeContext: RuntimeContext, requestIndexer: RequestIndexer): Unit = {
print("saving data" + t)
//包装成一个Map或者JsonObject
val hashMap = new util.HashMap[String, String]()
hashMap.put("sensor_id", t.id)
hashMap.put("temperature", t.temperature.toString)
hashMap.put("timestamp", t.timestamp.toString)
//创建index request,准备发送数据
val indexRequest = Requests.indexRequest().index("sensor").`type`("readingData").source(hashMap)
//发送请求,写入数据
requestIndexer.add(indexRequest)
println("data saved successfully")
}
}
)
dataStream.addSink(esSinkBuilder.build())
env.execute("es sink test")
}
}
2.启动Elasticsearch
3.访问: 127.0.0.1:9200/sensor/_search?pretty ,结果如下
以上是关于Flink输出到Elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章
ElasticSearch实战(四十九)-Flink 大数据实时同步方案
Flink使用Flink实现索引数据到Elasticsearch
《从0到1学习Flink》—— Flink 写入数据到 ElasticSearch
Elasticsearch:Data pipeline: Kafka => Flink => Elasticsearch