bookkeeperStream.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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.9">
  7. <meta name="Forrest-skin-name" content="pelt">
  8. <title>Streaming with BookKeeper</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_1.3', 'skin/')" id="menu_1.3Title" class="menutitle">BookKeeper</div>
  138. <div id="menu_1.3" class="menuitemgroup">
  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="menuitem">
  149. <a href="bookkeeperProgrammer.html">Programmer's guide</a>
  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="bookkeeperStream.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  202. PDF</a>
  203. </div>
  204. <h1>Streaming with BookKeeper</h1>
  205. <div id="front-matter">
  206. <div id="minitoc-area">
  207. <ul class="minitoc">
  208. <li>
  209. <a href="#bk_StreamSummary">Summary</a>
  210. </li>
  211. <li>
  212. <a href="#bk_LedgerOutputStream">Writing a stream of bytes</a>
  213. </li>
  214. <li>
  215. <a href="#bk_LedgerInputStream">Reading a stream of bytes</a>
  216. </li>
  217. </ul>
  218. </div>
  219. </div>
  220. <a name="bk_StreamSummary"></a>
  221. <h2 class="h3">Summary</h2>
  222. <div class="section">
  223. <p>
  224. When using the BookKeeper API, an application has to split the data to write into entries, each
  225. entry being a byte array. This is natural for many applications. For example, when using BookKeeper
  226. for write-ahead logging, an application typically wants to write the modifications corresponding
  227. to a command or a transaction. Some other applications, however, might not have a natural boundary
  228. for entries, and may prefer to write and read streams of bytes. This is exactly the purpose of the
  229. stream API we have implemented on top of BookKeeper.
  230. </p>
  231. <p>
  232. The stream API is implemented in the package <span class="codefrag computeroutput">Streaming</span>, and it contains two main classes: <span class="codefrag computeroutput">LedgerOutputStream</span> and
  233. <span class="codefrag computeroutput">LedgerInputStream</span>. The class names are indicative of what they do.
  234. </p>
  235. </div>
  236. <a name="bk_LedgerOutputStream"></a>
  237. <h2 class="h3">Writing a stream of bytes</h2>
  238. <div class="section">
  239. <p>
  240. Class <span class="codefrag computeroutput">LedgerOutputStream</span> implements two constructors and five public methods:
  241. </p>
  242. <p>
  243. <span class="codefrag computeroutput">
  244. public LedgerOutputStream(LedgerHandle lh)
  245. </span>
  246. </p>
  247. <p>
  248. where:
  249. </p>
  250. <ul>
  251. <li>
  252. <p>
  253. <span class="codefrag computeroutput">lh</span> is a ledger handle for a previously created and open ledger.
  254. </p>
  255. </li>
  256. </ul>
  257. <p>
  258. <span class="codefrag computeroutput">
  259. public LedgerOutputStream(LedgerHandle lh, int size)
  260. </span>
  261. </p>
  262. <p>
  263. where:
  264. </p>
  265. <ul>
  266. <li>
  267. <p>
  268. <span class="codefrag computeroutput">lh</span> is a ledger handle for a previously created and open ledger.
  269. </p>
  270. </li>
  271. <li>
  272. <p>
  273. <span class="codefrag computeroutput">size</span> is the size of the byte buffer to store written bytes before flushing.
  274. </p>
  275. </li>
  276. </ul>
  277. <p>
  278. <strong>Closing a stream.</strong> This call closes the stream by flushing the write buffer.
  279. </p>
  280. <p>
  281. <span class="codefrag computeroutput">
  282. public void close()
  283. </span>
  284. </p>
  285. <p>
  286. which has no parameters.
  287. </p>
  288. <p>
  289. <strong>Flushing a stream.</strong> This call essentially flushes the write buffer.
  290. </p>
  291. <p>
  292. <span class="codefrag computeroutput">
  293. public synchronized void flush()
  294. </span>
  295. </p>
  296. <p>
  297. which has no parameters.
  298. </p>
  299. <p>
  300. <strong>Writing bytes.</strong> There are three calls for writing bytes to a stream.
  301. </p>
  302. <p>
  303. <span class="codefrag computeroutput">
  304. public synchronized void write(byte[] b)
  305. </span>
  306. </p>
  307. <p>
  308. where:
  309. </p>
  310. <ul>
  311. <li>
  312. <p>
  313. <span class="codefrag computeroutput">b</span> is an array of bytes to write.
  314. </p>
  315. </li>
  316. </ul>
  317. <p>
  318. <span class="codefrag computeroutput">
  319. public synchronized void write(byte[] b, int off, int len)
  320. </span>
  321. </p>
  322. <p>
  323. where:
  324. </p>
  325. <ul>
  326. <li>
  327. <p>
  328. <span class="codefrag computeroutput">b</span> is an array of bytes to write.
  329. </p>
  330. </li>
  331. <li>
  332. <p>
  333. <span class="codefrag computeroutput">off</span> is a buffer offset.
  334. </p>
  335. </li>
  336. <li>
  337. <p>
  338. <span class="codefrag computeroutput">len</span> is the length to write.
  339. </p>
  340. </li>
  341. </ul>
  342. <p>
  343. <span class="codefrag computeroutput">
  344. public synchronized void write(int b)
  345. </span>
  346. </p>
  347. <p>
  348. where:
  349. </p>
  350. <ul>
  351. <li>
  352. <p>
  353. <span class="codefrag computeroutput">b</span> contains a byte to write. The method writes the least significant byte of the integer four bytes.
  354. </p>
  355. </li>
  356. </ul>
  357. </div>
  358. <a name="bk_LedgerInputStream"></a>
  359. <h2 class="h3">Reading a stream of bytes</h2>
  360. <div class="section">
  361. <p>
  362. Class <span class="codefrag computeroutput">LedgerOutputStream</span> implements two constructors and four public methods:
  363. </p>
  364. <p>
  365. <span class="codefrag computeroutput">
  366. public LedgerInputStream(LedgerHandle lh)
  367. throws BKException, InterruptedException
  368. </span>
  369. </p>
  370. <p>
  371. where:
  372. </p>
  373. <ul>
  374. <li>
  375. <p>
  376. <span class="codefrag computeroutput">lh</span> is a ledger handle for a previously created and open ledger.
  377. </p>
  378. </li>
  379. </ul>
  380. <p>
  381. <span class="codefrag computeroutput">
  382. public LedgerInputStream(LedgerHandle lh, int size)
  383. throws BKException, InterruptedException
  384. </span>
  385. </p>
  386. <p>
  387. where:
  388. </p>
  389. <ul>
  390. <li>
  391. <p>
  392. <span class="codefrag computeroutput">lh</span> is a ledger handle for a previously created and open ledger.
  393. </p>
  394. </li>
  395. <li>
  396. <p>
  397. <span class="codefrag computeroutput">size</span> is the size of the byte buffer to store bytes that the application
  398. will eventually read.
  399. </p>
  400. </li>
  401. </ul>
  402. <p>
  403. <strong>Closing.</strong> There is one call to close an input stream, but the call
  404. is currently empty and the application is responsible for closing the ledger handle.
  405. </p>
  406. <p>
  407. <span class="codefrag computeroutput">
  408. public void close()
  409. </span>
  410. </p>
  411. <p>
  412. which has no parameters.
  413. </p>
  414. <p>
  415. <strong>Reading.</strong> There are three calls to read from the stream.
  416. </p>
  417. <p>
  418. <span class="codefrag computeroutput">
  419. public synchronized int read()
  420. throws IOException
  421. </span>
  422. </p>
  423. <p>
  424. which has no parameters.
  425. </p>
  426. <p>
  427. <span class="codefrag computeroutput">
  428. public synchronized int read(byte[] b)
  429. throws IOException
  430. </span>
  431. </p>
  432. <p>
  433. where:
  434. </p>
  435. <ul>
  436. <li>
  437. <p>
  438. <span class="codefrag computeroutput">b</span> is a byte array to write to.
  439. </p>
  440. </li>
  441. </ul>
  442. <p>
  443. <span class="codefrag computeroutput">
  444. public synchronized int read(byte[] b, int off, int len)
  445. throws IOException
  446. </span>
  447. </p>
  448. <p>
  449. where:
  450. </p>
  451. <ul>
  452. <li>
  453. <p>
  454. <span class="codefrag computeroutput">b</span> is a byte array to write to.
  455. </p>
  456. </li>
  457. <li>
  458. <p>
  459. <span class="codefrag computeroutput">off</span> is an offset for byte array <span class="codefrag computeroutput">b</span>.
  460. </p>
  461. </li>
  462. <li>
  463. <p>
  464. <span class="codefrag computeroutput">len</span> is the length in bytes to write to <span class="codefrag computeroutput">b</span>.
  465. </p>
  466. </li>
  467. </ul>
  468. </div>
  469. <p align="right">
  470. <font size="-2"></font>
  471. </p>
  472. </div>
  473. <!--+
  474. |end content
  475. +-->
  476. <div class="clearboth">&nbsp;</div>
  477. </div>
  478. <div id="footer">
  479. <!--+
  480. |start bottomstrip
  481. +-->
  482. <div class="lastmodified">
  483. <script type="text/javascript"><!--
  484. document.write("Last Published: " + document.lastModified);
  485. // --></script>
  486. </div>
  487. <div class="copyright">
  488. Copyright &copy;
  489. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  490. </div>
  491. <!--+
  492. |end bottomstrip
  493. +-->
  494. </div>
  495. </body>
  496. </html>