flink 批量压缩redis集群 sink
idea maven依赖
<dependency>
<groupId>org.apache.bahir</groupId>
<artifactId>flink-connector-redis_2.11</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.8.0</version>
</dependency>
import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction
import org.apache.flink.streaming.api.functions.sink.SinkFunction
import redis.clients.jedis.*
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPOutputStream
class RedisSink : RichSinkFunction<Data>() {
private lateinit var jedisCluster: JedisCluster
override fun open(parameters: Configuration) {
super.open(parameters)
val jedisPoolConf = JedisPoolConfig()
jedisPoolConf.maxTotal = 128// 最大连接数
jedisPoolConf.maxIdle = 50// 最大空闲连接数
jedisPoolConf.testOnBorrow = true // 当调用 borrow Object方法时,是否进行有效性检查
// 集群模式
val nodes = HashSet<HostAndPort>()
val hostAndPort1 = HostAndPort("h1", port)
val hostAndPort2 = HostAndPort("h2", port)
val hostAndPort3 = HostAndPort("h3", port)
nodes.add(hostAndPort1)
nodes.add(hostAndPort2)
nodes.add(hostAndPort3)
jedisCluster = JedisCluster(nodes, 100000, 100000, 2, "password", jedisPoolConf)
}
override fun invoke(value: Data, context: SinkFunction.Context<*>?) {
val key = "data" + value.x+ value.y
val outputBts = ByteArrayOutputStream()
val gzip = GZIPOutputStream(outputBts)
gzip.write(value.toByteArray())
gzip.flush()
gzip.finish()
jedisCluster.set(key.toByteArray(), outputBts.toByteArray())
jedisCluster.expire(key.toByteArray(),15552000)
}
override fun close() {
super.close()
jedisCluster.close()
}
}
原文地址:https://blog.csdn.net/m0_65850671/article/details/142356197
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!