本文整理汇总了Java中kafka.javaapi.OffsetResponse.hasError方法的典型用法代码示例。如果您正苦于以下问题:Java OffsetResponse.hasError方法的具体用法?Java OffsetResponse.hasError怎么用?Java OffsetResponse.hasError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kafka.javaapi.OffsetResponse
的用法示例。
在下文中一共展示了OffsetResponse.hasError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
public static long getLastOffset(SimpleConsumer consumer, String topic, int partition, long whichTime, String
clientName) {
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition,
PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
OffsetRequest request = new OffsetRequest(requestInfo, kafka.api.OffsetRequest.CurrentVersion(), clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
System.out.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic,
partition));
return 0;
}
long[] offsets = response.offsets(topic, partition);
return offsets[0];
}
示例2: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
private static long getLastOffset(SimpleConsumer consumer, String topic, int partition,
long whichTime) {
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(requestInfo,
kafka.api.OffsetRequest.CurrentVersion(), CLIENT_ID);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
System.out.println("Error fetching data Offset Data the Broker. Reason: "
+ response.errorCode(topic, partition));
return 0;
}
long[] offsets = response.offsets(topic, partition);
return offsets[0];
}
示例3: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
private static long getLastOffset(SimpleConsumer consumer, String topic, int partition, long whichTime) {
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(requestInfo,
kafka.api.OffsetRequest.CurrentVersion(), CLIENT_ID);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
System.out.println(
"Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition));
return 0;
}
long[] offsets = response.offsets(topic, partition);
return offsets[0];
}
示例4: getLatestOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
private static long getLatestOffset(SimpleConsumer consumer, TopicAndPartition topicAndPartition) {
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(kafka.api.OffsetRequest.LatestTime(), 1));
kafka.javaapi.OffsetRequest request =
new kafka.javaapi.OffsetRequest(requestInfo, kafka.api.OffsetRequest.CurrentVersion(), consumer.clientId());
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
logger.warn("Failed to fetch offset for {} due to {}", topicAndPartition,
response.errorCode(topicAndPartition.topic(), topicAndPartition.partition()));
return -1;
}
long[] offsets = response.offsets(topicAndPartition.topic(), topicAndPartition.partition());
return offsets[0];
}
示例5: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
/**
* @param consumer
* @param topic
* @param partition
* @param whichTime
* @param clientName
* @return 0 if consumer is null at this time
*/
public static long getLastOffset(SimpleConsumer consumer, String topic, int partition, long whichTime, String clientName)
{
if (consumer == null) {
return 0;
}
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
OffsetRequest request = new OffsetRequest(requestInfo, kafka.api.OffsetRequest.CurrentVersion(), clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
logger.error("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition));
return 0;
}
long[] offsets = response.offsets(topic, partition);
return offsets[0];
}
示例6: fetchResetOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
public long fetchResetOffset(String reset){
long time = LatestTime();
if (reset != null && reset.equals(SmallestTimeString()))
time = EarliestTime();
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
TopicAndPartition tp = new TopicAndPartition(topic, partition);
PartitionOffsetRequestInfo info = new PartitionOffsetRequestInfo(time,1);
requestInfo.put(tp, info);
OffsetRequest request = new OffsetRequest(requestInfo,CurrentVersion(), clientId);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
//ErrorMapping.exceptionFor(response.errorCode(topic, partition)).printStackTrace();
throw new KafkaPartitionReaderException(response.errorCode(topic, partition));
}
long[] offsets = response.offsets(topic, partition);
//TODO: confirm with xiaoju why we need this check?
// if (offsets.length <= 0)
// continue;
return offsets[0];
}
示例7: findAllOffsets
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
private static long[] findAllOffsets(SimpleConsumer consumer, String topicName, int partitionId)
{
TopicAndPartition topicAndPartition = new TopicAndPartition(topicName, partitionId);
// The API implies that this will always return all of the offsets. So it seems a partition can not have
// more than Integer.MAX_VALUE-1 segments.
//
// This also assumes that the lowest value returned will be the first segment available. So if segments have been dropped off, this value
// should not be 0.
PartitionOffsetRequestInfo partitionOffsetRequestInfo = new PartitionOffsetRequestInfo(kafka.api.OffsetRequest.LatestTime(), Integer.MAX_VALUE);
OffsetRequest offsetRequest = new OffsetRequest(ImmutableMap.of(topicAndPartition, partitionOffsetRequestInfo), kafka.api.OffsetRequest.CurrentVersion(), consumer.clientId());
OffsetResponse offsetResponse = consumer.getOffsetsBefore(offsetRequest);
if (offsetResponse.hasError()) {
short errorCode = offsetResponse.errorCode(topicName, partitionId);
log.warn("Offset response has error: %d", errorCode);
throw new PrestoException(KAFKA_SPLIT_ERROR, "could not fetch data from Kafka, error code is '" + errorCode + "'");
}
return offsetResponse.offsets(topicName, partitionId);
}
示例8: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
private long getLastOffset(SimpleConsumer consumer, String topic, int partition,
long whichTime, String clientName) throws StageException {
try {
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(
requestInfo, kafka.api.OffsetRequest.CurrentVersion(), clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
LOG.error(KafkaErrors.KAFKA_22.getMessage(), consumer.host() + ":" + consumer.port(),
response.errorCode(topic, partition));
return 0;
}
long[] offsets = response.offsets(topic, partition);
return offsets[0];
} catch (Exception e) {
LOG.error(KafkaErrors.KAFKA_30.getMessage(), e.toString(), e);
throw new StageException(KafkaErrors.KAFKA_30, e.toString(), e);
}
}
示例9: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
public static long getLastOffset( SimpleConsumer consumer, String topic, int partition, long whichTime, String clientName ) {
TopicAndPartition topicAndPartition = new TopicAndPartition( topic, partition );
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put( topicAndPartition, new PartitionOffsetRequestInfo( whichTime, 1 ) );
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest( requestInfo,
kafka.api.OffsetRequest.CurrentVersion(), clientName );
OffsetResponse response = consumer.getOffsetsBefore( request );
if ( response.hasError() ) {
System.out.println( "Error fetching data Offset Data the Broker. Reason: "
+ response.errorCode( topic, partition ) );
return 0;
}
long[] offsets = response.offsets( topic, partition );
return offsets[0];
}
示例10: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
public static long getLastOffset(SimpleConsumer consumer, String topic, int partition,
long whichTime, String clientName) {
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(
requestInfo, kafka.api.OffsetRequest.CurrentVersion(), clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
System.out.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition) );
return 0;
}
long[] offsets = response.offsets(topic, partition);
return offsets[0];
}
示例11: findLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
private long findLastOffset(TopicPartition topicPartition, SimpleConsumer consumer) {
TopicAndPartition topicAndPartition = new TopicAndPartition(topicPartition.getTopic(),
topicPartition.getPartition());
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo =
new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(
kafka.api.OffsetRequest.LatestTime(), 1));
final String clientName = getClientName(topicPartition);
OffsetRequest request = new OffsetRequest(requestInfo,
kafka.api.OffsetRequest.CurrentVersion(),
clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
throw new RuntimeException("Error fetching offset data. Reason: " +
response.errorCode(topicPartition.getTopic(), topicPartition.getPartition()));
}
long[] offsets = response.offsets(topicPartition.getTopic(),
topicPartition.getPartition());
return offsets[0] - 1;
}
示例12: getEarliestOffsetFromKafka
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
private long getEarliestOffsetFromKafka(String topic, int partition, long startTime) {
LOG.info("getEarliestOffsetFromKafka.");
TopicAndPartition tp = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo =
new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(tp, new PartitionOffsetRequestInfo(startTime, 1));
OffsetRequest req = new OffsetRequest(
requestInfo, kafka.api.OffsetRequest.CurrentVersion(), getClientName());
OffsetResponse resp = consumer.getOffsetsBefore(req);
if (resp.hasError()) {
LOG.error("error when fetching offset: " + resp.errorCode(topic, partition)); //xxx
return 0;
}
LOG.info("Earliest offset " + resp.offsets(topic, partition)[0]);
return resp.offsets(topic, partition)[0];
}
示例13: getLastOffSet
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
/**
* 获取当前groupID对应的consumer在对应的topic和partition中对应的offset偏移量
*
* @param consumer 消费者
* @param groupId 消费者分区id
* @param topic 所属的Topic
* @param partitionID 所属的分区ID
* @param whichTime 用于判断,当consumer从没有消费数据的时候,从当前topic的Partition的那个offset开始读取数据
* @param clientName client名称
*
* @return 正常情况下,返回非负数,当出现异常的时候,返回-1
*/
public long getLastOffSet(SimpleConsumer consumer, String groupId, String topic, int partitionID, long whichTime,
String clientName) {
// 1. 从ZK中获取偏移量,当zk的返回偏移量大于0的时候,表示是一个正常的偏移量
long offset = this.getOffsetOfTopicAndPartition(consumer, groupId, clientName, topic, partitionID);
if (offset > 0) {
return offset;
}
// 2. 获取当前topic当前分区的数据偏移量
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partitionID);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfoMap = new HashMap<TopicAndPartition,
PartitionOffsetRequestInfo>();
requestInfoMap.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
OffsetRequest request = new OffsetRequest(requestInfoMap, kafka.api.OffsetRequest.CurrentVersion(), clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
System.out.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic,
partitionID));
return -1;
}
// 获取偏移量
long[] offsets = response.offsets(topic, partitionID);
return offsets[0];
}
示例14: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
public static long getLastOffset(String leadBrokerHost, int port, String topic,
int partition, long whichTime) {
String clientName = "Client_" + topic + "_" + partition;
SimpleConsumer consumer = new SimpleConsumer(leadBrokerHost, port,
soTimeoutMS, bufferSize, clientName);
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo =
new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(requestInfo,
kafka.api.OffsetRequest.CurrentVersion(), clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
LOG.warn("Error fetching data Offset Data the Broker. Reason: "
+ response.errorCode(topic, partition));
return 0;
}
long[] offsets = response.offsets(topic, partition);
if (consumer != null) {
try {
consumer.close();
} catch (Exception e) {
LOG.error("SimpleConsumer.close() error due to ", e);
}
}
return offsets.length > 0 ? offsets[0] : -1;
}
示例15: getLastOffset
import kafka.javaapi.OffsetResponse; //导入方法依赖的package包/类
/**
* Retrieves the last offset before the given timestamp for a given topic partition.
*
* @return The last offset before the given timestamp or {@code 0} if failed to do so.
*/
private long getLastOffset(TopicPartition topicPart, long timestamp) {
BrokerInfo brokerInfo = brokerService.getLeader(topicPart.getTopic(), topicPart.getPartition());
SimpleConsumer consumer = brokerInfo == null ? null : consumers.getUnchecked(brokerInfo);
// If no broker, treat it as failure attempt.
if (consumer == null) {
LOG.warn("Failed to talk to any broker. Default offset to 0 for {}", topicPart);
return 0L;
}
// Fire offset request
OffsetRequest request = new OffsetRequest(ImmutableMap.of(
new TopicAndPartition(topicPart.getTopic(), topicPart.getPartition()),
new PartitionOffsetRequestInfo(timestamp, 1)
), kafka.api.OffsetRequest.CurrentVersion(), consumer.clientId());
OffsetResponse response = consumer.getOffsetsBefore(request);
// Retrieve offsets from response
long[] offsets = response.hasError() ? null : response.offsets(topicPart.getTopic(), topicPart.getPartition());
if (offsets == null || offsets.length <= 0) {
short errorCode = response.errorCode(topicPart.getTopic(), topicPart.getPartition());
// If the topic partition doesn't exists, use offset 0 without logging error.
if (errorCode != ErrorMapping.UnknownTopicOrPartitionCode()) {
consumers.refresh(brokerInfo);
LOG.warn("Failed to fetch offset for {} with timestamp {}. Error: {}. Default offset to 0.",
topicPart, timestamp, errorCode);
}
return 0L;
}
LOG.debug("Offset {} fetched for {} with timestamp {}.", offsets[0], topicPart, timestamp);
return offsets[0];
}