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; } } } } }
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); }
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); }
/** * @param zookeeperHosts * Zookeeper hosts e.g. localhost:2181. If multiple zookeeper * then host1:port1[,host2:port2,...] * @param groupID * consumer group to update * @param offsets * mapping of (topic and) partition to offset to push to * Zookeeper */ public void createOffsets(String zookeeperHosts, String groupID, Map<TopicAndPartition, Long> offsets) { try (SuperZkClient zkClient = new SuperZkClient(zookeeperHosts)) { for (Map.Entry<TopicAndPartition, Long> entry : offsets.entrySet()) { TopicAndPartition topicAndPartition = entry.getKey(); ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupID, topicAndPartition.topic()); int partition = topicAndPartition.partition(); long offset = entry.getValue(); String partitionOffsetPath = topicDirs.consumerOffsetDir() + "/" + partition; ZkUtils.updatePersistentPath(zkClient, partitionOffsetPath, Long.toString(offset)); } } }
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); }
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); }
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; } } } } }
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; } } } } }
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; } } } } }