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)

相关推荐
莫叫石榴姐8 分钟前
数据科学与SQL:组距分组分析 | 区间分布问题
大数据·人工智能·sql·深度学习·算法·机器学习·数据挖掘
魔珐科技1 小时前
以3D数字人AI产品赋能教育培训人才发展,魔珐科技亮相AI+教育创新与人才发展大会
大数据·人工智能
上优2 小时前
uniapp 选择 省市区 省市 以及 回显
大数据·elasticsearch·uni-app
samLi06203 小时前
【更新】中国省级产业集聚测算数据及协调集聚指数数据(2000-2022年)
大数据
Mephisto.java3 小时前
【大数据学习 | Spark-Core】Spark提交及运行流程
大数据·学习·spark
EasyCVR4 小时前
私有化部署视频平台EasyCVR宇视设备视频平台如何构建视频联网平台及升级视频转码业务?
大数据·网络·音视频·h.265
hummhumm4 小时前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j
jwolf24 小时前
Elasticsearch向量搜索:从语义搜索到图搜图只有一步之遥
elasticsearch·搜索引擎·ai
科技象限5 小时前
电脑禁用U盘的四种简单方法(电脑怎么阻止u盘使用)
大数据·网络·电脑
你可以叫我仔哥呀5 小时前
ElasticSearch学习笔记三:基础操作(一)
笔记·学习·elasticsearch