本文整理汇总了Java中org.apache.kafka.clients.consumer.OffsetResetStrategy类的典型用法代码示例。如果您正苦于以下问题:Java OffsetResetStrategy类的具体用法?Java OffsetResetStrategy怎么用?Java OffsetResetStrategy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OffsetResetStrategy类属于org.apache.kafka.clients.consumer包,在下文中一共展示了OffsetResetStrategy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setup
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Before
public void setup() {
this.time = new MockTime();
this.subscriptions = new SubscriptionState(OffsetResetStrategy.EARLIEST);
this.metadata = new Metadata(0, Long.MAX_VALUE, true);
this.metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
this.client = new MockClient(time, metadata);
this.consumerClient = new ConsumerNetworkClient(client, metadata, time, 100, 1000);
this.metrics = new Metrics(time);
this.rebalanceListener = new MockRebalanceListener();
this.mockOffsetCommitCallback = new MockCommitCallback();
this.partitionAssignor.clear();
client.setNode(node);
this.coordinator = buildCoordinator(metrics, assignors, ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, autoCommitEnabled, true);
}
示例2: shouldFallbackToPartitionsForIfPartitionNotInAllPartitionsList
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void shouldFallbackToPartitionsForIfPartitionNotInAllPartitionsList() throws Exception {
final MockConsumer<byte[], byte[]> consumer = new MockConsumer(OffsetResetStrategy.EARLIEST) {
@Override
public List<PartitionInfo> partitionsFor(final String topic) {
return Collections.singletonList(partitionInfo);
}
};
final StoreChangelogReader changelogReader = new StoreChangelogReader(consumer, new MockTime(), 10);
changelogReader.validatePartitionExists(topicPartition, "store");
}
示例3: shouldThrowStreamsExceptionIfTimeoutOccursDuringPartitionsFor
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void shouldThrowStreamsExceptionIfTimeoutOccursDuringPartitionsFor() throws Exception {
final MockConsumer<byte[], byte[]> consumer = new MockConsumer(OffsetResetStrategy.EARLIEST) {
@Override
public List<PartitionInfo> partitionsFor(final String topic) {
throw new TimeoutException("KABOOM!");
}
};
final StoreChangelogReader changelogReader = new StoreChangelogReader(consumer, new MockTime(), 5);
try {
changelogReader.validatePartitionExists(topicPartition, "store");
fail("Should have thrown streams exception");
} catch (final StreamsException e) {
// pass
}
}
示例4: shouldRequestPartitionInfoIfItDoesntExist
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void shouldRequestPartitionInfoIfItDoesntExist() throws Exception {
final MockConsumer<byte[], byte[]> consumer = new MockConsumer(OffsetResetStrategy.EARLIEST) {
@Override
public Map<String, List<PartitionInfo>> listTopics() {
return Collections.emptyMap();
}
};
consumer.updatePartitions(topicPartition.topic(), Collections.singletonList(partitionInfo));
final StoreChangelogReader changelogReader = new StoreChangelogReader(consumer, Time.SYSTEM, 5000);
changelogReader.validatePartitionExists(topicPartition, "store");
}
示例5: before
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Before
public void before() throws IOException {
final Map<String, String> storeToTopic = new HashMap<>();
storeToTopic.put("t1-store", "t1");
storeToTopic.put("t2-store", "t2");
final Map<StateStore, ProcessorNode> storeToProcessorNode = new HashMap<>();
store1 = new NoOpReadOnlyStore<>("t1-store");
storeToProcessorNode.put(store1, new MockProcessorNode(-1));
store2 = new NoOpReadOnlyStore("t2-store");
storeToProcessorNode.put(store2, new MockProcessorNode(-1));
topology = new ProcessorTopology(Collections.<ProcessorNode>emptyList(),
Collections.<String, SourceNode>emptyMap(),
Collections.<String, SinkNode>emptyMap(),
Collections.<StateStore>emptyList(),
storeToTopic,
Arrays.<StateStore>asList(store1, store2));
context = new NoOpProcessorContext();
stateDirPath = TestUtils.tempDirectory().getPath();
stateDirectory = new StateDirectory("appId", stateDirPath, time);
consumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
stateManager = new GlobalStateManagerImpl(topology, consumer, stateDirectory);
checkpointFile = new File(stateManager.baseDir(), ProcessorStateManager.CHECKPOINT_FILE_NAME);
}
示例6: shouldThrowStreamsExceptionOnStartupIfExceptionOccurred
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void shouldThrowStreamsExceptionOnStartupIfExceptionOccurred() throws Exception {
final MockConsumer<byte[], byte[]> mockConsumer = new MockConsumer(OffsetResetStrategy.EARLIEST) {
@Override
public List<PartitionInfo> partitionsFor(final String topic) {
throw new RuntimeException("KABOOM!");
}
};
globalStreamThread = new GlobalStreamThread(builder.buildGlobalStateTopology(),
config,
mockConsumer,
new StateDirectory("appId", TestUtils.tempDirectory().getPath(), time),
new Metrics(),
new MockTime(),
"clientId");
try {
globalStreamThread.start();
fail("Should have thrown StreamsException if start up failed");
} catch (StreamsException e) {
assertThat(e.getCause(), instanceOf(RuntimeException.class));
assertThat(e.getCause().getMessage(), equalTo("KABOOM!"));
}
assertFalse(globalStreamThread.stillRunning());
}
示例7: testListOffsetsSendsIsolationLevel
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Test
public void testListOffsetsSendsIsolationLevel() {
for (final IsolationLevel isolationLevel : IsolationLevel.values()) {
Fetcher<byte[], byte[]> fetcher = createFetcher(subscriptions, new Metrics(), new ByteArrayDeserializer(),
new ByteArrayDeserializer(), Integer.MAX_VALUE, isolationLevel);
subscriptions.assignFromUser(singleton(tp1));
subscriptions.needOffsetReset(tp1, OffsetResetStrategy.LATEST);
client.prepareResponse(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
ListOffsetRequest request = (ListOffsetRequest) body;
return request.isolationLevel() == isolationLevel;
}
}, listOffsetResponse(Errors.NONE, 1L, 5L));
fetcher.updateFetchPositions(singleton(tp1));
assertFalse(subscriptions.isOffsetResetNeeded(tp1));
assertTrue(subscriptions.isFetchable(tp1));
assertEquals(5, subscriptions.position(tp1).longValue());
}
}
示例8: testUpdateFetchPositionDisconnect
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Test
public void testUpdateFetchPositionDisconnect() {
subscriptions.assignFromUser(singleton(tp1));
subscriptions.needOffsetReset(tp1, OffsetResetStrategy.LATEST);
// First request gets a disconnect
client.prepareResponse(listOffsetRequestMatcher(ListOffsetRequest.LATEST_TIMESTAMP),
listOffsetResponse(Errors.NONE, 1L, 5L), true);
// Next one succeeds
client.prepareResponse(listOffsetRequestMatcher(ListOffsetRequest.LATEST_TIMESTAMP),
listOffsetResponse(Errors.NONE, 1L, 5L));
fetcher.updateFetchPositions(singleton(tp1));
assertFalse(subscriptions.isOffsetResetNeeded(tp1));
assertTrue(subscriptions.isFetchable(tp1));
assertEquals(5, subscriptions.position(tp1).longValue());
}
示例9: testUpdateFetchPositionOfPausedPartitionsRequiringOffsetReset
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Test
public void testUpdateFetchPositionOfPausedPartitionsRequiringOffsetReset() {
subscriptions.assignFromUser(singleton(tp1));
subscriptions.committed(tp1, new OffsetAndMetadata(0));
subscriptions.pause(tp1); // paused partition does not have a valid position
subscriptions.needOffsetReset(tp1, OffsetResetStrategy.LATEST);
client.prepareResponse(listOffsetRequestMatcher(ListOffsetRequest.LATEST_TIMESTAMP),
listOffsetResponse(Errors.NONE, 1L, 10L));
fetcher.updateFetchPositions(singleton(tp1));
assertFalse(subscriptions.isOffsetResetNeeded(tp1));
assertFalse(subscriptions.isFetchable(tp1)); // because tp is paused
assertTrue(subscriptions.hasValidPosition(tp1));
assertEquals(10, subscriptions.position(tp1).longValue());
}
示例10: getConsumerProperties
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
public Properties getConsumerProperties(String groupId, String clientId, OffsetResetStrategy autoOffsetReset) {
if (groupId == null) {
throw new IllegalArgumentException("The groupId is required");
} else {
Properties props = new Properties();
props.setProperty("bootstrap.servers", brokers);
props.setProperty("group.id", groupId);
props.setProperty("enable.auto.commit", Boolean.FALSE.toString());
if (autoOffsetReset != null) {
props.setProperty("auto.offset.reset",
autoOffsetReset.toString().toLowerCase());
}
if (clientId != null) {
props.setProperty("client.id", clientId);
}
return props;
}
}
示例11: resetOffset
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
/**
* Reset offsets for the given partition using the offset reset strategy.
*
* @param partition The given partition that needs reset offset
* @throws org.apache.kafka.clients.consumer.NoOffsetForPartitionException If no offset reset strategy is defined
*/
private void resetOffset(TopicPartition partition) {
OffsetResetStrategy strategy = subscriptions.resetStrategy(partition);
final long timestamp;
if (strategy == OffsetResetStrategy.EARLIEST)
timestamp = ListOffsetRequest.EARLIEST_TIMESTAMP;
else if (strategy == OffsetResetStrategy.LATEST)
timestamp = ListOffsetRequest.LATEST_TIMESTAMP;
else
throw new NoOffsetForPartitionException(partition);
log.debug("Resetting offset for partition {} to {} offset.", partition, strategy.name().toLowerCase(Locale.ROOT));
long offset = listOffset(partition, timestamp);
// we might lose the assignment while fetching the offset, so check it is still active
if (subscriptions.isAssigned(partition))
this.subscriptions.seek(partition, offset);
}
示例12: setup
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Before
public void setup() {
this.time = new MockTime();
this.client = new MockClient(time);
this.subscriptions = new SubscriptionState(OffsetResetStrategy.EARLIEST);
this.metadata = new Metadata(0, Long.MAX_VALUE);
this.metadata.update(cluster, time.milliseconds());
this.consumerClient = new ConsumerNetworkClient(client, metadata, time, 100, 1000);
this.metrics = new Metrics(time);
this.rebalanceListener = new MockRebalanceListener();
this.defaultOffsetCommitCallback = new MockCommitCallback();
this.partitionAssignor.clear();
client.setNode(node);
this.coordinator = buildCoordinator(metrics, assignors, ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, autoCommitEnabled);
}
示例13: testUpdateFetchPositionDisconnect
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Test
public void testUpdateFetchPositionDisconnect() {
subscriptions.assignFromUser(Arrays.asList(tp));
subscriptions.needOffsetReset(tp, OffsetResetStrategy.LATEST);
// First request gets a disconnect
client.prepareResponse(listOffsetRequestMatcher(ListOffsetRequest.LATEST_TIMESTAMP),
listOffsetResponse(Errors.NONE, Arrays.asList(5L)), true);
// Next one succeeds
client.prepareResponse(listOffsetRequestMatcher(ListOffsetRequest.LATEST_TIMESTAMP),
listOffsetResponse(Errors.NONE, Arrays.asList(5L)));
fetcher.updateFetchPositions(Collections.singleton(tp));
assertFalse(subscriptions.isOffsetResetNeeded(tp));
assertTrue(subscriptions.isFetchable(tp));
assertEquals(5, (long) subscriptions.position(tp));
}
示例14: testConsume
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Test
public void testConsume(TestContext ctx) throws Exception {
final String topicName = "testConsume";
String consumerId = topicName;
Async batch = ctx.async();
AtomicInteger index = new AtomicInteger();
int numMessages = 1000;
kafkaCluster.useTo().produceStrings(numMessages, batch::complete, () ->
new ProducerRecord<>(topicName, 0, "key-" + index.get(), "value-" + index.getAndIncrement()));
batch.awaitSuccess(20000);
Properties config = kafkaCluster.useTo().getConsumerProperties(consumerId, consumerId, OffsetResetStrategy.EARLIEST);
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumer = createConsumer(vertx, config);
Async done = ctx.async();
AtomicInteger count = new AtomicInteger(numMessages);
consumer.exceptionHandler(ctx::fail);
consumer.handler(rec -> {
if (count.decrementAndGet() == 0) {
done.complete();
}
});
consumer.subscribe(Collections.singleton(topicName));
}
示例15: testPartitionsFor
import org.apache.kafka.clients.consumer.OffsetResetStrategy; //导入依赖的package包/类
@Test
public void testPartitionsFor(TestContext ctx) throws Exception {
String topicName = "testPartitionsFor";
String consumerId = topicName;
kafkaCluster.createTopic(topicName, 2, 1);
Properties config = kafkaCluster.useTo().getConsumerProperties(consumerId, consumerId, OffsetResetStrategy.EARLIEST);
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
Context context = vertx.getOrCreateContext();
consumer = createConsumer(context, config);
Async done = ctx.async();
consumer.partitionsFor(topicName, ar -> {
if (ar.succeeded()) {
List<PartitionInfo> partitionInfo = ar.result();
ctx.assertEquals(2, partitionInfo.size());
} else {
ctx.fail();
}
done.complete();
});
}