Java Code Examples for kafka.utils.ZKGroupTopicDirs#consumerOffsetDir()

The following examples show how to use kafka.utils.ZKGroupTopicDirs#consumerOffsetDir() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZookeeperOffsetHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public static Long getOffsetFromZooKeeper(CuratorFramework curatorClient, String groupId, String topic, int partition) throws Exception {
	ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupId, topic);
	String path = topicDirs.consumerOffsetDir() + "/" + partition;
	curatorClient.newNamespaceAwareEnsurePath(path).ensure(curatorClient.getZookeeperClient());

	byte[] data = curatorClient.getData().forPath(path);

	if (data == null) {
		return null;
	} else {
		String asString = new String(data, ConfigConstants.DEFAULT_CHARSET);
		if (asString.length() == 0) {
			return null;
		} else {
			try {
				return Long.valueOf(asString);
			}
			catch (NumberFormatException e) {
				LOG.error(
						"The offset in ZooKeeper for group '{}', topic '{}', partition {} is a malformed string: {}",
					groupId, topic, partition, asString);
				return null;
			}
		}
	}
}
 
Example 2
Source File: ZookeeperOffsetHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
public static Long getOffsetFromZooKeeper(CuratorFramework curatorClient, String groupId, String topic, int partition) throws Exception {
	ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupId, topic);
	String path = topicDirs.consumerOffsetDir() + "/" + partition;
	curatorClient.newNamespaceAwareEnsurePath(path).ensure(curatorClient.getZookeeperClient());

	byte[] data = curatorClient.getData().forPath(path);

	if (data == null) {
		return null;
	} else {
		String asString = new String(data, ConfigConstants.DEFAULT_CHARSET);
		if (asString.length() == 0) {
			return null;
		} else {
			try {
				return Long.valueOf(asString);
			}
			catch (NumberFormatException e) {
				LOG.error(
						"The offset in ZooKeeper for group '{}', topic '{}', partition {} is a malformed string: {}",
					groupId, topic, partition, asString);
				return null;
			}
		}
	}
}
 
Example 3
Source File: ZookeeperCheckpointManager.java    From uReplicator with Apache License 2.0 6 votes vote down vote up
public Long fetchOffset(TopicPartition topicPartition) {
  ZKGroupTopicDirs dirs = new ZKGroupTopicDirs(groupId, topicPartition.topic());
  String path = dirs.consumerOffsetDir() + "/" + topicPartition.partition();
  if (!commitZkClient.exists(path)) {
    return -1L;
  }
  String offset = commitZkClient.readData(path).toString();
  if (StringUtils.isEmpty(offset)) {
    return -1L;
  }
  try {
    return Long.parseLong(offset);
  } catch (Exception e) {
    LOGGER.warn("Parse offset {} for topic partition failed, zk path: {}", offset, path);
    return -1L;
  }
}
 
Example 4
Source File: ZookeeperOffsetHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static void setOffsetInZooKeeper(CuratorFramework curatorClient, String groupId, String topic, int partition, long offset) throws Exception {
	ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupId, topic);
	String path = topicDirs.consumerOffsetDir() + "/" + partition;
	curatorClient.newNamespaceAwareEnsurePath(path).ensure(curatorClient.getZookeeperClient());
	byte[] data = Long.toString(offset).getBytes(ConfigConstants.DEFAULT_CHARSET);
	curatorClient.setData().forPath(path, data);
}
 
Example 5
Source File: ZookeeperOffsetHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
public static void setOffsetInZooKeeper(CuratorFramework curatorClient, String groupId, String topic, int partition, long offset) throws Exception {
	ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupId, topic);
	String path = topicDirs.consumerOffsetDir() + "/" + partition;
	curatorClient.newNamespaceAwareEnsurePath(path).ensure(curatorClient.getZookeeperClient());
	byte[] data = Long.toString(offset).getBytes(ConfigConstants.DEFAULT_CHARSET);
	curatorClient.setData().forPath(path, data);
}
 
Example 6
Source File: ZookeeperCheckpointManager.java    From uReplicator with Apache License 2.0 5 votes vote down vote up
private void commitOffsetToZookeeper(TopicPartition topicPartition, long offset) {
  if (!offsetCheckpoints.containsKey(topicPartition)
      || offsetCheckpoints.get(topicPartition) != offset) {
    ZKGroupTopicDirs dirs = new ZKGroupTopicDirs(groupId, topicPartition.topic());
    String path = dirs.consumerOffsetDir() + "/" + topicPartition.partition();
    if (!commitZkClient.exists(path)) {
      commitZkClient.createPersistent(path, true);
    }
    commitZkClient.writeData(path,
        String.valueOf(offset));
    offsetCheckpoints.put(topicPartition, offset);
  }
}