flink:自定义数据分区

shuffle随机地将数据分配到下游的子任务。

rebalance用round robbin模式将数据分配到下游的子任务。

global把所有的数据都分配到一个分区。

partitionCustom: 自定义数据分区。

package cn.edu.tju.demo;

import org.apache.flink.api.common.functions.;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.collector.selector.OutputSelector;
import org.apache.flink.streaming.api.datastream.
;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import org.apache.flink.streaming.api.functions.co.CoMapFunction;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

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

import org.apache.flink.util.Collector;

import org.apache.kafka.clients.consumer.ConsumerConfig;

import org.apache.kafka.clients.producer.ProducerConfig;

import org.apache.kafka.common.serialization.StringDeserializer;

import org.apache.kafka.common.serialization.StringSerializer;

import java.util.*;

public class Test12 {

public static void main(String\[\] args) throws Exception {

StreamExecutionEnvironment environment = StreamExecutionEnvironment

.getExecutionEnvironment();

复制代码
    DataStreamSource<String> mySource = environment.addSource(new MySourceFunction());
    SingleOutputStreamOperator<Tuple2<String, Integer>> mapStream = mySource.map(new MapFunction<String, Tuple2<String, Integer>>() {
        @Override
        public Tuple2<String, Integer> map(String value) throws Exception {
            return new Tuple2<>(value, new Random().nextInt(10));
        }
    });

    DataStream<Tuple2<String, Integer>> resultStream = mapStream.partitionCustom(new MyPartitioner(), 1);


    resultStream.print();

    environment.execute("my job");

}

public static class MyPartitioner implements Partitioner<Integer>{

    @Override
    public int partition(Integer key, int partitions) {
        return key % partitions;
    }
}



public static class MySourceFunction implements SourceFunction<String> {
    private boolean runningFlag = true;
    @Override
    public void run(SourceContext<String> ctx) throws Exception {
        while (runningFlag){
            ctx.collect("hi world");
            ctx.collect("hello world");
            Thread.sleep(30000);
        }
    }

    @Override
    public void cancel() {
        runningFlag = false;
    }
}

}

相关推荐
枯木◊靠推文躺平版1 小时前
2026 企业级 AI 办公平台选型指南:如何评估可落地的办公 AI 能力
大数据·人工智能
weidian6661 小时前
小红书广告投放形式和小红书广告投放核心流程
大数据
龙亘川2 小时前
数智驱动民政升级 精准守护民生保障
大数据·数据库·人工智能
小蒋观天下3 小时前
端侧大模型在安防摄像头部署实操(上)|行业痛点、架构选型、落地思路解析
大数据·人工智能·安全·计算机视觉·ai大模型
Elastic 中国社区官方博客3 小时前
使用 Lucene 搜索你的 Bean — 索引
java·大数据·数据库·elasticsearch·搜索引擎·全文检索·lucene
马六六i5 小时前
市面上正规的IP驱动产业新场景新工具有哪些
大数据·网络·python·tcp/ip
AI副业教程5 小时前
加航 Aeroplan 定向活动:加拿大—欧洲高级舱额外 SQC 资讯整理(2026 秋季)
大数据
隔振降噪研究员6 小时前
振动筛振动治理科普
大数据·人工智能
阿部多瑞 ABU6 小时前
泛二次元问题舆论战与问题诸论
大数据·人工智能·ai写作
码云数智-园园7 小时前
码云数智、小鹅通、创客匠人知识付费小程序平台对比
大数据