flink 例子(scala)

import org.apache.flink.api.common.functions.RuntimeContext

import org.apache.flink.api.common.serialization.SimpleStringSchema

import org.apache.flink.api.java.utils.ParameterTool

import org.apache.flink.api.scala._

import org.apache.flink.runtime.state.filesystem.FsStateBackend

import org.apache.flink.streaming.api.TimeCharacteristic

import org.apache.flink.streaming.api.scala.DataStream

import org.apache.flink.streaming.connectors.elasticsearch.{ElasticsearchSinkFunction, RequestIndexer}

import org.apache.flink.streaming.connectors.elasticsearch7.ElasticsearchSink

import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

import org.elasticsearch.action.DocWriteRequest

import org.elasticsearch.action.index.IndexRequest

import org.elasticsearch.client.Requests

object demo{

def main(args: Array[String]): Unit = {

val env = StreamExecutionEnvironment.getExecutionEnvironment

//需要状态开启下面的配置

//env.setStateBackend(new RocksDBStateBackend(s"hdfs://${namenodeID}", true))//hdfs 作为状态后端

//env.enableCheckpointing(10 * 60 * 1000L)

//env.getCheckpointConfig.setCheckpointTimeout(10 * 60 * 1000L)

env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime) //处理时间

val props = new Properties

props.setProperty("bootstrap.servers", "host:6667")//有些是9092端口

props.setProperty("group.id", "groupId")

props.setProperty("retries", "10")

props.setProperty("retries.backoff.ms", "100")

props.put(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG, "60000")

//是否配置了权限,有的话加上下面的配置

// props.setProperty("sasl.jaas.config","org.apache.kafka.common.security.plain.PlainLoginModule required username='' password='';")

//props.setProperty("security.protocol", "SASL_PLAINTEXT");

// props.setProperty("sasl.mechanism", "PLAIN")

val myConsumer = new FlinkKafkaConsumer[String]("topicName", new SimpleStringSchema(), props)

.setStartFromEarliest()//从什么时间开始读

val stream = env.addSource(myConsumer)

.map(m => {

val list= m.split("\t")

val id = list(1)

val ts = list(2)

Demo(id,ts)

})

val httpHosts = CP.getESConf

val esSinkBuilder = new ElasticsearchSink.Builder[Demo](

httpHosts,

new ElasticsearchSinkFunction[Demo] {

def process(element: Demo, ctx: RuntimeContext, indexer: RequestIndexer) {

val json = new java.util.HashMap[String, String]

json.put("@timestamp", element.ts)

json.put("id", element.id)

val rqst: IndexRequest = Requests.indexRequest

//.id("自定义id,不加会自动生成")

.id(element.id)

.index("indexName")

.source(json)

.opType(DocWriteRequest.OpType.INDEX)

indexer.add(rqst)

}

}

)

setESConf(esSinkBuilder, 50000)

stream.addSink(esSinkBuilder.build())

.uid("write-to-es")

.name("write-to-es")

env.execute(s"demo")

}

def setESConf[T](esSinkBuilder: ElasticsearchSink.Builder[T], numMaxActions: Int) {

esSinkBuilder.setBulkFlushMaxActions(numMaxActions)

esSinkBuilder.setBulkFlushMaxSizeMb(10)

esSinkBuilder.setBulkFlushInterval(10000)

esSinkBuilder.setBulkFlushBackoff(true)

esSinkBuilder.setBulkFlushBackoffDelay(2)

esSinkBuilder.setBulkFlushBackoffRetries(3)

esSinkBuilder.setRestClientFactory(new RestClientFactory {

override def configureRestClientBuilder(restClientBuilder: RestClientBuilder): Unit = {

restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {

override def customizeRequestConfig(requestConfigBuilder: RequestConfig.Builder): RequestConfig.Builder = {

requestConfigBuilder.setConnectTimeout(12000)

requestConfigBuilder.setSocketTimeout(90000)

}

})

}

})

}

}

case class Demo(id: String, ts: String)

相关推荐
W Y13 分钟前
【架构-37】Spark和Flink
架构·flink·spark
ycsdn1015 分钟前
Caused by: org.apache.flink.api.common.io.ParseException: Row too short:
大数据·flink
DolphinScheduler社区2 小时前
Apache DolphinScheduler + OceanBase,搭建分布式大数据调度平台的实践
大数据
时差9533 小时前
MapReduce 的 Shuffle 过程
大数据·mapreduce
kakwooi4 小时前
Hadoop---MapReduce(3)
大数据·hadoop·mapreduce
数新网络4 小时前
《深入浅出Apache Spark》系列②:Spark SQL原理精髓全解析
大数据·sql·spark
昨天今天明天好多天9 小时前
【数据仓库】
大数据
油头少年_w9 小时前
大数据导论及分布式存储HadoopHDFS入门
大数据·hadoop·hdfs
Elastic 中国社区官方博客10 小时前
释放专利力量:Patently 如何利用向量搜索和 NLP 简化协作
大数据·数据库·人工智能·elasticsearch·搜索引擎·自然语言处理
力姆泰克10 小时前
看电动缸是如何提高农机的自动化水平
大数据·运维·服务器·数据库·人工智能·自动化·1024程序员节