Flink中的数据重分区操作

示例1:
public class TransformTest6_Partition {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        env.setParallelism(1);
        // 从文件中读取数据
        DataStream<String> inputStream = env.readTextFile("/Users/sundongping/IdeaProjects/FirstFlinkTest/src/main/resources/sensor.txt");
        inputStream.print("input");

        // 转换成SensorReading类型
        // java8 中的lamda表达式
        DataStream<SensorReading> dataStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });

        //1、shuffle
        DataStream<String>  shuffleStream = inputStream.shuffle();
        shuffleStream.print("shuffle");

        //2、keyBy
        dataStream.keyBy("id").print("keyBy");

        env.execute();
    }
}

运行结果:

input:3> sensor_1,154777324345,35.8
input:3> sensor_2,154777324346,15.4
input:3> sensor_3,154777324347,6.7
shuffle:2> sensor_2,154777324346,15.4
shuffle:2> sensor_3,154777324347,6.7
keyBy:1> SensorReading{id='sensor_2', timestamp=154777324346, temperature=15.4}
shuffle:1> sensor_1,154777324345,35.8
keyBy:1> SensorReading{id='sensor_3', timestamp=154777324347, temperature=6.7}
keyBy:3> SensorReading{id='sensor_1', timestamp=154777324345, temperature=35.8}
input:4> sensor_4,154777324348,38.1
input:4> sensor_1,154777324349,36.9
input:1> sensor_1,154777324350,32.9
shuffle:2> sensor_1,154777324349,36.9
input:1> sensor_1,154777324351,37.1
shuffle:1> sensor_4,154777324348,38.1
keyBy:3> SensorReading{id='sensor_1', timestamp=154777324349, temperature=36.9}
keyBy:4> SensorReading{id='sensor_4', timestamp=154777324348, temperature=38.1}
shuffle:1> sensor_1,154777324350,32.9
shuffle:2> sensor_1,154777324351,37.1
input:2> sensor_6,154777324351,15.4
input:2> sensor_7,154777324350,6.7
keyBy:3> SensorReading{id='sensor_1', timestamp=154777324350, temperature=32.9}
keyBy:3> SensorReading{id='sensor_1', timestamp=154777324351, temperature=37.1}
shuffle:1> sensor_6,154777324351,15.4
shuffle:2> sensor_7,154777324350,6.7
keyBy:3> SensorReading{id='sensor_6', timestamp=154777324351, temperature=15.4}
keyBy:4> SensorReading{id='sensor_7', timestamp=154777324350, temperature=6.7}
示例2:
public class TransformTest6_Partition {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        env.setParallelism(1);
        // 从文件中读取数据
        DataStream<String> inputStream = env.readTextFile("/Users/sundongping/IdeaProjects/FirstFlinkTest/src/main/resources/sensor.txt");
        inputStream.print("input");

        // 转换成SensorReading类型
        // java8 中的lamda表达式
        DataStream<SensorReading> dataStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });

//        //1、shuffle
//        DataStream<String>  shuffleStream = inputStream.shuffle();
//        shuffleStream.print("shuffle");
//
//        //2、keyBy
//        dataStream.keyBy("id").print("keyBy");

        //3、global
        dataStream.global().print("global");
        env.execute();
    }
}

运行结果:

input:4> sensor_1,154777324345,35.8
input:2> sensor_1,154777324350,32.9
input:2> sensor_1,154777324351,37.1
input:3> sensor_6,154777324351,15.4
input:3> sensor_7,154777324350,6.7
global:1> SensorReading{id='sensor_6', timestamp=154777324351, temperature=15.4}
global:1> SensorReading{id='sensor_7', timestamp=154777324350, temperature=6.7}
input:1> sensor_4,154777324348,38.1
input:1> sensor_1,154777324349,36.9
global:1> SensorReading{id='sensor_4', timestamp=154777324348, temperature=38.1}
global:1> SensorReading{id='sensor_1', timestamp=154777324349, temperature=36.9}
input:4> sensor_2,154777324346,15.4
input:4> sensor_3,154777324347,6.7
global:1> SensorReading{id='sensor_1', timestamp=154777324345, temperature=35.8}
global:1> SensorReading{id='sensor_2', timestamp=154777324346, temperature=15.4}
global:1> SensorReading{id='sensor_3', timestamp=154777324347, temperature=6.7}
global:1> SensorReading{id='sensor_1', timestamp=154777324350, temperature=32.9}
global:1> SensorReading{id='sensor_1', timestamp=154777324351, temperature=37.1}
Flink数据倾斜是指在进行并行计算时,某些任务所处理的数据量远远大于其他任务处理的数据量,导致处理速度变慢,甚至引起任务失败。数据倾斜问题是分布式计算常见的问题,而Flink提供了一些解决方案来应对这个问题。 以下是一些解决方案: 1. 手动分区 手动分区是最常见的解决方案之一。通过将数据进行分区,使得处理数据量较大的任务被分配到多个任务,从而避免数据倾斜的问题。具体操作是,对于数据倾斜的key,可以将其分散到不同的分区,使得每个分区数据量都不会过大。这种方法需要对数据进行分区,因此需要考虑分区的数量和分区的均衡性。 2. 增加并行度 增加并行度也是一种解决数据倾斜的方案。当一个任务的数据量过大时,可以通过增加任务的并行度来分散数据的处理,从而避免数据倾斜的问题。增加并行度可以通过增加TaskManager的数量或者使用更多的slot来实现。 3. 采用随机key 如果数据倾斜的原因是某些key的数据量过大,可以采用随机key的方式来解决问题。具体操作是,将原来的key进行替换,使用随机数作为新的key,从而实现数据的均衡分布。这种方法需要考虑随机key的生成方式和分配方式,以保证数据的正确性和处理效率。 4. 使用分桶技术 分桶技术是一种常用的解决数据倾斜问题的方案。具体操作是,将数据按照一定的规则分到不同的桶,从而实现数据的均衡分布。Flink提供了BucketingSink等相关的API来实现数据的分桶操作。 5. 基于动态负载均衡的解决方案 基于动态负载均衡的解决方案是一种相对较新的解决方案。该方案通过监控任务的处理情况,动态地调整任务的分配策略,从而实现数据的均衡分布。具体实现可以采用Flink提供的TaskExecutor的动态调整功能,也可以使用第三方的负载均衡方案。 以上是Flink解决数据倾斜问题的一些方案,具体方法需要根据实际情况进行选择和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值