use of org.apache.rocketmq.spark.TopicQueueId in project rocketmq-externals by apache.
the class RocketMqUtilsTest method testGetOffsets.
@Test
public void testGetOffsets() throws MQBrokerException, MQClientException, InterruptedException, UnsupportedEncodingException {
Map<String, String> optionParams = new HashMap<>();
optionParams.put(RocketMQConfig.NAME_SERVER_ADDR, NAME_SERVER);
SparkConf sparkConf = new SparkConf().setAppName("JavaCustomReceiver").setMaster("local[*]");
JavaStreamingContext sc = new JavaStreamingContext(sparkConf, new Duration(1000));
List<String> topics = new ArrayList<>();
topics.add(TOPIC_DEFAULT);
LocationStrategy locationStrategy = LocationStrategy.PreferConsistent();
JavaInputDStream<MessageExt> dStream = RocketMqUtils.createJavaMQPullStream(sc, UUID.randomUUID().toString(), topics, ConsumerStrategy.earliest(), false, false, false, locationStrategy, optionParams);
// hold a reference to the current offset ranges, so it can be used downstream
final AtomicReference<Map<TopicQueueId, OffsetRange[]>> offsetRanges = new AtomicReference<>();
final Set<MessageExt> result = Collections.synchronizedSet(new HashSet<MessageExt>());
dStream.transform(new Function<JavaRDD<MessageExt>, JavaRDD<MessageExt>>() {
@Override
public JavaRDD<MessageExt> call(JavaRDD<MessageExt> v1) throws Exception {
Map<TopicQueueId, OffsetRange[]> offsets = ((HasOffsetRanges) v1.rdd()).offsetRanges();
offsetRanges.set(offsets);
return v1;
}
}).foreachRDD(new VoidFunction<JavaRDD<MessageExt>>() {
@Override
public void call(JavaRDD<MessageExt> messageExtJavaRDD) throws Exception {
result.addAll(messageExtJavaRDD.collect());
}
});
sc.start();
long startTime = System.currentTimeMillis();
boolean matches = false;
while (!matches && System.currentTimeMillis() - startTime < 10000) {
matches = MESSAGE_NUM == result.size();
Thread.sleep(50);
}
sc.stop();
}
Aggregations