本文整理汇总了Java中org.I0Itec.zkclient.ZkConnection类的典型用法代码示例。如果您正苦于以下问题:Java ZkConnection类的具体用法?Java ZkConnection怎么用?Java ZkConnection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ZkConnection类属于org.I0Itec.zkclient包,在下文中一共展示了ZkConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createTopic
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
/**
* Create a Kafka topic with the given parameters.
*
* @param topic The name of the topic.
* @param partitions The number of partitions for this topic.
* @param replication The replication factor for (partitions of) this topic.
* @param topicConfig Additional topic-level configuration settings.
*/
public void createTopic(final String topic,
final int partitions,
final int replication,
final Properties topicConfig) {
log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
topic, partitions, replication, topicConfig);
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// createTopic() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
final ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
final boolean isSecure = false;
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
zkClient.close();
}
示例2: createTopic
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
/**
* Create a Kafka topic with the given parameters.
*
* @param topic The name of the topic.
* @param partitions The number of partitions for this topic.
* @param replication The replication factor for (partitions of) this topic.
* @param topicConfig Additional topic-level configuration settings.
*/
public void createTopic(String topic,
int partitions,
int replication,
Properties topicConfig) {
log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
topic, partitions, replication, topicConfig);
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// createTopic() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
boolean isSecure = false;
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
zkClient.close();
}
示例3: createTopic
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
/**
* Create a Kafka topic with the given parameters.
*
* @param topic The name of the topic.
* @param partitions The number of partitions for this topic.
* @param replication The replication factor for (partitions of) this topic.
* @param topicConfig Additional topic-level configuration settings.
*/
public void createTopic(final String topic,
final int partitions,
final int replication,
final Properties topicConfig) {
log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
topic, partitions, replication, topicConfig);
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// createTopic() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
final ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
final boolean isSecure = false;
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
zkClient.close();
}
示例4: KafkaAdminClient
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
/**
* Creates and connects a new KafkaAdminClient
* @param zkConnect The zk connect string
* @param sessionTimeout The session timeout
* @param connectionTimeout The connection timeout
*/
private KafkaAdminClient(final String zkConnect, final int sessionTimeout, final int connectionTimeout) {
this.zkConnect = zkConnect;
this.sessionTimeout = sessionTimeout;
this.connectionTimeout = connectionTimeout;
zkClient = new ZkClient(
zkConnect,
this.sessionTimeout,
this.connectionTimeout,
ZKStringSerializer$.MODULE$);
zkConnection = new ZkConnection(zkConnect, DEFAULT_SESSION_TIMEOUT);
zkConnection.connect(this);
zkUtils = new ZkUtils(zkClient, zkConnection, false);
final String endPoint = findEndpoint(zkConnection);
adminClient = endPoint==null ? null : AdminClient.createSimplePlaintext(endPoint);
}
示例5: startKafka
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
private void startKafka() throws Exception
{
FileUtils.deleteDirectory(new File(kafkaTmpDir));
Properties props = new Properties();
props.setProperty("zookeeper.session.timeout.ms", "100000");
props.put("advertised.host.name", "localhost");
props.put("port", 11111);
// props.put("broker.id", "0");
props.put("log.dir", kafkaTmpDir);
props.put("enable.zookeeper", "true");
props.put("zookeeper.connect", zookeeperLocalCluster.getConnectString());
KafkaConfig kafkaConfig = KafkaConfig.fromProps(props);
kafkaLocalBroker = new KafkaServer(kafkaConfig, new SystemTime(), scala.Option.apply("kafkaThread"));
kafkaLocalBroker.startup();
zkClient = new ZkClient(zookeeperLocalCluster.getConnectString(), 60000, 60000, ZKStringSerializer$.MODULE$);
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperLocalCluster.getConnectString()), false);
// ZkUtils zkUtils = ZkUtils.apply(zookeeperLocalCluster.getConnectString(), 60000, 60000, false);
AdminUtils.createTopic(zkUtils, topic, 1, 1, new Properties());
}
示例6: initKafka
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
@BeforeClass
public static void initKafka() throws Exception {
synchronized (TestKafkaSuit.class) {
if (initCount.get() == 0) {
ZookeeperTestUtil.setZookeeperSaslTestConfigProps();
System.setProperty(JaasUtils.JAVA_LOGIN_CONFIG_PARAM, ClassLoader.getSystemResource(LOGIN_CONF_RESOURCE_PATHNAME).getFile());
embeddedKafkaCluster = new EmbeddedKafkaCluster();
Properties topicProps = new Properties();
zkClient = new ZkClient(embeddedKafkaCluster.getZkServer().getConnectionString(), SESSION_TIMEOUT, CONN_TIMEOUT, ZKStringSerializer$.MODULE$);
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(embeddedKafkaCluster.getZkServer().getConnectionString()), false);
AdminUtils.createTopic(zkUtils, QueryConstants.JSON_TOPIC, 1, 1, topicProps, RackAwareMode.Disabled$.MODULE$);
org.apache.kafka.common.requests.MetadataResponse.TopicMetadata fetchTopicMetadataFromZk = AdminUtils
.fetchTopicMetadataFromZk(QueryConstants.JSON_TOPIC, zkUtils);
logger.info("Topic Metadata: " + fetchTopicMetadataFromZk);
KafkaMessageGenerator generator = new KafkaMessageGenerator(embeddedKafkaCluster.getKafkaBrokerList(),
StringSerializer.class);
generator.populateJsonMsgIntoKafka(QueryConstants.JSON_TOPIC, NUM_JSON_MSG);
}
initCount.incrementAndGet();
runningSuite = true;
}
logger.info("Initialized Embedded Zookeeper and Kafka");
}
示例7: createTopic
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
/**
* Create a Kafka topic with the given parameters.
*
* @param topic The name of the topic.
* @param partitions The number of partitions for this topic.
* @param replication The replication factor for (partitions of) this topic.
* @param topicConfig Additional topic-level configuration settings.
*/
public void createTopic(String topic,
int partitions,
int replication,
Properties topicConfig) {
log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
topic, partitions, replication, topicConfig);
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// registerTopic() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
boolean isSecure = false;
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
zkClient.close();
}
示例8: waitUntilConnected
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
/**
* wait until we get a non-zero session-id. note that we might lose zkconnection
* right after we read session-id. but it's ok to get stale session-id and we will have
* another handle-new-session callback to correct this.
*/
void waitUntilConnected() {
boolean isConnected;
do {
isConnected =
_zkclient.waitUntilConnected(ZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
if (!isConnected) {
LOG.error("fail to connect zkserver: " + _zkAddress + " in "
+ ZkClient.DEFAULT_CONNECTION_TIMEOUT + "ms. expiredSessionId: " + _sessionId
+ ", clusterName: " + _clusterName);
continue;
}
ZkConnection zkConnection = ((ZkConnection) _zkclient.getConnection());
_sessionId = Long.toHexString(zkConnection.getZookeeper().getSessionId());
/**
* at the time we read session-id, zkconnection might be lost again
* wait until we get a non-zero session-id
*/
} while (!isConnected || "0".equals(_sessionId));
LOG.info("Handling new session, session id: " + _sessionId + ", instance: " + _instanceName
+ ", instanceTye: " + _instanceType + ", cluster: " + _clusterName + ", zkconnection: "
+ ((ZkConnection) _zkclient.getConnection()).getZookeeper());
}
示例9: simulateSessionExpiry
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
protected void simulateSessionExpiry(ZkConnection zkConnection) throws IOException,
InterruptedException {
ZooKeeper oldZookeeper = zkConnection.getZookeeper();
LOG.info("Old sessionId = " + oldZookeeper.getSessionId());
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
LOG.info("In New connection, process event:" + event);
}
};
ZooKeeper newZookeeper =
new ZooKeeper(zkConnection.getServers(), oldZookeeper.getSessionTimeout(), watcher,
oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd());
LOG.info("New sessionId = " + newZookeeper.getSessionId());
// Thread.sleep(3000);
newZookeeper.close();
Thread.sleep(10000);
oldZookeeper = zkConnection.getZookeeper();
LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId());
}
示例10: createTopic
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
public void createTopic(String topic) {
ZkClient zkClient = new ZkClient(
kafkaRpcConfig.getZookeeperConnect(),
kafkaRpcConfig.getSessionTimeout(),
kafkaRpcConfig.getConnectionTimeout(),
ZKStringSerializer$.MODULE$);
try {
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(kafkaRpcConfig.getZookeeperConnect()), false);
Properties topicConfig = kafkaRpcConfig.topicProps();
if (!AdminUtils.topicExists(zkUtils, topic)) {
AdminUtils.createTopic(zkUtils, topic, kafkaRpcConfig.getNumPartitions(),
kafkaRpcConfig.getReplicationFactor(), topicConfig, RackAwareMode.Enforced$.MODULE$);
}
} finally {
zkClient.close();
}
}
示例11: getZKUtils
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
/**
* Create a Kafka Zookeeper client
*
* @param zkClient zookeeper client
* @param zkServers It's a comma-separated list of zookeeper servers host:port
* @return {@link ZkUtils} Kafka Zookeeper client
* @throws com.mcafee.dxl.streaming.operations.client.exception.ConnectionException when Zookeeper connection failed
*/
protected ZkUtils getZKUtils(final ZkClient zkClient, final String zkServers) {
try {
return new ZkUtils(zkClient,
new ZkConnection(zkServers),
false);
} catch (Exception e) {
throw new ConnectionException(zkServers,e.getMessage(),e,this.getClass());
}
}
示例12: BrokerFailureDetector
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
public BrokerFailureDetector(KafkaCruiseControlConfig config,
LoadMonitor loadMonitor,
Queue<Anomaly> anomalies,
Time time) {
String zkUrl = config.getString(KafkaCruiseControlConfig.ZOOKEEPER_CONNECT_CONFIG);
ZkConnection zkConnection = new ZkConnection(zkUrl, 30000);
_zkClient = new ZkClient(zkConnection, 30000, new ZkStringSerializer());
// Do not support secure ZK at this point.
_zkUtils = new ZkUtils(_zkClient, zkConnection, false);
_failedBrokers = new HashMap<>();
_failedBrokersZkPath = config.getString(KafkaCruiseControlConfig.FAILED_BROKERS_ZK_PATH_CONFIG);
_loadMonitor = loadMonitor;
_anomalies = anomalies;
_time = time;
}
示例13: checkAndCreateTopic
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
/**
* Creates Kafka topic if it does not exist.
*
* @param topic Kafka topic
*/
protected void checkAndCreateTopic(final String topic) {
String hosts = config.getZookeeperHosts();
ZkClient zkClient = new ZkClient(hosts, config.getZookeeperSessionTimeout(),
config.getZookeeperConnectTimeout(), ZKStringSerializer$.MODULE$);
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(hosts), false);
// FIXME(dbogun): race condition
if (!AdminUtils.topicExists(zkUtils, topic)) {
AdminUtils.createTopic(zkUtils, topic, 1, 1,
AdminUtils.createTopic$default$5(), AdminUtils.createTopic$default$6());
}
}
示例14: SchedulerMonitor
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
public SchedulerMonitor(String registryType, String servers) {
if ("redis".equals(registryType)) {
} else {
ZkConnection zkConnection = new ZkConnection(servers);
zkClient = new ZkClient(zkConnection, 3000);
}
}
示例15: deleteTopic
import org.I0Itec.zkclient.ZkConnection; //导入依赖的package包/类
public void deleteTopic(final String topic) {
log.debug("Deleting topic { name: {} }", topic);
final ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
final boolean isSecure = false;
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.deleteTopic(zkUtils, topic);
zkClient.close();
}