當前位置: 首頁>>代碼示例>>Java>>正文


Java TopicPartition類代碼示例

本文整理匯總了Java中io.vertx.kafka.client.common.TopicPartition的典型用法代碼示例。如果您正苦於以下問題:Java TopicPartition類的具體用法?Java TopicPartition怎麽用?Java TopicPartition使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


TopicPartition類屬於io.vertx.kafka.client.common包,在下文中一共展示了TopicPartition類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: exampleConsumerAssignPartition

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
/**
 * Example about how Kafka consumer receives messages
 * from a topic requesting a specific partition for that
 * @param consumer
 */
public void exampleConsumerAssignPartition(KafkaConsumer<String, String> consumer) {

  // register the handler for incoming messages
  consumer.handler(record -> {
    System.out.println("key=" + record.key() + ",value=" + record.value() +
      ",partition=" + record.partition() + ",offset=" + record.offset());
  });

  //
  Set<TopicPartition> topicPartitions = new HashSet<>();
  topicPartitions.add(new TopicPartition()
    .setTopic("test")
    .setPartition(0));

  // requesting to be assigned the specific partition
  consumer.assign(topicPartitions, done -> {

    if (done.succeeded()) {
      System.out.println("Partition assigned");

      // requesting the assigned partitions
      consumer.assignment(done1 -> {

        if (done1.succeeded()) {

          for (TopicPartition topicPartition : done1.result()) {
            System.out.println(topicPartition.getTopic() + " " + topicPartition.getPartition());
          }
        }
      });
    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:39,代碼來源:VertxKafkaClientExamples.java

示例2: exampleSeek

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
public void exampleSeek(KafkaConsumer<String, String> consumer) {

    TopicPartition topicPartition = new TopicPartition()
      .setTopic("test")
      .setPartition(0);

    // seek to a specific offset
    consumer.seek(topicPartition, 10, done -> {

      if (done.succeeded()) {
        System.out.println("Seeking done");
      }
    });

  }
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:16,代碼來源:VertxKafkaClientExamples.java

示例3: exampleSeekToBeginning

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
public void exampleSeekToBeginning(KafkaConsumer<String, String> consumer) {

    TopicPartition topicPartition = new TopicPartition()
      .setTopic("test")
      .setPartition(0);

    // seek to the beginning of the partition
    consumer.seekToBeginning(Collections.singleton(topicPartition), done -> {

      if (done.succeeded()) {
        System.out.println("Seeking done");
      }
    });
  }
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:15,代碼來源:VertxKafkaClientExamples.java

示例4: exampleSeekToEnd

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
/**
 * Example about how Kafka consumer can seek in the partition
 * changing the offset from which starting to read messages
 * @param consumer
 */
public void exampleSeekToEnd(KafkaConsumer<String, String> consumer) {

  TopicPartition topicPartition = new TopicPartition()
    .setTopic("test")
    .setPartition(0);

  // seek to the end of the partition
  consumer.seekToEnd(Collections.singleton(topicPartition), done -> {

    if (done.succeeded()) {
      System.out.println("Seeking done");
    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:20,代碼來源:VertxKafkaClientExamples.java

示例5: exampleConsumerOffsetsForTimes

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
/**
 * Example to demonstrate how one can use the new offsetsForTimes API (introduced with Kafka
 * 0.10.1.1) to look up an offset by timestamp, i.e. search parameter is an epoch timestamp and
 * the call returns the lowest offset with ingestion timestamp >= given timestamp.
 * @param consumer Consumer to be used
 */
public void exampleConsumerOffsetsForTimes(KafkaConsumer<String, String> consumer) {
  Map<TopicPartition, Long> topicPartitionsWithTimestamps = new HashMap<>();
  TopicPartition topicPartition = new TopicPartition().setTopic("test").setPartition(0);

  // We are interested in the offset for data ingested 60 seconds ago
  long timestamp = (System.currentTimeMillis() - 60000);

  topicPartitionsWithTimestamps.put(topicPartition, timestamp);
  consumer.offsetsForTimes(topicPartitionsWithTimestamps, done -> {
    if(done.succeeded()) {
      Map<TopicPartition, OffsetAndTimestamp> results = done.result();
      results.forEach((topic, offset) ->
        System.out.println("Offset for topic="+topic.getTopic()+
          ", partition="+topic.getPartition()+"\n"+
          ", timestamp="+timestamp+", offset="+offset.getOffset()+
          ", offsetTimestamp="+offset.getTimestamp()));

    }
  });

  // Convenience method for single-partition lookup
  consumer.offsetsForTimes(topicPartition, timestamp, done -> {
    if(done.succeeded()) {
      OffsetAndTimestamp offsetAndTimestamp = done.result();
        System.out.println("Offset for topic="+topicPartition.getTopic()+
          ", partition="+topicPartition.getPartition()+"\n"+
          ", timestamp="+timestamp+", offset="+offsetAndTimestamp.getOffset()+
          ", offsetTimestamp="+offsetAndTimestamp.getTimestamp());

    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:39,代碼來源:VertxKafkaClientExamples.java

示例6: exampleConsumerFlowControl

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
/**
 * Example about how Kafka consumer can pause reading from a topic partition
 * and then resume read operation for continuing to get messages
 * @param vertx
 * @param consumer
 */
public void exampleConsumerFlowControl(Vertx vertx, KafkaConsumer<String, String> consumer) {

  TopicPartition topicPartition = new TopicPartition()
    .setTopic("test")
    .setPartition(0);

  // registering the handler for incoming messages
  consumer.handler(record -> {
    System.out.println("key=" + record.key() + ",value=" + record.value() +
      ",partition=" + record.partition() + ",offset=" + record.offset());

    // i.e. pause/resume on partition 0, after reading message up to offset 5
    if ((record.partition() == 0) && (record.offset() == 5)) {

      // pause the read operations
      consumer.pause(topicPartition, ar -> {

        if (ar.succeeded()) {

          System.out.println("Paused");

          // resume read operation after a specific time
          vertx.setTimer(5000, timeId -> {

            // resumi read operations
            consumer.resume(topicPartition);
          });
        }
      });
    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:39,代碼來源:VertxKafkaClientExamples.java

示例7: paused

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
@Override
public void paused(Handler<AsyncResult<Set<TopicPartition>>> handler) {

  this.stream.paused(done -> {

    if (done.succeeded()) {
      handler.handle(Future.succeededFuture(Helper.from(done.result())));
    } else {
      handler.handle(Future.failedFuture(done.cause()));
    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:13,代碼來源:KafkaConsumerImpl.java

示例8: assignment

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
@Override
public KafkaConsumer<K, V> assignment(Handler<AsyncResult<Set<TopicPartition>>> handler) {
  this.stream.assignment(done -> {

    if (done.succeeded()) {
      handler.handle(Future.succeededFuture(Helper.from(done.result())));
    } else {
      handler.handle(Future.failedFuture(done.cause()));
    }

  });
  return this;
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:14,代碼來源:KafkaConsumerImpl.java

示例9: commit

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
@Override
public void commit(Map<TopicPartition, OffsetAndMetadata> offsets, Handler<AsyncResult<Map<TopicPartition, OffsetAndMetadata>>> completionHandler) {

  this.stream.commit(Helper.to(offsets), done -> {

    if (done.succeeded()) {

      completionHandler.handle(Future.succeededFuture(Helper.from(done.result())));
    } else {
      completionHandler.handle(Future.failedFuture(done.cause()));
    }

  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:15,代碼來源:KafkaConsumerImpl.java

示例10: committed

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
@Override
public void committed(TopicPartition topicPartition, Handler<AsyncResult<OffsetAndMetadata>> handler) {
  this.stream.committed(Helper.to(topicPartition), done -> {

    if (done.succeeded()) {
      handler.handle(Future.succeededFuture(Helper.from(done.result())));
    } else {
      handler.handle(Future.failedFuture(done.cause()));
    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:12,代碼來源:KafkaConsumerImpl.java

示例11: offsetsForTimes

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
@Override
public void offsetsForTimes(TopicPartition topicPartition, Long timestamp, Handler<AsyncResult<OffsetAndTimestamp>> handler) {
  Map<TopicPartition, Long> topicPartitions = new HashMap<>();
  topicPartitions.put(topicPartition, timestamp);

  this.stream.offsetsForTimes(Helper.toTopicPartitionTimes(topicPartitions), done -> {
    if(done.succeeded()) {
      if (done.result().values().size() == 1) {
        org.apache.kafka.common.TopicPartition kTopicPartition = new org.apache.kafka.common.TopicPartition (topicPartition.getTopic(), topicPartition.getPartition());
        org.apache.kafka.clients.consumer.OffsetAndTimestamp offsetAndTimestamp = done.result().get(kTopicPartition);
        if(offsetAndTimestamp != null) {
          OffsetAndTimestamp resultOffsetAndTimestamp = new OffsetAndTimestamp(offsetAndTimestamp.offset(), offsetAndTimestamp.timestamp());
          handler.handle(Future.succeededFuture(resultOffsetAndTimestamp));
        }
        // offsetAndTimestamp is null, i.e., search by timestamp did not lead to a result
        else {
          handler.handle(Future.succeededFuture());
        }
      } else if (done.result().values().size() == 0) {
        handler.handle(Future.succeededFuture());
      } else {
        handler.handle(Future.failedFuture("offsetsForTimes should return exactly one OffsetAndTimestamp"));
      }
    } else {
      handler.handle(Future.failedFuture(done.cause()));
    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:29,代碼來源:KafkaConsumerImpl.java

示例12: beginningOffsets

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
@Override
public void beginningOffsets(Set<TopicPartition> topicPartitions, Handler<AsyncResult<Map<TopicPartition, Long>>> handler) {
  this.stream.beginningOffsets(Helper.to(topicPartitions), done -> {
    if(done.succeeded()) {
      handler.handle(Future.succeededFuture(Helper.fromTopicPartitionOffsets(done.result())));
    } else {
      handler.handle(Future.failedFuture(done.cause()));
    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:11,代碼來源:KafkaConsumerImpl.java

示例13: endOffsets

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
@Override
public void endOffsets(Set<TopicPartition> topicPartitions, Handler<AsyncResult<Map<TopicPartition, Long>>> handler) {
  this.stream.endOffsets(Helper.to(topicPartitions), done -> {
    if(done.succeeded()) {
      handler.handle(Future.succeededFuture(Helper.fromTopicPartitionOffsets(done.result())));
    } else {
      handler.handle(Future.failedFuture(done.cause()));
    }
  });
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:11,代碼來源:KafkaConsumerImpl.java

示例14: adaptHandler

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
public static Handler<Set<org.apache.kafka.common.TopicPartition>> adaptHandler(Handler<Set<TopicPartition>> handler) {
  if (handler != null) {
    return topicPartitions -> handler.handle(Helper.from(topicPartitions));
  } else {
    return null;
  }
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:8,代碼來源:Helper.java

示例15: fromTopicPartitionOffsetAndTimestamp

import io.vertx.kafka.client.common.TopicPartition; //導入依賴的package包/類
public static Map<TopicPartition, OffsetAndTimestamp> fromTopicPartitionOffsetAndTimestamp(Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndTimestamp> topicPartitionOffsetAndTimestamps) {
  return topicPartitionOffsetAndTimestamps.entrySet().stream()
    .filter(e-> e.getValue() != null)
    .collect(Collectors.toMap(
      e -> new TopicPartition(e.getKey().topic(), e.getKey().partition()),
      e ->new OffsetAndTimestamp(e.getValue().offset(), e.getValue().timestamp()))
    );
}
 
開發者ID:vert-x3,項目名稱:vertx-kafka-client,代碼行數:9,代碼來源:Helper.java


注:本文中的io.vertx.kafka.client.common.TopicPartition類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。