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()
    }

}
相关推荐
reddingtons37 分钟前
Adobe Firefly AI驱动设计:实用技巧与创新思维路径
大数据·人工智能·adobe·illustrator·photoshop·premiere·indesign
MonkeyKing_sunyuhua2 小时前
Ehcache、Caffeine、Spring Cache、Redis、J2Cache、Memcached 和 Guava Cache 的主要区别
redis·spring·memcached
G皮T2 小时前
【Elasticsearch】全文检索 & 组合检索
大数据·elasticsearch·搜索引擎·全文检索·match·query·组合检索
Cachel wood9 天前
Spark教程6:Spark 底层执行原理详解
大数据·数据库·分布式·计算机网络·spark
Sally璐璐9 天前
数据标注工具详解
大数据·ai
笨手笨脚の9 天前
Redis 源码分析-Redis 中的事件驱动
数据库·redis·缓存·select·nio·epoll·io模型
(:满天星:)9 天前
Redis哨兵模式深度解析与实战部署
linux·服务器·网络·数据库·redis·缓存·centos
expect7g9 天前
新时代多流Join的一个思路----Partial Update
后端·flink
云宏信息9 天前
金融vmware替换过程中关于利旧纳管、迁移、数据安全容灾备份、成本及案例|金融行业数字化QA合集④
大数据·运维·服务器·科技·金融·云计算
weixin_438335409 天前
Spring Boot:运用Redis统计用户在线数量
java·spring boot·redis