forked from apache/zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbookkeeperStream.html
612 lines (496 loc) · 13.5 KB
/
bookkeeperStream.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="Apache Forrest" name="Generator">
<meta name="Forrest-version" content="0.9">
<meta name="Forrest-skin-name" content="pelt">
<title>Streaming with BookKeeper</title>
<link type="text/css" href="skin/basic.css" rel="stylesheet">
<link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
<link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
<link type="text/css" href="skin/profile.css" rel="stylesheet">
<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>
<link rel="shortcut icon" href="images/favicon.ico">
</head>
<body onload="init()">
<script type="text/javascript">ndeSetTextSize();</script>
<div id="top">
<!--+
|breadtrail
+-->
<div class="breadtrail">
<a href="http://www.apache.org/">Apache</a> > <a href="http://zookeeper.apache.org/">ZooKeeper</a> > <a href="http://zookeeper.apache.org/">ZooKeeper</a><script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
</div>
<!--+
|header
+-->
<div class="header">
<!--+
|start group logo
+-->
<div class="grouplogo">
<a href="http://hadoop.apache.org/"><img class="logoImage" alt="Hadoop" src="images/hadoop-logo.jpg" title="Apache Hadoop"></a>
</div>
<!--+
|end group logo
+-->
<!--+
|start Project Logo
+-->
<div class="projectlogo">
<a href="http://zookeeper.apache.org/"><img class="logoImage" alt="ZooKeeper" src="images/zookeeper_small.gif" title="ZooKeeper: distributed coordination"></a>
</div>
<!--+
|end Project Logo
+-->
<!--+
|start Search
+-->
<div class="searchbox">
<form action="http://www.google.com/search" method="get" class="roundtopsmall">
<input value="zookeeper.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">
<input name="Search" value="Search" type="submit">
</form>
</div>
<!--+
|end search
+-->
<!--+
|start Tabs
+-->
<ul id="tabs">
<li>
<a class="unselected" href="http://zookeeper.apache.org/">Project</a>
</li>
<li>
<a class="unselected" href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/">Wiki</a>
</li>
<li class="current">
<a class="selected" href="index.html">ZooKeeper 3.4 Documentation</a>
</li>
</ul>
<!--+
|end Tabs
+-->
</div>
</div>
<div id="main">
<div id="publishedStrip">
<!--+
|start Subtabs
+-->
<div id="level2tabs"></div>
<!--+
|end Endtabs
+-->
<script type="text/javascript"><!--
document.write("Last Published: " + document.lastModified);
// --></script>
</div>
<!--+
|breadtrail
+-->
<div class="breadtrail">
</div>
<!--+
|start Menu, mainarea
+-->
<!--+
|start Menu
+-->
<div id="menu">
<div onclick="SwitchMenu('menu_1.1', 'skin/')" id="menu_1.1Title" class="menutitle">Overview</div>
<div id="menu_1.1" class="menuitemgroup">
<div class="menuitem">
<a href="index.html">Welcome</a>
</div>
<div class="menuitem">
<a href="zookeeperOver.html">Overview</a>
</div>
<div class="menuitem">
<a href="zookeeperStarted.html">Getting Started</a>
</div>
<div class="menuitem">
<a href="releasenotes.html">Release Notes</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.2', 'skin/')" id="menu_1.2Title" class="menutitle">Developer</div>
<div id="menu_1.2" class="menuitemgroup">
<div class="menuitem">
<a href="api/index.html">API Docs</a>
</div>
<div class="menuitem">
<a href="zookeeperProgrammers.html">Programmer's Guide</a>
</div>
<div class="menuitem">
<a href="javaExample.html">Java Example</a>
</div>
<div class="menuitem">
<a href="zookeeperTutorial.html">Barrier and Queue Tutorial</a>
</div>
<div class="menuitem">
<a href="recipes.html">Recipes</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.3', 'skin/')" id="menu_1.3Title" class="menutitle">BookKeeper</div>
<div id="menu_1.3" class="menuitemgroup">
<div class="menuitem">
<a href="bookkeeperStarted.html">Getting started</a>
</div>
<div class="menuitem">
<a href="bookkeeperOverview.html">Overview</a>
</div>
<div class="menuitem">
<a href="bookkeeperConfig.html">Setup guide</a>
</div>
<div class="menuitem">
<a href="bookkeeperProgrammer.html">Programmer's guide</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Admin & Ops</div>
<div id="menu_1.4" class="menuitemgroup">
<div class="menuitem">
<a href="zookeeperAdmin.html">Administrator's Guide</a>
</div>
<div class="menuitem">
<a href="zookeeperQuotas.html">Quota Guide</a>
</div>
<div class="menuitem">
<a href="zookeeperJMX.html">JMX</a>
</div>
<div class="menuitem">
<a href="zookeeperObservers.html">Observers Guide</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.5', 'skin/')" id="menu_1.5Title" class="menutitle">Contributor</div>
<div id="menu_1.5" class="menuitemgroup">
<div class="menuitem">
<a href="zookeeperInternals.html">ZooKeeper Internals</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.6', 'skin/')" id="menu_1.6Title" class="menutitle">Miscellaneous</div>
<div id="menu_1.6" class="menuitemgroup">
<div class="menuitem">
<a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER">Wiki</a>
</div>
<div class="menuitem">
<a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/FAQ">FAQ</a>
</div>
<div class="menuitem">
<a href="http://zookeeper.apache.org/mailing_lists.html">Mailing Lists</a>
</div>
</div>
<div id="credit"></div>
<div id="roundbottom">
<img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
<!--+
|alternative credits
+-->
<div id="credit2"></div>
</div>
<!--+
|end Menu
+-->
<!--+
|start content
+-->
<div id="content">
<div title="Portable Document Format" class="pdflink">
<a class="dida" href="bookkeeperStream.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
PDF</a>
</div>
<h1>Streaming with BookKeeper</h1>
<div id="front-matter">
<div id="minitoc-area">
<ul class="minitoc">
<li>
<a href="#bk_StreamSummary">Summary</a>
</li>
<li>
<a href="#bk_LedgerOutputStream">Writing a stream of bytes</a>
</li>
<li>
<a href="#bk_LedgerInputStream">Reading a stream of bytes</a>
</li>
</ul>
</div>
</div>
<a name="bk_StreamSummary"></a>
<h2 class="h3">Summary</h2>
<div class="section">
<p>
When using the BookKeeper API, an application has to split the data to write into entries, each
entry being a byte array. This is natural for many applications. For example, when using BookKeeper
for write-ahead logging, an application typically wants to write the modifications corresponding
to a command or a transaction. Some other applications, however, might not have a natural boundary
for entries, and may prefer to write and read streams of bytes. This is exactly the purpose of the
stream API we have implemented on top of BookKeeper.
</p>
<p>
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
<span class="codefrag computeroutput">LedgerInputStream</span>. The class names are indicative of what they do.
</p>
</div>
<a name="bk_LedgerOutputStream"></a>
<h2 class="h3">Writing a stream of bytes</h2>
<div class="section">
<p>
Class <span class="codefrag computeroutput">LedgerOutputStream</span> implements two constructors and five public methods:
</p>
<p>
<span class="codefrag computeroutput">
public LedgerOutputStream(LedgerHandle lh)
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a ledger handle for a previously created and open ledger.
</p>
</li>
</ul>
<p>
<span class="codefrag computeroutput">
public LedgerOutputStream(LedgerHandle lh, int size)
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a ledger handle for a previously created and open ledger.
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">size</span> is the size of the byte buffer to store written bytes before flushing.
</p>
</li>
</ul>
<p>
<strong>Closing a stream.</strong> This call closes the stream by flushing the write buffer.
</p>
<p>
<span class="codefrag computeroutput">
public void close()
</span>
</p>
<p>
which has no parameters.
</p>
<p>
<strong>Flushing a stream.</strong> This call essentially flushes the write buffer.
</p>
<p>
<span class="codefrag computeroutput">
public synchronized void flush()
</span>
</p>
<p>
which has no parameters.
</p>
<p>
<strong>Writing bytes.</strong> There are three calls for writing bytes to a stream.
</p>
<p>
<span class="codefrag computeroutput">
public synchronized void write(byte[] b)
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">b</span> is an array of bytes to write.
</p>
</li>
</ul>
<p>
<span class="codefrag computeroutput">
public synchronized void write(byte[] b, int off, int len)
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">b</span> is an array of bytes to write.
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">off</span> is a buffer offset.
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">len</span> is the length to write.
</p>
</li>
</ul>
<p>
<span class="codefrag computeroutput">
public synchronized void write(int b)
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">b</span> contains a byte to write. The method writes the least significant byte of the integer four bytes.
</p>
</li>
</ul>
</div>
<a name="bk_LedgerInputStream"></a>
<h2 class="h3">Reading a stream of bytes</h2>
<div class="section">
<p>
Class <span class="codefrag computeroutput">LedgerOutputStream</span> implements two constructors and four public methods:
</p>
<p>
<span class="codefrag computeroutput">
public LedgerInputStream(LedgerHandle lh)
throws BKException, InterruptedException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a ledger handle for a previously created and open ledger.
</p>
</li>
</ul>
<p>
<span class="codefrag computeroutput">
public LedgerInputStream(LedgerHandle lh, int size)
throws BKException, InterruptedException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a ledger handle for a previously created and open ledger.
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">size</span> is the size of the byte buffer to store bytes that the application
will eventually read.
</p>
</li>
</ul>
<p>
<strong>Closing.</strong> There is one call to close an input stream, but the call
is currently empty and the application is responsible for closing the ledger handle.
</p>
<p>
<span class="codefrag computeroutput">
public void close()
</span>
</p>
<p>
which has no parameters.
</p>
<p>
<strong>Reading.</strong> There are three calls to read from the stream.
</p>
<p>
<span class="codefrag computeroutput">
public synchronized int read()
throws IOException
</span>
</p>
<p>
which has no parameters.
</p>
<p>
<span class="codefrag computeroutput">
public synchronized int read(byte[] b)
throws IOException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">b</span> is a byte array to write to.
</p>
</li>
</ul>
<p>
<span class="codefrag computeroutput">
public synchronized int read(byte[] b, int off, int len)
throws IOException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">b</span> is a byte array to write to.
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">off</span> is an offset for byte array <span class="codefrag computeroutput">b</span>.
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">len</span> is the length in bytes to write to <span class="codefrag computeroutput">b</span>.
</p>
</li>
</ul>
</div>
<p align="right">
<font size="-2"></font>
</p>
</div>
<!--+
|end content
+-->
<div class="clearboth"> </div>
</div>
<div id="footer">
<!--+
|start bottomstrip
+-->
<div class="lastmodified">
<script type="text/javascript"><!--
document.write("Last Published: " + document.lastModified);
// --></script>
</div>
<div class="copyright">
Copyright ©
2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
</div>
<!--+
|end bottomstrip
+-->
</div>
</body>
</html>