blob: df8a6e6c01f83293b761c7b369d264ef8fcb5337 [file] [log] [blame]
<!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>BookKeeper Getting Started Guide</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> &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>
</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://hadoop.apache.org/zookeeper/"><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="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;
<input name="Search" value="Search" type="submit">
</form>
</div>
<!--+
|end search
+-->
<!--+
|start Tabs
+-->
<ul id="tabs">
<li>
<a class="unselected" href="http://hadoop.apache.org/zookeeper/">Project</a>
</li>
<li>
<a class="unselected" href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
</li>
<li class="current">
<a class="selected" href="index.html">ZooKeeper 3.3 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">
&nbsp;
</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_selected_1.3', 'skin/')" id="menu_selected_1.3Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">BookKeeper</div>
<div id="menu_selected_1.3" class="selectedmenuitemgroup" style="display: block;">
<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="menupage">
<div class="menupagetitle">Programmer's guide</div>
</div>
</div>
<div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Admin &amp; 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="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
</div>
<div class="menuitem">
<a href="http://wiki.apache.org/hadoop/ZooKeeper/FAQ">FAQ</a>
</div>
<div class="menuitem">
<a href="http://hadoop.apache.org/zookeeper/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="bookkeeperProgrammer.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
PDF</a>
</div>
<h1>BookKeeper Getting Started Guide</h1>
<div id="front-matter">
<div id="minitoc-area">
<ul class="minitoc">
<li>
<a href="#bk_GettingStarted">Programming with BookKeeper</a>
<ul class="minitoc">
<li>
<a href="#bk_instance"> Instantiating BookKeeper.</a>
</li>
<li>
<a href="#bk_createLedger"> Creating a ledger. </a>
</li>
<li>
<a href="#bk_writeLedger"> Adding entries to a ledger. </a>
</li>
<li>
<a href="#bk_closeLedger"> Closing a ledger. </a>
</li>
<li>
<a href="#bk_openLedger"> Opening a ledger. </a>
</li>
<li>
<a href="#bk_readLedger"> Reading from ledger </a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<a name="bk_GettingStarted"></a>
<h2 class="h3">Programming with BookKeeper</h2>
<div class="section">
<ul>
<li>
<p>
<a href="#bk_instance"> Instantiating BookKeeper.</a>
</p>
</li>
<li>
<p>
<a href="#bk_createLedger"> Creating a ledger. </a>
</p>
</li>
<li>
<p>
<a href="#bk_writeLedger"> Adding entries to a ledger. </a>
</p>
</li>
<li>
<p>
<a href="#bk_closeLedger"> Closing a ledger. </a>
</p>
</li>
<li>
<p>
<a href="#bk_openLedger"> Opening a ledger. </a>
</p>
</li>
<li>
<p>
<a href="#bk_readLedger"> Reading from ledger </a>
</p>
</li>
</ul>
<a name="bk_instance"></a>
<h3 class="h4"> Instantiating BookKeeper.</h3>
<p>
The first step to use BookKeeper is to instantiate a BookKeeper object:
</p>
<p>
<span class="codefrag computeroutput">
org.apache.bookkeeper.BookKeeper
</span>
</p>
<p>
There are three BookKeeper constructors:
</p>
<p>
<span class="codefrag computeroutput">
public BookKeeper(String servers)
throws KeeperException, IOException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">servers</span> is a comma-separated list of ZooKeeper servers.
</p>
</li>
</ul>
<p>
<span class="codefrag computeroutput">
public BookKeeper(ZooKeeper zk)
throws InterruptedException, KeeperException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">zk</span> is a ZooKeeper object. This constructor is useful when
the application also using ZooKeeper and wants to have a single instance of ZooKeeper.
</p>
</li>
</ul>
<p>
<span class="codefrag computeroutput">
public BookKeeper(ZooKeeper zk, ClientSocketChannelFactory channelFactory)
throws InterruptedException, KeeperException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">zk</span> is a ZooKeeper object. This constructor is useful when
the application also using ZooKeeper and wants to have a single instance of ZooKeeper.
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">channelFactory</span> is a netty channel object
(<span class="codefrag computeroutput">org.jboss.netty.channel.socket</span>).
</p>
</li>
</ul>
<a name="bk_createLedger"></a>
<h3 class="h4"> Creating a ledger. </h3>
<p> Before writing entries to BookKeeper, it is necessary to create a ledger.
With the current BookKeeper API, it is possible to create a ledger both synchronously
or asynchronously. The following methods belong
to <span class="codefrag computeroutput">org.apache.bookkeeper.client.BookKeeper</span>.
</p>
<p>
<strong>Synchronous call:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public LedgerHandle createLedger(int ensSize, int qSize, DigestType type, byte passwd[])
throws KeeperException, InterruptedException,
IOException, BKException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">ensSize</span> is the number of bookies (ensemble size);
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">qSize</span> is the write quorum size;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">type</span> is the type of digest used with entries: either MAC or CRC32.
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">passwd</span> is a password that authorizes the client to write to the
ledger being created.
</p>
</li>
</ul>
<p>
All further operations on a ledger are invoked through the <span class="codefrag computeroutput">LedgerHandle</span>
object returned.
</p>
<p>
As a convenience, we provide a <span class="codefrag computeroutput">createLedger</span> with default parameters (3,2,VERIFIABLE),
and the only two input parameters it requires are a digest type and a password.
</p>
<p>
<strong>Asynchronous call:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public void asyncCreateLedger(int ensSize,
int qSize,
DigestType type,
byte passwd[],
CreateCallback cb,
Object ctx
)
</span>
</p>
<p>
The parameters are the same of the synchronous version, with the
exception of <span class="codefrag computeroutput">cb</span> and <span class="codefrag computeroutput">ctx</span>. <span class="codefrag computeroutput">CreateCallback</span>
is an interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
a class implementing it has to implement a method called <span class="codefrag computeroutput">createComplete</span>
that has the following signature:
</p>
<p>
<span class="codefrag computeroutput">
void createComplete(int rc, LedgerHandle lh, Object ctx);
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<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);
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">ctx</span> is a control object for accountability purposes. It can be essentially any object the application is happy with.
</p>
</li>
</ul>
<p>
The <span class="codefrag computeroutput">ctx</span> object passed as a parameter to the call to create a ledger
is the one same returned in the callback.
</p>
<a name="bk_writeLedger"></a>
<h3 class="h4"> Adding entries to a ledger. </h3>
<p>
Once we have a ledger handle <span class="codefrag computeroutput">lh</span> obtained through a call to create a ledger, we
can start writing entries. As with creating ledgers, we can write both synchronously and
asynchronously. The following methods belong
to <span class="codefrag computeroutput">org.apache.bookkeeper.client.LedgerHandle</span>.
</p>
<p>
<strong>Synchronous call:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public long addEntry(byte[] data)
throws InterruptedException
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">data</span> is a byte array;
</p>
</li>
</ul>
<p>
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);
</p>
<p>
<strong>Asynchronous call:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public void asyncAddEntry(byte[] data, AddCallback cb, Object ctx)
</span>
</p>
<p>
It also takes a byte array as the sequence of bytes to be stored as an entry. Additionaly, 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
the <span class="codefrag computeroutput">AddCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
a class implementing it has to implement a method called <span class="codefrag computeroutput">addComplete</span>
that has the following signature:
</p>
<p>
<span class="codefrag computeroutput">
void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx);
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<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);
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">entryId</span> is the identifier of entry associated with this request;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes. It can be any object the application is happy with.
</p>
</li>
</ul>
<a name="bk_closeLedger"></a>
<h3 class="h4"> Closing a ledger. </h3>
<p>
Once a client is done writing, it closes the ledger. The following methods belong
to <span class="codefrag computeroutput">org.apache.bookkeeper.client.LedgerHandle</span>.
</p>
<p>
<strong>Synchronous close:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public void close()
throws InterruptedException
</span>
</p>
<p>
It takes no input parameters.
</p>
<p>
<strong>Asynchronous close:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public void asyncClose(CloseCallback cb, Object ctx)
throws InterruptedException
</span>
</p>
<p>
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
the <span class="codefrag computeroutput">CloseCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
a class implementing it has to implement a method called <span class="codefrag computeroutput">closeComplete</span>
that has the following signature:
</p>
<p>
<span class="codefrag computeroutput">
void closeComplete(int rc, LedgerHandle lh, Object ctx)
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<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);
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes.
</p>
</li>
</ul>
<a name="bk_openLedger"></a>
<h3 class="h4"> Opening a ledger. </h3>
<p>
To read from a ledger, a client must open it first. The following methods belong
to <span class="codefrag computeroutput">org.apache.bookkeeper.client.BookKeeper</span>.
</p>
<p>
<strong>Synchronous open:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public LedgerHandle openLedger(long lId, DigestType type, byte passwd[])
throws InterruptedException, BKException
</span>
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">ledgerId</span> is the ledger identifier;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">type</span> is the type of digest used with entries: either MAC or CRC32.
</p>
</li>
<li>
<p>
<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);
</p>
</li>
</ul>
<p>
<strong>Asynchronous open:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public void asyncOpenLedger(long lId, DigestType type, byte passwd[], OpenCallback cb, Object ctx)
</span>
</p>
<p>
It also takes a a ledger identifier and a password. Additionaly, 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
the <span class="codefrag computeroutput">OpenCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
a class implementing it has to implement a method called <span class="codefrag computeroutput">openComplete</span>
that has the following signature:
</p>
<p>
<span class="codefrag computeroutput">
public void openComplete(int rc, LedgerHandle lh, Object ctx)
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<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);
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes.
</p>
</li>
</ul>
<a name="bk_readLedger"></a>
<h3 class="h4"> Reading from ledger </h3>
<p>
Read calls may request one or more consecutive entries. The following methods belong
to <span class="codefrag computeroutput">org.apache.bookkeeper.client.LedgerHandle</span>.
</p>
<p>
<strong>Synchronous read:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public LedgerSequence readEntries(long firstEntry, long lastEntry)
throws InterruptedException, BKException
</span>
</p>
<ul>
<li>
<p>
<span class="codefrag computeroutput">firstEntry</span> is the identifier of the first entry in the sequence of entries to read;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">lastEntry</span> is the identifier of the last entry in the sequence of entries to read.
</p>
</li>
</ul>
<p>
<strong>Asynchronous read:</strong>
</p>
<p>
<span class="codefrag computeroutput">
public void asyncReadEntries(long firstEntry,
long lastEntry, ReadCallback cb, Object ctx)
throws BKException, InterruptedException
</span>
</p>
<p>
It also takes a first and a last entry identifiers. Additionaly, 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
the <span class="codefrag computeroutput">ReadCallback</span> interface in <span class="codefrag computeroutput">org.apache.bookkeeper.client.AsyncCallback</span>, and
a class implementing it has to implement a method called <span class="codefrag computeroutput">readComplete</span>
that has the following signature:
</p>
<p>
<span class="codefrag computeroutput">
void readComplete(int rc, LedgerHandle lh, Enumeration&lt;LedgerEntry&gt; seq, Object ctx)
</span>
</p>
<p>
where:
</p>
<ul>
<li>
<p>
<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);
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">lh</span> is a <span class="codefrag computeroutput">LedgerHandle</span> object to manipulate a ledger;
</p>
</li>
<li>
<p>
<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;
</p>
</li>
<li>
<p>
<span class="codefrag computeroutput">ctx</span> is control object used for accountability purposes.
</p>
</li>
</ul>
</div>
<p align="right">
<font size="-2"></font>
</p>
</div>
<!--+
|end content
+-->
<div class="clearboth">&nbsp;</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 &copy;
2008-2013 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
</div>
<!--+
|end bottomstrip
+-->
</div>
</body>
</html>