本文整理汇总了Java中org.apache.zookeeper.ZooKeeper.getData方法的典型用法代码示例。如果您正苦于以下问题:Java ZooKeeper.getData方法的具体用法?Java ZooKeeper.getData怎么用?Java ZooKeeper.getData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.ZooKeeper
的用法示例。
在下文中一共展示了ZooKeeper.getData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBadAuthNotifiesWatch
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testBadAuthNotifiesWatch() throws Exception {
ZooKeeper zk = createClient();
try {
zk.addAuthInfo("FOO", "BAR".getBytes());
zk.getData("/path1", false, null);
Assert.fail("Should get auth state error");
} catch(KeeperException.AuthFailedException e) {
if(!authFailed.await(CONNECTION_TIMEOUT,
TimeUnit.MILLISECONDS))
{
Assert.fail("Should have called my watcher");
}
}
finally {
zk.close();
}
}
示例2: getCanalCursor
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public PositionEventData getCanalCursor(String destination, short clientId) {
String path = String.format(CANAL_CURSOR_PATH, destination, String.valueOf(clientId));
try {
IZkConnection connection = zookeeper.getConnection();
// zkclient会将获取stat信息和正常的操作分开,使用原生的zk进行优化
ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper();
Stat stat = new Stat();
byte[] bytes = orginZk.getData(path, false, stat);
PositionEventData eventData = new PositionEventData();
eventData.setCreateTime(new Date(stat.getCtime()));
eventData.setModifiedTime(new Date(stat.getMtime()));
eventData.setPosition(new String(bytes, "UTF-8"));
return eventData;
} catch (Exception e) {
return null;
}
}
示例3: testMultiToFollower
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Tests if a multiop submitted to a non-leader propagates to the leader properly
* (see ZOOKEEPER-1124).
*
* The test works as follows. It has a client connect to a follower and submit a multiop
* to the follower. It then verifies that the multiop successfully gets committed by the leader.
*
* Without the fix in ZOOKEEPER-1124, this fails with a ConnectionLoss KeeperException.
*/
@Test
public void testMultiToFollower() throws Exception {
qu = new QuorumUtil(1);
CountdownWatcher watcher = new CountdownWatcher();
qu.startQuorum();
int index = 1;
while(qu.getPeer(index).peer.leader == null)
index++;
ZooKeeper zk = new ZooKeeper(
"127.0.0.1:" + qu.getPeer((index == 1)?2:1).peer.getClientPort(),
ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.multi(Arrays.asList(
Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
Op.create("/multi1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
));
zk.getData("/multi0", false, null);
zk.getData("/multi1", false, null);
zk.getData("/multi2", false, null);
zk.close();
}
示例4: testSequentialNodeData
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testSequentialNodeData() throws Exception {
ZooKeeper zk= null;
String queue_handle = "/queue";
try {
zk = createClient();
zk.create(queue_handle, new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create(queue_handle + "/element", "0".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
zk.create(queue_handle + "/element", "1".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
List<String> children = zk.getChildren(queue_handle, true);
Assert.assertEquals(children.size(), 2);
String child1 = children.get(0);
String child2 = children.get(1);
int compareResult = child1.compareTo(child2);
Assert.assertNotSame(compareResult, 0);
if (compareResult < 0) {
} else {
String temp = child1;
child1 = child2;
child2 = temp;
}
String child1data = new String(zk.getData(queue_handle
+ "/" + child1, false, null));
String child2data = new String(zk.getData(queue_handle
+ "/" + child2, false, null));
Assert.assertEquals(child1data, "0");
Assert.assertEquals(child2data, "1");
} finally {
if (zk != null) {
zk.close();
}
}
}
示例5: printTree
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public static void printTree(ZooKeeper zk,String path,Writer writer,String lineSplitChar) throws Exception{
String[] list = getTree(zk,path);
Stat stat = new Stat();
for(String name:list){
byte[] value = zk.getData(name, false, stat);
if(value == null){
writer.write(name + lineSplitChar);
}else{
writer.write(name+"[v."+ stat.getVersion() +"]"+"["+ new String(value) +"]" + lineSplitChar);
}
}
}
示例6: testNodeDataChanged
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeDataChanged() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test-changed";
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getData(path, watcher, null);
qu.shutdown(1);
zk2.delete(path, -1);
zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
qu.start(1);
watcher.waitForConnected(TIMEOUT);
watcher.assertEvent(TIMEOUT, EventType.NodeDataChanged);
zk1.exists(path, watcher);
qu.shutdown(1);
zk2.delete(path, -1);
zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDataChanged);
qu.shutdownAll();
}
示例7: testNodeDeleted
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeDeleted() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test-deleted";
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getData(path, watcher, null);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.exists(path, watcher);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getChildren(path, watcher);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
qu.shutdownAll();
}
示例8: read
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
static EditLogLedgerMetadata read(ZooKeeper zkc, String path)
throws IOException, KeeperException.NoNodeException {
try {
byte[] data = zkc.getData(path, false, null);
EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
if (LOG.isDebugEnabled()) {
LOG.debug("Reading " + path + " data: " + new String(data, UTF_8));
}
TextFormat.merge(new String(data, UTF_8), builder);
if (!builder.isInitialized()) {
throw new IOException("Invalid/Incomplete data in znode");
}
EditLogLedgerProto ledger = builder.build();
int dataLayoutVersion = ledger.getDataLayoutVersion();
long ledgerId = ledger.getLedgerId();
long firstTxId = ledger.getFirstTxId();
if (ledger.hasLastTxId()) {
long lastTxId = ledger.getLastTxId();
return new EditLogLedgerMetadata(path, dataLayoutVersion,
ledgerId, firstTxId, lastTxId);
} else {
return new EditLogLedgerMetadata(path, dataLayoutVersion,
ledgerId, firstTxId);
}
} catch(KeeperException.NoNodeException nne) {
throw nne;
} catch(KeeperException ke) {
throw new IOException("Error reading from zookeeper", ke);
} catch (InterruptedException ie) {
throw new IOException("Interrupted reading from zookeeper", ie);
}
}
示例9: testMultiTransaction
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Test write operations using multi request.
*/
@Test(timeout = 90000)
public void testMultiTransaction() throws Exception {
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected
final String data = "Data to be read in RO mode";
final String node1 = "/tnode1";
final String node2 = "/tnode2";
zk.create(node1, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
watcher.reset();
qu.shutdown(2);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertEquals("Should be in r-o mode", States.CONNECTEDREADONLY,
zk.getState());
// read operation during r/o mode
String remoteData = new String(zk.getData(node1, false, null));
Assert.assertEquals("Failed to read data in r-o mode", data, remoteData);
try {
Transaction transaction = zk.transaction();
transaction.setData(node1, "no way".getBytes(), -1);
transaction.create(node2, data.getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
transaction.commit();
Assert.fail("Write operation using multi-transaction"
+ " api has succeeded during RO mode");
} catch (NotReadOnlyException e) {
// ok
}
Assert.assertNull("Should have created the znode:" + node2,
zk.exists(node2, false));
}
示例10: utestGet
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
private void utestGet(int port)
throws IOException, InterruptedException, KeeperException
{
ZooKeeper zk =
new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, this);
for (int i = 0; i < 10000; i++) {
Stat stat = new Stat();
zk.getData("/" + i, true, stat);
}
zk.close();
}
示例11: validAuth
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void validAuth() throws Exception {
ZooKeeper zk = createClient();
// any multiple of 5 will do...
zk.addAuthInfo("key", "25".getBytes());
try {
createNodePrintAcl(zk, "/valid", "testValidAuth");
zk.getData("/abc", false, null);
zk.setData("/abc", "testData3".getBytes(), -1);
} catch (KeeperException.AuthFailedException e) {
Assert.fail("test failed :" + e);
} finally {
zk.close();
}
}
示例12: delQuota
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* this method deletes quota for a node.
*
* @param zk the zookeeper client
* @param path the path to delete quota for
* @param bytes true if number of bytes needs to be unset
* @param numNodes true if number of nodes needs to be unset
* @return true if quota deletion is successful
* @throws KeeperException
* @throws IOException
* @throws InterruptedException
*/
public static boolean delQuota(ZooKeeper zk, String path,
boolean bytes, boolean numNodes)
throws KeeperException, IOException, InterruptedException, MalformedPathException {
String parentPath = Quotas.quotaZookeeper + path;
String quotaPath = Quotas.quotaZookeeper + path + "/" +
Quotas.limitNode;
if (zk.exists(quotaPath, false) == null) {
System.out.println("Quota does not exist for " + path);
return true;
}
byte[] data = null;
try {
data = zk.getData(quotaPath, false, new Stat());
} catch (IllegalArgumentException ex) {
throw new MalformedPathException(ex.getMessage());
} catch (KeeperException.NoNodeException ne) {
System.err.println("quota does not exist for " + path);
return true;
}
StatsTrack strack = new StatsTrack(new String(data));
if (bytes && !numNodes) {
strack.setBytes(-1L);
zk.setData(quotaPath, strack.toString().getBytes(), -1);
} else if (!bytes && numNodes) {
strack.setCount(-1);
zk.setData(quotaPath, strack.toString().getBytes(), -1);
} else if (bytes && numNodes) {
// delete till you can find a node with more than
// one child
List<String> children = zk.getChildren(parentPath, false);
/// delete the direct children first
for (String child : children) {
zk.delete(parentPath + "/" + child, -1);
}
// cut the tree till their is more than one child
trimProcQuotas(zk, parentPath);
}
return true;
}
示例13: start
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void start() {
try {
zk = new ZooKeeper(hostPort, 15000, this);
zk.getData("/simpleCase", true, this, null);
if (null != r) {
r.report("Client " + index + " connecting to " + hostPort);
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例14: testAsync
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testAsync() throws Exception
{
ZooKeeper zk = null;
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd".getBytes());
zk.create("/ben", new byte[0], Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT, this, results);
zk.create("/ben/2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results);
zk.delete("/ben", -1, this, results);
zk.create("/ben2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results);
zk.getData("/ben2", false, this, results);
synchronized (results) {
while (results.size() < 5) {
results.wait();
}
}
Assert.assertEquals(0, (int) results.get(0));
Assert.assertEquals(Code.NOAUTH, Code.get(results.get(1)));
Assert.assertEquals(0, (int) results.get(2));
Assert.assertEquals(0, (int) results.get(3));
Assert.assertEquals(0, (int) results.get(4));
} finally {
zk.close();
}
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd2".getBytes());
try {
zk.getData("/ben2", false, new Stat());
Assert.fail("Should have received a permission error");
} catch (KeeperException e) {
Assert.assertEquals(Code.NOAUTH, e.code());
}
} finally {
zk.close();
}
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd".getBytes());
zk.getData("/ben2", false, new Stat());
} finally {
zk.close();
}
}
示例15: testQuota
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testQuota() throws IOException,
InterruptedException, KeeperException, Exception {
final ZooKeeper zk = createClient();
final String path = "/a/b/v";
// making sure setdata works on /
zk.setData("/", "some".getBytes(), -1);
zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b/v/d", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
ZooKeeperMain.createQuota(zk, path, 5L, 10);
// see if its set
String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;
byte[] data = zk.getData(absolutePath, false, new Stat());
StatsTrack st = new StatsTrack(new String(data));
Assert.assertTrue("bytes are set", st.getBytes() == 5L);
Assert.assertTrue("num count is set", st.getCount() == 10);
String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode;
byte[] qdata = zk.getData(statPath, false, new Stat());
StatsTrack qst = new StatsTrack(new String(qdata));
Assert.assertTrue("bytes are set", qst.getBytes() == 8L);
Assert.assertTrue("count is set", qst.getCount() == 2);
//force server to restart and load from snapshot, not txn log
stopServer();
startServer();
stopServer();
startServer();
ZooKeeperServer server = getServer(serverFactory);
Assert.assertNotNull("Quota is still set",
server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) != null);
}