Java Code Examples for org.apache.curator.framework.api.GetChildrenBuilder#forPath()

The following examples show how to use org.apache.curator.framework.api.GetChildrenBuilder#forPath() . 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: NodeService.java    From config-toolkit with Apache License 2.0 6 votes vote down vote up
@Override
public List<PropertyItem> findProperties(String node) {
	LOGGER.debug("Find properties in node: [{}]", node);
	List<PropertyItem> properties = Lists.newArrayList();
	try {
		Stat stat = client.checkExists().forPath(node);
		if (stat != null) {
			GetChildrenBuilder childrenBuilder = client.getChildren();
			List<String> children = childrenBuilder.forPath(node);
			GetDataBuilder dataBuilder = client.getData();
			if (children != null) {
				for (String child : children) {
					String propPath = ZKPaths.makePath(node, child);
					PropertyItem item = new PropertyItem(child, new String(dataBuilder.forPath(propPath), Charsets.UTF_8));
					properties.add(item);
				}
			}
		}
	} catch (Exception e) {
		LOGGER.error(e.getMessage(), e);
	}
	return properties;
}
 
Example 2
Source File: CuratorService.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * List all children of a path
 * @param path path of operation
 * @return a possibly empty list of children
 * @throws IOException
 */
public List<String> zkList(String path) throws IOException {
  checkServiceLive();
  String fullpath = createFullPath(path);
  try {
    if (LOG.isDebugEnabled()) {
      LOG.debug("ls {}", fullpath);
    }
    GetChildrenBuilder builder = curator.getChildren();
    List<String> children = builder.forPath(fullpath);
    return children;
  } catch (Exception e) {
    throw operationFailure(path, "ls()", e);
  }
}
 
Example 3
Source File: CuratorService.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * List all children of a path
 * @param path path of operation
 * @return a possibly empty list of children
 * @throws IOException
 */
public List<String> zkList(String path) throws IOException {
  checkServiceLive();
  String fullpath = createFullPath(path);
  try {
    if (LOG.isDebugEnabled()) {
      LOG.debug("ls {}", fullpath);
    }
    GetChildrenBuilder builder = curator.getChildren();
    List<String> children = builder.forPath(fullpath);
    return children;
  } catch (Exception e) {
    throw operationFailure(path, "ls()", e);
  }
}
 
Example 4
Source File: ZkSubscriptionImpl.java    From nakadi with MIT License 5 votes vote down vote up
@Override
protected List<String> query(final boolean setListener) throws NakadiRuntimeException {
    final GetChildrenBuilder builder = curatorFramework.getChildren();
    if (setListener) {
        builder.usingWatcher(this);
    }
    try {
        return builder.forPath(key);
    } catch (final Exception ex) {
        throw new NakadiRuntimeException(ex);
    }
}
 
Example 5
Source File: NodeService.java    From config-toolkit with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> listChildren(String node) {
	LOGGER.debug("Find children of node: [{}]", node);
	List<String> children = null;
	try {
		Stat stat = client.checkExists().forPath(node);
		if (stat != null) {
			GetChildrenBuilder childrenBuilder = client.getChildren();
			children = childrenBuilder.forPath(node);
		}
	} catch (Exception e) {
		LOGGER.error(e.getMessage(), e);
	}
	return children;
}
 
Example 6
Source File: ZKPathDumper.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively expand the path into the supplied string builder, increasing
 * the indentation by {@link #INDENT} as it proceeds (depth first) down
 * the tree
 * @param builder string build to append to
 * @param path path to examine
 * @param indent current indentation
 */
private void expand(StringBuilder builder,
    String path,
    int indent) {
  try {
    GetChildrenBuilder childrenBuilder = curator.getChildren();
    List<String> children = childrenBuilder.forPath(path);
    for (String child : children) {
      String childPath = path + "/" + child;
      String body;
      Stat stat = curator.checkExists().forPath(childPath);
      StringBuilder bodyBuilder = new StringBuilder(256);
      bodyBuilder.append("  [")
                        .append(stat.getDataLength())
                        .append("]");
      if (stat.getEphemeralOwner() > 0) {
        bodyBuilder.append("*");
      }
      if (verbose) {
        // verbose: extract ACLs
        builder.append(" -- ");
        List<ACL> acls =
            curator.getACL().forPath(childPath);
        for (ACL acl : acls) {
          builder.append(RegistrySecurity.aclToString(acl));
          builder.append(" ");
        }
      }
      body = bodyBuilder.toString();
      // print each child
      append(builder, indent, ' ');
      builder.append('/').append(child);
      builder.append(body);
      builder.append('\n');
      // recurse
      expand(builder, childPath, indent + INDENT);
    }
  } catch (Exception e) {
    builder.append(e.toString()).append("\n");
  }
}
 
Example 7
Source File: ZKPathDumper.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively expand the path into the supplied string builder, increasing
 * the indentation by {@link #INDENT} as it proceeds (depth first) down
 * the tree
 * @param builder string build to append to
 * @param path path to examine
 * @param indent current indentation
 */
private void expand(StringBuilder builder,
    String path,
    int indent) {
  try {
    GetChildrenBuilder childrenBuilder = curator.getChildren();
    List<String> children = childrenBuilder.forPath(path);
    for (String child : children) {
      String childPath = path + "/" + child;
      String body;
      Stat stat = curator.checkExists().forPath(childPath);
      StringBuilder bodyBuilder = new StringBuilder(256);
      bodyBuilder.append("  [")
                        .append(stat.getDataLength())
                        .append("]");
      if (stat.getEphemeralOwner() > 0) {
        bodyBuilder.append("*");
      }
      if (verbose) {
        // verbose: extract ACLs
        builder.append(" -- ");
        List<ACL> acls =
            curator.getACL().forPath(childPath);
        for (ACL acl : acls) {
          builder.append(RegistrySecurity.aclToString(acl));
          builder.append(" ");
        }
      }
      body = bodyBuilder.toString();
      // print each child
      append(builder, indent, ' ');
      builder.append('/').append(child);
      builder.append(body);
      builder.append('\n');
      // recurse
      expand(builder, childPath, indent + INDENT);
    }
  } catch (Exception e) {
    builder.append(e.toString()).append("\n");
  }
}
 
Example 8
Source File: KafkaLocationManager.java    From nakadi with MIT License 4 votes vote down vote up
private void updateBootstrapServers(final boolean createWatcher) {
    final List<Broker> brokers = new ArrayList<>();
    try {
        final CuratorFramework curator = zkFactory.get();
        final GetChildrenBuilder childrenBuilder = curator.getChildren();
        if (createWatcher) {
            LOG.info("Creating watcher on brokers change");
            childrenBuilder.usingWatcher((Watcher) event -> {
                if (event.getType() != Watcher.Event.EventType.NodeChildrenChanged) {
                    return;
                }
                this.scheduledExecutor.schedule(() -> updateBootstrapServersSafe(true), 0, TimeUnit.MILLISECONDS);
            });
        }
        for (final String brokerId : childrenBuilder.forPath(BROKERS_IDS_PATH)) {
            final byte[] brokerData = curator.getData().forPath(BROKERS_IDS_PATH + "/" + brokerId);
            brokers.add(Broker.fromByteJson(brokerData));
        }
    } catch (final Exception e) {
        LOG.error("Failed to fetch list of brokers from ZooKeeper", e);
        return;
    }

    if (brokers.isEmpty()) {
        return;
    }
    final String bootstrapServers = brokers.stream()
            .sorted()
            .map(Broker::toString)
            .collect(Collectors.joining(","));
    final String currentBootstrapServers =
            (String) kafkaProperties.get(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG);

    if (bootstrapServers.equals(currentBootstrapServers)) {
        return;
    }

    kafkaProperties.setProperty(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    this.ipAddressChangeListeners.forEach(listener -> {
        try {
            listener.run();
        } catch (final RuntimeException re) {
            LOG.error("Failed to process listener {}", re.getMessage(), re);
        }
    });
    LOG.info("Kafka client bootstrap servers changed: {}", bootstrapServers);
}