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)

相关推荐
说私域9 小时前
基于开源AI大模型与S2B2C模式的线下服务型门店增长策略研究——以AI智能名片与小程序源码技术为核心
大数据·人工智能·小程序·开源
V_HY147629 小时前
AI碰一碰发视频获客工具,系统开发逻辑详细解析
大数据·人工智能·新媒体运营·流量运营
遇码9 小时前
单机快速部署开源、免费的分布式任务调度系统——DolphinScheduler
大数据·运维·分布式·开源·定时任务·dolphin·scheduler
一个天蝎座 白勺 程序猿10 小时前
大数据(4.2)Hive核心操作实战指南:表创建、数据加载与分区/分桶设计深度解析
大数据·hive·hadoop
计算机毕设定制辅导-无忧学长11 小时前
TDengine 核心概念与时序数据模型深度解析(一)
大数据·时序数据库·tdengine
TDengine (老段)11 小时前
TDengine 中的命名与边界
大数据·数据库·物联网·oracle·时序数据库·tdengine·iotdb
凉白开33811 小时前
Scala基础知识
开发语言·后端·scala
不要不开心了11 小时前
Scala内容
开发语言·pytorch·flask·scala·dash
2401_8242568611 小时前
Scala的函数式编程
开发语言·后端·scala
Acrelhuang13 小时前
8.3MW屋顶光伏+光储协同:上海汽车变速器低碳工厂的能源革命-安科瑞黄安南
大数据·数据库·人工智能·物联网·数据库开发