bookkeeperProgrammer.html 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <meta content="Apache Forrest" name="Generator">
  6. <meta name="Forrest-version" content="0.8">
  7. <meta name="Forrest-skin-name" content="pelt">
  8. <title>BookKeeper Getting Started Guide</title>
  9. <link type="text/css" href="skin/basic.css" rel="stylesheet">
  10. <link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
  11. <link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
  12. <link type="text/css" href="skin/profile.css" rel="stylesheet">
  13. <script src="skin/getBlank.js" language="javascript" type="text/javascript"></script><script src="skin/getMenu.js" language="javascript" type="text/javascript"></script><script src="skin/fontsize.js" language="javascript" type="text/javascript"></script>
  14. <link rel="shortcut icon" href="images/favicon.ico">
  15. </head>
  16. <body onload="init()">
  17. <script type="text/javascript">ndeSetTextSize();</script>
  18. <div id="top">
  19. <!--+
  20. |breadtrail
  21. +-->
  22. <div class="breadtrail">
  23. <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://hadoop.apache.org/">Hadoop</a> &gt; <a href="http://hadoop.apache.org/zookeeper/">ZooKeeper</a><script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
  24. </div>
  25. <!--+
  26. |header
  27. +-->
  28. <div class="header">
  29. <!--+
  30. |start group logo
  31. +-->
  32. <div class="grouplogo">
  33. <a href="http://hadoop.apache.org/"><img class="logoImage" alt="Hadoop" src="images/hadoop-logo.jpg" title="Apache Hadoop"></a>
  34. </div>
  35. <!--+
  36. |end group logo
  37. +-->
  38. <!--+
  39. |start Project Logo
  40. +-->
  41. <div class="projectlogo">
  42. <a href="http://hadoop.apache.org/zookeeper/"><img class="logoImage" alt="ZooKeeper" src="images/zookeeper_small.gif" title="ZooKeeper: distributed coordination"></a>
  43. </div>
  44. <!--+
  45. |end Project Logo
  46. +-->
  47. <!--+
  48. |start Search
  49. +-->
  50. <div class="searchbox">
  51. <form action="http://www.google.com/search" method="get" class="roundtopsmall">
  52. <input value="hadoop.apache.org" name="sitesearch" type="hidden"><input onFocus="getBlank (this, 'Search the site with google');" size="25" name="q" id="query" type="text" value="Search the site with google">&nbsp;
  53. <input name="Search" value="Search" type="submit">
  54. </form>
  55. </div>
  56. <!--+
  57. |end search
  58. +-->
  59. <!--+
  60. |start Tabs
  61. +-->
  62. <ul id="tabs">
  63. <li>
  64. <a class="unselected" href="http://hadoop.apache.org/zookeeper/">Project</a>
  65. </li>
  66. <li>
  67. <a class="unselected" href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
  68. </li>
  69. <li class="current">
  70. <a class="selected" href="index.html">ZooKeeper 3.4 Documentation</a>
  71. </li>
  72. </ul>
  73. <!--+
  74. |end Tabs
  75. +-->
  76. </div>
  77. </div>
  78. <div id="main">
  79. <div id="publishedStrip">
  80. <!--+
  81. |start Subtabs
  82. +-->
  83. <div id="level2tabs"></div>
  84. <!--+
  85. |end Endtabs
  86. +-->
  87. <script type="text/javascript"><!--
  88. document.write("Last Published: " + document.lastModified);
  89. // --></script>
  90. </div>
  91. <!--+
  92. |breadtrail
  93. +-->
  94. <div class="breadtrail">
  95. &nbsp;
  96. </div>
  97. <!--+
  98. |start Menu, mainarea
  99. +-->
  100. <!--+
  101. |start Menu
  102. +-->
  103. <div id="menu">
  104. <div onclick="SwitchMenu('menu_1.1', 'skin/')" id="menu_1.1Title" class="menutitle">Overview</div>
  105. <div id="menu_1.1" class="menuitemgroup">
  106. <div class="menuitem">
  107. <a href="index.html">Welcome</a>
  108. </div>
  109. <div class="menuitem">
  110. <a href="zookeeperOver.html">Overview</a>
  111. </div>
  112. <div class="menuitem">
  113. <a href="zookeeperStarted.html">Getting Started</a>
  114. </div>
  115. <div class="menuitem">
  116. <a href="releasenotes.html">Release Notes</a>
  117. </div>
  118. </div>
  119. <div onclick="SwitchMenu('menu_1.2', 'skin/')" id="menu_1.2Title" class="menutitle">Developer</div>
  120. <div id="menu_1.2" class="menuitemgroup">
  121. <div class="menuitem">
  122. <a href="api/index.html">API Docs</a>
  123. </div>
  124. <div class="menuitem">
  125. <a href="zookeeperProgrammers.html">Programmer's Guide</a>
  126. </div>
  127. <div class="menuitem">
  128. <a href="javaExample.html">Java Example</a>
  129. </div>
  130. <div class="menuitem">
  131. <a href="zookeeperTutorial.html">Barrier and Queue Tutorial</a>
  132. </div>
  133. <div class="menuitem">
  134. <a href="recipes.html">Recipes</a>
  135. </div>
  136. </div>
  137. <div onclick="SwitchMenu('menu_selected_1.3', 'skin/')" id="menu_selected_1.3Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">BookKeeper</div>
  138. <div id="menu_selected_1.3" class="selectedmenuitemgroup" style="display: block;">
  139. <div class="menuitem">
  140. <a href="bookkeeperStarted.html">Getting started</a>
  141. </div>
  142. <div class="menuitem">
  143. <a href="bookkeeperOverview.html">Overview</a>
  144. </div>
  145. <div class="menuitem">
  146. <a href="bookkeeperConfig.html">Setup guide</a>
  147. </div>
  148. <div class="menupage">
  149. <div class="menupagetitle">Programmer's guide</div>
  150. </div>
  151. </div>
  152. <div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Admin &amp; Ops</div>
  153. <div id="menu_1.4" class="menuitemgroup">
  154. <div class="menuitem">
  155. <a href="zookeeperAdmin.html">Administrator's Guide</a>
  156. </div>
  157. <div class="menuitem">
  158. <a href="zookeeperQuotas.html">Quota Guide</a>
  159. </div>
  160. <div class="menuitem">
  161. <a href="zookeeperJMX.html">JMX</a>
  162. </div>
  163. <div class="menuitem">
  164. <a href="zookeeperObservers.html">Observers Guide</a>
  165. </div>
  166. </div>
  167. <div onclick="SwitchMenu('menu_1.5', 'skin/')" id="menu_1.5Title" class="menutitle">Contributor</div>
  168. <div id="menu_1.5" class="menuitemgroup">
  169. <div class="menuitem">
  170. <a href="zookeeperInternals.html">ZooKeeper Internals</a>
  171. </div>
  172. </div>
  173. <div onclick="SwitchMenu('menu_1.6', 'skin/')" id="menu_1.6Title" class="menutitle">Miscellaneous</div>
  174. <div id="menu_1.6" class="menuitemgroup">
  175. <div class="menuitem">
  176. <a href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
  177. </div>
  178. <div class="menuitem">
  179. <a href="http://wiki.apache.org/hadoop/ZooKeeper/FAQ">FAQ</a>
  180. </div>
  181. <div class="menuitem">
  182. <a href="http://hadoop.apache.org/zookeeper/mailing_lists.html">Mailing Lists</a>
  183. </div>
  184. </div>
  185. <div id="credit"></div>
  186. <div id="roundbottom">
  187. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  188. <!--+
  189. |alternative credits
  190. +-->
  191. <div id="credit2"></div>
  192. </div>
  193. <!--+
  194. |end Menu
  195. +-->
  196. <!--+
  197. |start content
  198. +-->
  199. <div id="content">
  200. <div title="Portable Document Format" class="pdflink">
  201. <a class="dida" href="bookkeeperProgrammer.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  202. PDF</a>
  203. </div>
  204. <h1>BookKeeper Getting Started Guide</h1>
  205. <div id="minitoc-area">
  206. <ul class="minitoc">
  207. <li>
  208. <a href="#bk_GettingStarted">Programming with BookKeeper</a>
  209. <ul class="minitoc">
  210. <li>
  211. <a href="#bk_instance"> Instantiating BookKeeper.</a>
  212. </li>
  213. <li>
  214. <a href="#bk_createLedger"> Creating a ledger. </a>
  215. </li>
  216. <li>
  217. <a href="#bk_writeLedger"> Adding entries to a ledger. </a>
  218. </li>
  219. <li>
  220. <a href="#bk_closeLedger"> Closing a ledger. </a>
  221. </li>
  222. <li>
  223. <a href="#bk_openLedger"> Opening a ledger. </a>
  224. </li>
  225. <li>
  226. <a href="#bk_readLedger"> Reading from ledger </a>
  227. </li>
  228. <li>
  229. <a href="#bk_deleteLedger"> Deleting a ledger </a>
  230. </li>
  231. </ul>
  232. </li>
  233. </ul>
  234. </div>
  235. <a name="N10009"></a><a name="bk_GettingStarted"></a>
  236. <h2 class="h3">Programming with BookKeeper</h2>
  237. <div class="section">
  238. <ul>
  239. <li>
  240. <p>
  241. <a href="#bk_instance"> Instantiating BookKeeper.</a>
  242. </p>
  243. </li>
  244. <li>
  245. <p>
  246. <a href="#bk_createLedger"> Creating a ledger. </a>
  247. </p>
  248. </li>
  249. <li>
  250. <p>
  251. <a href="#bk_writeLedger"> Adding entries to a ledger. </a>
  252. </p>
  253. </li>
  254. <li>
  255. <p>
  256. <a href="#bk_closeLedger"> Closing a ledger. </a>
  257. </p>
  258. </li>
  259. <li>
  260. <p>
  261. <a href="#bk_openLedger"> Opening a ledger. </a>
  262. </p>
  263. </li>
  264. <li>
  265. <p>
  266. <a href="#bk_readLedger"> Reading from ledger </a>
  267. </p>
  268. </li>
  269. <li>
  270. <p>
  271. <a href="#bk_deleteLedger"> Deleting a ledger </a>
  272. </p>
  273. </li>
  274. </ul>
  275. <a name="N1004A"></a><a name="bk_instance"></a>
  276. <h3 class="h4"> Instantiating BookKeeper.</h3>
  277. <p>
  278. The first step to use BookKeeper is to instantiate a BookKeeper object:
  279. </p>
  280. <p>
  281. <span class="codefrag computeroutput">
  282. org.apache.bookkeeper.BookKeeper
  283. </span>
  284. </p>
  285. <p>
  286. There are three BookKeeper constructors:
  287. </p>
  288. <p>
  289. <span class="codefrag computeroutput">
  290. public BookKeeper(String servers)
  291. throws KeeperException, IOException
  292. </span>
  293. </p>
  294. <p>
  295. where:
  296. </p>
  297. <ul>
  298. <li>
  299. <p>
  300. <span class="codefrag computeroutput">servers</span> is a comma-separated list of ZooKeeper servers.
  301. </p>
  302. </li>
  303. </ul>
  304. <p>
  305. <span class="codefrag computeroutput">
  306. public BookKeeper(ZooKeeper zk)
  307. throws InterruptedException, KeeperException
  308. </span>
  309. </p>
  310. <p>
  311. where:
  312. </p>
  313. <ul>
  314. <li>
  315. <p>
  316. <span class="codefrag computeroutput">zk</span> is a ZooKeeper object. This constructor is useful when
  317. the application also using ZooKeeper and wants to have a single instance of ZooKeeper.
  318. </p>
  319. </li>
  320. </ul>
  321. <p>
  322. <span class="codefrag computeroutput">
  323. public BookKeeper(ZooKeeper zk, ClientSocketChannelFactory channelFactory)
  324. throws InterruptedException, KeeperException
  325. </span>
  326. </p>
  327. <p>
  328. where:
  329. </p>
  330. <ul>
  331. <li>
  332. <p>
  333. <span class="codefrag computeroutput">zk</span> is a ZooKeeper object. This constructor is useful when
  334. the application also using ZooKeeper and wants to have a single instance of ZooKeeper.
  335. </p>
  336. </li>
  337. <li>
  338. <p>
  339. <span class="codefrag computeroutput">channelFactory</span> is a netty channel object
  340. (<span class="codefrag computeroutput">org.jboss.netty.channel.socket</span>).
  341. </p>
  342. </li>
  343. </ul>
  344. <a name="N100B1"></a><a name="bk_createLedger"></a>
  345. <h3 class="h4"> Creating a ledger. </h3>
  346. <p> Before writing entries to BookKeeper, it is necessary to create a ledger.
  347. With the current BookKeeper API, it is possible to create a ledger both synchronously
  348. or asynchronously. The following methods belong
  349. to <span class="codefrag computeroutput">org.apache.bookkeeper.client.BookKeeper</span>.
  350. </p>
  351. <p>
  352. <strong>Synchronous call:</strong>
  353. </p>
  354. <p>
  355. <span class="codefrag computeroutput">
  356. public LedgerHandle createLedger(int ensSize, int qSize, DigestType type, byte passwd[])
  357. throws KeeperException, InterruptedException,
  358. IOException, BKException
  359. </span>
  360. </p>
  361. <p>
  362. where:
  363. </p>
  364. <ul>
  365. <li>
  366. <p>
  367. <span class="codefrag computeroutput">ensSize</span> is the number of bookies (ensemble size);
  368. </p>
  369. </li>
  370. <li>
  371. <p>
  372. <span class="codefrag computeroutput">qSize</span> is the write quorum size;
  373. </p>
  374. </li>
  375. <li>
  376. <p>
  377. <span class="codefrag computeroutput">type</span> is the type of digest used with entries: either MAC or CRC32.
  378. </p>
  379. </li>
  380. <li>
  381. <p>
  382. <span class="codefrag computeroutput">passwd</span> is a password that authorizes the client to write to the
  383. ledger being created.
  384. </p>
  385. </li>
  386. </ul>
  387. <p>
  388. All further operations on a ledger are invoked through the <span class="codefrag computeroutput">LedgerHandle</span>
  389. object returned.
  390. </p>
  391. <p>
  392. As a convenience, we provide a <span class="codefrag computeroutput">createLedger</span> with default parameters (3,2,VERIFIABLE),
  393. and the only two input parameters it requires are a digest type and a password.
  394. </p>
  395. <p>
  396. <strong>Asynchronous call:</strong>
  397. </p>
  398. <p>
  399. <span class="codefrag computeroutput">
  400. public void asyncCreateLedger(int ensSize,
  401. int qSize,
  402. DigestType type,
  403. byte passwd[],
  404. CreateCallback cb,
  405. Object ctx
  406. )
  407. </span>
  408. </p>
  409. <p>
  410. The parameters are the same of the synchronous version, with the
  411. exception of <span class="codefrag computeroutput">cb</span> and <span class="codefrag computeroutput">ctx</span>. <span class="codefrag computeroutput">CreateCallback</span>
  412. is an interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
  413. a class implementing it has to implement a method called <span class="codefrag computeroutput">createComplete</span>
  414. that has the following signature:
  415. </p>
  416. <p>
  417. <span class="codefrag computeroutput">
  418. void createComplete(int rc, LedgerHandle lh, Object ctx);
  419. </span>
  420. </p>
  421. <p>
  422. where:
  423. </p>
  424. <ul>
  425. <li>
  426. <p>
  427. <span class="codefrag computeroutput">rc</span> is a return code (please refer to <span class="codefrag computeroutput">org.apache.bookeeper.client.BKException</span> for a list);
  428. </p>
  429. </li>
  430. <li>
  431. <p>
  432. <span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
  433. </p>
  434. </li>
  435. <li>
  436. <p>
  437. <span class="codefrag computeroutput">ctx</span> is a control object for accountability purposes. It can be essentially any object the application is happy with.
  438. </p>
  439. </li>
  440. </ul>
  441. <p>
  442. The <span class="codefrag computeroutput">ctx</span> object passed as a parameter to the call to create a ledger
  443. is the one same returned in the callback.
  444. </p>
  445. <a name="N10166"></a><a name="bk_writeLedger"></a>
  446. <h3 class="h4"> Adding entries to a ledger. </h3>
  447. <p>
  448. Once we have a ledger handle <span class="codefrag computeroutput">lh</span> obtained through a call to create a ledger, we
  449. can start writing entries. As with creating ledgers, we can write both synchronously and
  450. asynchronously. The following methods belong
  451. to <span class="codefrag computeroutput">org.apache.bookkeeper.client.LedgerHandle</span>.
  452. </p>
  453. <p>
  454. <strong>Synchronous call:</strong>
  455. </p>
  456. <p>
  457. <span class="codefrag computeroutput">
  458. public long addEntry(byte[] data)
  459. throws InterruptedException
  460. </span>
  461. </p>
  462. <p>
  463. where:
  464. </p>
  465. <ul>
  466. <li>
  467. <p>
  468. <span class="codefrag computeroutput">data</span> is a byte array;
  469. </p>
  470. </li>
  471. </ul>
  472. <p>
  473. A call to <span class="codefrag computeroutput">addEntry</span> returns the status of the operation (please refer to <span class="codefrag computeroutput">org.apache.bookeeper.client.BKDefs</span> for a list);
  474. </p>
  475. <p>
  476. <strong>Asynchronous call:</strong>
  477. </p>
  478. <p>
  479. <span class="codefrag computeroutput">
  480. public void asyncAddEntry(byte[] data, AddCallback cb, Object ctx)
  481. </span>
  482. </p>
  483. <p>
  484. It also takes a byte array as the sequence of bytes to be stored as an entry. Additionaly, it takes
  485. a callback object <span class="codefrag computeroutput">cb</span> and a control object <span class="codefrag computeroutput">ctx</span>. The callback object must implement
  486. the <span class="codefrag computeroutput">AddCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
  487. a class implementing it has to implement a method called <span class="codefrag computeroutput">addComplete</span>
  488. that has the following signature:
  489. </p>
  490. <p>
  491. <span class="codefrag computeroutput">
  492. void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx);
  493. </span>
  494. </p>
  495. <p>
  496. where:
  497. </p>
  498. <ul>
  499. <li>
  500. <p>
  501. <span class="codefrag computeroutput">rc</span> is a return code (please refer to <span class="codefrag computeroutput">org.apache.bookeeper.client.BKDefs</span> for a list);
  502. </p>
  503. </li>
  504. <li>
  505. <p>
  506. <span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
  507. </p>
  508. </li>
  509. <li>
  510. <p>
  511. <span class="codefrag computeroutput">entryId</span> is the identifier of entry associated with this request;
  512. </p>
  513. </li>
  514. <li>
  515. <p>
  516. <span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes. It can be any object the application is happy with.
  517. </p>
  518. </li>
  519. </ul>
  520. <a name="N10201"></a><a name="bk_closeLedger"></a>
  521. <h3 class="h4"> Closing a ledger. </h3>
  522. <p>
  523. Once a client is done writing, it closes the ledger. The following methods belong
  524. to <span class="codefrag computeroutput">org.apache.bookkeeper.client.LedgerHandle</span>.
  525. </p>
  526. <p>
  527. <strong>Synchronous close:</strong>
  528. </p>
  529. <p>
  530. <span class="codefrag computeroutput">
  531. public void close()
  532. throws InterruptedException
  533. </span>
  534. </p>
  535. <p>
  536. It takes no input parameters.
  537. </p>
  538. <p>
  539. <strong>Asynchronous close:</strong>
  540. </p>
  541. <p>
  542. <span class="codefrag computeroutput">
  543. public void asyncClose(CloseCallback cb, Object ctx)
  544. throws InterruptedException
  545. </span>
  546. </p>
  547. <p>
  548. It takes a callback object <span class="codefrag computeroutput">cb</span> and a control object <span class="codefrag computeroutput">ctx</span>. The callback object must implement
  549. the <span class="codefrag computeroutput">CloseCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
  550. a class implementing it has to implement a method called <span class="codefrag computeroutput">closeComplete</span>
  551. that has the following signature:
  552. </p>
  553. <p>
  554. <span class="codefrag computeroutput">
  555. void closeComplete(int rc, LedgerHandle lh, Object ctx)
  556. </span>
  557. </p>
  558. <p>
  559. where:
  560. </p>
  561. <ul>
  562. <li>
  563. <p>
  564. <span class="codefrag computeroutput">rc</span> is a return code (please refer to <span class="codefrag computeroutput">org.apache.bookeeper.client.BKDefs</span> for a list);
  565. </p>
  566. </li>
  567. <li>
  568. <p>
  569. <span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
  570. </p>
  571. </li>
  572. <li>
  573. <p>
  574. <span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes.
  575. </p>
  576. </li>
  577. </ul>
  578. <a name="N10276"></a><a name="bk_openLedger"></a>
  579. <h3 class="h4"> Opening a ledger. </h3>
  580. <p>
  581. To read from a ledger, a client must open it first. The following methods belong
  582. to <span class="codefrag computeroutput">org.apache.bookkeeper.client.BookKeeper</span>.
  583. </p>
  584. <p>
  585. <strong>Synchronous open:</strong>
  586. </p>
  587. <p>
  588. <span class="codefrag computeroutput">
  589. public LedgerHandle openLedger(long lId, DigestType type, byte passwd[])
  590. throws InterruptedException, BKException
  591. </span>
  592. </p>
  593. <ul>
  594. <li>
  595. <p>
  596. <span class="codefrag computeroutput">ledgerId</span> is the ledger identifier;
  597. </p>
  598. </li>
  599. <li>
  600. <p>
  601. <span class="codefrag computeroutput">type</span> is the type of digest used with entries: either MAC or CRC32.
  602. </p>
  603. </li>
  604. <li>
  605. <p>
  606. <span class="codefrag computeroutput">passwd</span> is a password to access the ledger (used only in the case of <span class="codefrag computeroutput">VERIFIABLE</span> ledgers);
  607. </p>
  608. </li>
  609. </ul>
  610. <p>
  611. <strong>Asynchronous open:</strong>
  612. </p>
  613. <p>
  614. <span class="codefrag computeroutput">
  615. public void asyncOpenLedger(long lId, DigestType type, byte passwd[], OpenCallback cb, Object ctx)
  616. </span>
  617. </p>
  618. <p>
  619. It also takes a a ledger identifier and a password. Additionaly, it takes a callback object
  620. <span class="codefrag computeroutput">cb</span> and a control object <span class="codefrag computeroutput">ctx</span>. The callback object must implement
  621. the <span class="codefrag computeroutput">OpenCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
  622. a class implementing it has to implement a method called <span class="codefrag computeroutput">openComplete</span>
  623. that has the following signature:
  624. </p>
  625. <p>
  626. <span class="codefrag computeroutput">
  627. public void openComplete(int rc, LedgerHandle lh, Object ctx)
  628. </span>
  629. </p>
  630. <p>
  631. where:
  632. </p>
  633. <ul>
  634. <li>
  635. <p>
  636. <span class="codefrag computeroutput">rc</span> is a return code (please refer to <span class="codefrag computeroutput">org.apache.bookeeper.client.BKDefs</span> for a list);
  637. </p>
  638. </li>
  639. <li>
  640. <p>
  641. <span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
  642. </p>
  643. </li>
  644. <li>
  645. <p>
  646. <span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes.
  647. </p>
  648. </li>
  649. </ul>
  650. <a name="N1030D"></a><a name="bk_readLedger"></a>
  651. <h3 class="h4"> Reading from ledger </h3>
  652. <p>
  653. Read calls may request one or more consecutive entries. The following methods belong
  654. to <span class="codefrag computeroutput">org.apache.bookkeeper.client.LedgerHandle</span>.
  655. </p>
  656. <p>
  657. <strong>Synchronous read:</strong>
  658. </p>
  659. <p>
  660. <span class="codefrag computeroutput">
  661. public Enumeration&lt;LedgerEntry&gt; readEntries(long firstEntry, long lastEntry)
  662. throws InterruptedException, BKException
  663. </span>
  664. </p>
  665. <ul>
  666. <li>
  667. <p>
  668. <span class="codefrag computeroutput">firstEntry</span> is the identifier of the first entry in the sequence of entries to read;
  669. </p>
  670. </li>
  671. <li>
  672. <p>
  673. <span class="codefrag computeroutput">lastEntry</span> is the identifier of the last entry in the sequence of entries to read.
  674. </p>
  675. </li>
  676. </ul>
  677. <p>
  678. <strong>Asynchronous read:</strong>
  679. </p>
  680. <p>
  681. <span class="codefrag computeroutput">
  682. public void asyncReadEntries(long firstEntry,
  683. long lastEntry, ReadCallback cb, Object ctx)
  684. throws BKException, InterruptedException
  685. </span>
  686. </p>
  687. <p>
  688. It also takes a first and a last entry identifiers. Additionaly, it takes a callback object
  689. <span class="codefrag computeroutput">cb</span> and a control object <span class="codefrag computeroutput">ctx</span>. The callback object must implement
  690. the <span class="codefrag computeroutput">ReadCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
  691. a class implementing it has to implement a method called <span class="codefrag computeroutput">readComplete</span>
  692. that has the following signature:
  693. </p>
  694. <p>
  695. <span class="codefrag computeroutput">
  696. void readComplete(int rc, LedgerHandle lh, Enumeration&lt;LedgerEntry&gt; seq, Object ctx)
  697. </span>
  698. </p>
  699. <p>
  700. where:
  701. </p>
  702. <ul>
  703. <li>
  704. <p>
  705. <span class="codefrag computeroutput">rc</span> is a return code (please refer to <span class="codefrag computeroutput">org.apache.bookeeper.client.BKDefs</span> for a list);
  706. </p>
  707. </li>
  708. <li>
  709. <p>
  710. <span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
  711. </p>
  712. </li>
  713. <li>
  714. <p>
  715. <span class="codefrag computeroutput">seq</span> is a <span class="codefrag computeroutput">Enumeration&lt;LedgerEntry&gt; </span> object to containing the list of entries requested;
  716. </p>
  717. </li>
  718. <li>
  719. <p>
  720. <span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes.
  721. </p>
  722. </li>
  723. </ul>
  724. <a name="N103A4"></a><a name="bk_deleteLedger"></a>
  725. <h3 class="h4"> Deleting a ledger </h3>
  726. <p>
  727. Once a client is done with a ledger and is sure that nobody will ever need to read from it again, they can delete the ledger.
  728. The following methods belong to <span class="codefrag computeroutput">org.apache.bookkeeper.client.BookKeeper</span>.
  729. </p>
  730. <p>
  731. <strong>Synchronous delete:</strong>
  732. </p>
  733. <p>
  734. <span class="codefrag computeroutput">
  735. public void deleteLedger(long lId) throws InterruptedException, BKException
  736. </span>
  737. </p>
  738. <ul>
  739. <li>
  740. <p>
  741. <span class="codefrag computeroutput">lId</span> is the ledger identifier;
  742. </p>
  743. </li>
  744. </ul>
  745. <p>
  746. <strong>Asynchronous delete:</strong>
  747. </p>
  748. <p>
  749. <span class="codefrag computeroutput">
  750. public void asyncDeleteLedger(long lId, DeleteCallback cb, Object ctx)
  751. </span>
  752. </p>
  753. <p>
  754. It takes a ledger identifier. Additionally, it takes a callback object
  755. <span class="codefrag computeroutput">cb</span> and a control object <span class="codefrag computeroutput">ctx</span>. The callback object must implement
  756. the <span class="codefrag computeroutput">DeleteCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
  757. a class implementing it has to implement a method called <span class="codefrag computeroutput">deleteComplete</span>
  758. that has the following signature:
  759. </p>
  760. <p>
  761. <span class="codefrag computeroutput">
  762. void deleteComplete(int rc, Object ctx)
  763. </span>
  764. </p>
  765. <p>
  766. where:
  767. </p>
  768. <ul>
  769. <li>
  770. <p>
  771. <span class="codefrag computeroutput">rc</span> is a return code (please refer to <span class="codefrag computeroutput">org.apache.bookeeper.client.BKDefs</span> for a list);
  772. </p>
  773. </li>
  774. <li>
  775. <p>
  776. <span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes.
  777. </p>
  778. </li>
  779. </ul>
  780. </div>
  781. <p align="right">
  782. <font size="-2"></font>
  783. </p>
  784. </div>
  785. <!--+
  786. |end content
  787. +-->
  788. <div class="clearboth">&nbsp;</div>
  789. </div>
  790. <div id="footer">
  791. <!--+
  792. |start bottomstrip
  793. +-->
  794. <div class="lastmodified">
  795. <script type="text/javascript"><!--
  796. document.write("Last Published: " + document.lastModified);
  797. // --></script>
  798. </div>
  799. <div class="copyright">
  800. Copyright &copy;
  801. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  802. </div>
  803. <!--+
  804. |end bottomstrip
  805. +-->
  806. </div>
  807. </body>
  808. </html>