Collectors - Demo3

原创转载请注明出处:http://agilestyle.iteye.com/blog/2425965

 

Outline

testPartitioningByWithPredicate();

testPartitionByWithPredicateAndCollector();

testReducingBinaryOperator();

testReducingBinaryOperatorAndIdentity();

testReducingBinaryOperatorAndIdentityAndFunction();

testSummarizingInt();

testSummarizingLong();

testSummarizingDouble();

 

SRC

package org.fool.java8.collector;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

public class CollectorsTest3 {

    private static List<Dish> menu = Arrays.asList(
            new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT),
            new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("french fries", true, 530, Dish.Type.OTHER),
            new Dish("rice", true, 350, Dish.Type.OTHER),
            new Dish("season fruit", true, 120, Dish.Type.OTHER),
            new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 300, Dish.Type.FISH),
            new Dish("salmon", false, 450, Dish.Type.FISH) );

    public static void main(String[] args) {
        testPartitioningByWithPredicate();
        testPartitionByWithPredicateAndCollector();
        testReducingBinaryOperator();
        testReducingBinaryOperatorAndIdentity();
        testReducingBinaryOperatorAndIdentityAndFunction();
        testSummarizingInt();
        testSummarizingLong();
        testSummarizingDouble();
    }

    private static void testPartitioningByWithPredicate() {
        Map<Boolean, List<Dish>> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian));
        Optional.of(collect).ifPresent(System.out::println);
    }

    private static void testPartitionByWithPredicateAndCollector() {
        Map<Boolean, Double> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.averagingInt(Dish::getCalories)));
        Optional.of(collect).ifPresent(System.out::println);
    }

    private static void testReducingBinaryOperator() {
        menu.stream().collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparingInt(Dish::getCalories))))
                .ifPresent(System.out::println);
    }

    private static void testReducingBinaryOperatorAndIdentity() {
        Integer collect = menu.stream().map(Dish::getCalories).collect(Collectors.reducing(0, (d1, d2) -> d1 + d2));
        System.out.println(collect);
    }

    private static void testReducingBinaryOperatorAndIdentityAndFunction() {
        Integer collect = menu.stream().collect(Collectors.reducing(0, Dish::getCalories, (d1, d2) -> d1 + d2));
        System.out.println(collect);
    }

    private static void testSummarizingInt() {
        Optional.of(menu.stream().collect(Collectors.summarizingInt(Dish::getCalories))).ifPresent(System.out::println);
    }

    private static void testSummarizingLong() {
        Optional.of(menu.stream().collect(Collectors.summarizingLong(Dish::getCalories))).ifPresent(System.out::println);
    }

    private static void testSummarizingDouble() {
        Optional.of(menu.stream().collect(Collectors.summarizingDouble(Dish::getCalories))).ifPresent(System.out::println);
    }

    public static class Dish {
        private String name;
        private boolean vegetarian;
        private int calories;
        private Type type;

        public Dish(String name, boolean vegetarian, int calories, Type type) {
            this.name = name;
            this.vegetarian = vegetarian;
            this.calories = calories;
            this.type = type;
        }

        public enum Type { MEAT, FISH, OTHER }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isVegetarian() {
            return vegetarian;
        }

        public void setVegetarian(boolean vegetarian) {
            this.vegetarian = vegetarian;
        }

        public int getCalories() {
            return calories;
        }

        public void setCalories(int calories) {
            this.calories = calories;
        }

        public Type getType() {
            return type;
        }

        public void setType(Type type) {
            this.type = type;
        }

        @Override
        public String toString() {
            return "Dish{" +
                    "name='" + name + '\'' +
                    ", vegetarian=" + vegetarian +
                    ", calories=" + calories +
                    ", type=" + type +
                    '}';
        }
    }

}

 Console Output

{false=[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}, Dish{name='beef', vegetarian=false, calories=700, type=MEAT}, Dish{name='chicken', vegetarian=false, calories=400, type=MEAT}, Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}], true=[Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}]}
{false=530.0, true=387.5}
Dish{name='pork', vegetarian=false, calories=800, type=MEAT}
4200
4200
IntSummaryStatistics{count=9, sum=4200, min=120, average=466.666667, max=800}
LongSummaryStatistics{count=9, sum=4200, min=120, average=466.666667, max=800}
DoubleSummaryStatistics{count=9, sum=4200.000000, min=120.000000, average=466.666667, max=800.000000}

 

 

 

以下是一个使用Java语言实现的Kafka分区重分配的Demo,供您参考: ```java public class KafkaPartitionReassignDemo { private static final String TOPIC_NAME = "test_topic"; private static final String BROKER_LIST = "localhost:9092"; private static final String GROUP_ID = "test_group"; public static void main(String[] args) { Properties props = new Properties(); props.put("bootstrap.servers", BROKER_LIST); props.put("group.id", GROUP_ID); props.put("enable.auto.commit", "false"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Collections.singletonList(TOPIC_NAME)); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); // 处理消息 // 获取当前消费者组的成员信息 Map<String, List<PartitionInfo>> currentAssignment = consumer.listTopics().entrySet().stream() .filter(e -> e.getKey().equals(TOPIC_NAME)) .flatMap(e -> e.getValue().stream()) .collect(Collectors.groupingBy(PartitionInfo::topic, Collectors.toList())); // 重新分配分区 Map<String, List<Integer>> newAssignment = new HashMap<>(); currentAssignment.forEach((topic, partitions) -> { List<Integer> partitionIds = partitions.stream() .map(PartitionInfo::partition) .collect(Collectors.toList()); newAssignment.put(topic, partitionIds); }); // 执行分区重分配 AdminClient adminClient = AdminClient.create(props); Map<String, KafkaFuture<Void>> futureMap = adminClient.alterConsumerGroupOffsets(GROUP_ID, newAssignment); futureMap.forEach((topic, future) -> { try { future.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }); } } } ``` 该Demo中,首先创建了一个Kafka消费者,然后在循环中轮询消息,并处理消息。在处理完消息后,获取当前消费者组的成员信息,然后重新分配分区并执行分区重分配。具体的分区重分配操作使用了Kafka的AdminClient API来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值