HBASE-26765 Minor refactor of async scanning code (#4121)
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / client / package-info.java
blobdd85d3e062886bb1ff81e471b22d1e0165337039
1 /*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 /**
21 Provides HBase Client
23 <h2>Table of Contents</h2>
24 <ul>
25 <li><a href="#overview">Overview</a></li>
26 <li><a href="#client_example">Example API Usage</a></li>
27 </ul>
29 <h2><a name="overview">Overview</a></h2>
30 <p>To administer HBase, create and drop tables, list and alter tables,
31 use {@link org.apache.hadoop.hbase.client.Admin}. Once created, table access is via an instance
32 of {@link org.apache.hadoop.hbase.client.Table}. You add content to a table a row at a time. To
33 insert, create an instance of a {@link org.apache.hadoop.hbase.client.Put} object. Specify value,
34 target column and optionally a timestamp. Commit your update using
35 {@link org.apache.hadoop.hbase.client.Table#put(Put)}.
36 To fetch your inserted value, use {@link org.apache.hadoop.hbase.client.Get}. The Get can be
37 specified to be broad -- get all on a particular row -- or narrow; i.e. return only a single cell
38 value. After creating an instance of
39 Get, invoke {@link org.apache.hadoop.hbase.client.Table#get(Get)}.
41 <p>Use {@link org.apache.hadoop.hbase.client.Scan} to set up a scanner -- a Cursor- like access.
42 After creating and configuring your Scan instance, call
43 {@link org.apache.hadoop.hbase.client.Table#getScanner(Scan)} and then
44 invoke next on the returned object. Both {@link org.apache.hadoop.hbase.client.Table#get(Get)}
45 and {@link org.apache.hadoop.hbase.client.Table#getScanner(Scan)} return a
46 {@link org.apache.hadoop.hbase.client.Result}.
48 <p>Use {@link org.apache.hadoop.hbase.client.Delete} to remove content.
49 You can remove individual cells or entire families, etc. Pass it to
50 {@link org.apache.hadoop.hbase.client.Table#delete(Delete)} to execute.
51 </p>
52 <p>Puts, Gets and Deletes take out a lock on the target row for the duration of their operation.
53 Concurrent modifications to a single row are serialized. Gets and scans run concurrently without
54 interference of the row locks and are guaranteed to not to return half written rows.
55 </p>
56 <p>Client code accessing a cluster finds the cluster by querying ZooKeeper.
57 This means that the ZooKeeper quorum to use must be on the client CLASSPATH.
58 Usually this means make sure the client can find your <code>hbase-site.xml</code>.
59 </p>
61 <h2><a name="client_example">Example API Usage</a></h2>
63 <p>Once you have a running HBase, you probably want a way to hook your application up to it.
64 If your application is in Java, then you should use the Java API. Here's an example of what
65 a simple client might look like. This example assumes that you've created a table called
66 "myTable" with a column family called "myColumnFamily".
67 </p>
69 <div style="background-color: #cccccc; padding: 2px">
70 <blockquote><pre>
71 import java.io.IOException;
73 import org.apache.hadoop.hbase.HBaseConfiguration;
74 import org.apache.hadoop.hbase.TableName;
75 import org.apache.hadoop.hbase.client.Connection;
76 import org.apache.hadoop.hbase.client.ConnectionFactory;
77 import org.apache.hadoop.hbase.client.Get;
78 import org.apache.hadoop.hbase.client.Table;
79 import org.apache.hadoop.hbase.client.Put;
80 import org.apache.hadoop.hbase.client.Result;
81 import org.apache.hadoop.hbase.client.ResultScanner;
82 import org.apache.hadoop.hbase.client.Scan;
83 import org.apache.hadoop.hbase.util.Bytes;
85 // Class that has nothing but a main.
86 // Does a Put, Get and a Scan against an hbase table.
87 // The API described here is since HBase 1.0.
88 public class MyLittleHBaseClient {
89 public static void main(String[] args) throws IOException {
90 // You need a configuration object to tell the client where to connect.
91 // When you create a HBaseConfiguration, it reads in whatever you've set
92 // into your hbase-site.xml and in hbase-default.xml, as long as these can
93 // be found on the CLASSPATH
94 Configuration config = HBaseConfiguration.create();
96 // Next you need a Connection to the cluster. Create one. When done with it,
97 // close it. A try/finally is a good way to ensure it gets closed or use
98 // the jdk7 idiom, try-with-resources: see
99 // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
101 // Connections are heavyweight. Create one once and keep it around. From a Connection
102 // you get a Table instance to access Tables, an Admin instance to administer the cluster,
103 // and RegionLocator to find where regions are out on the cluster. As opposed to Connections,
104 // Table, Admin and RegionLocator instances are lightweight; create as you need them and then
105 // close when done.
107 Connection connection = ConnectionFactory.createConnection(config);
108 try {
110 // The below instantiates a Table object that connects you to the "myLittleHBaseTable" table
111 // (TableName.valueOf turns String into a TableName instance).
112 // When done with it, close it (Should start a try/finally after this creation so it gets
113 // closed for sure the jdk7 idiom, try-with-resources: see
114 // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)
115 Table table = connection.getTable(TableName.valueOf("myLittleHBaseTable"));
116 try {
118 // To add to a row, use Put. A Put constructor takes the name of the row
119 // you want to insert into as a byte array. In HBase, the Bytes class has
120 // utility for converting all kinds of java types to byte arrays. In the
121 // below, we are converting the String "myLittleRow" into a byte array to
122 // use as a row key for our update. Once you have a Put instance, you can
123 // adorn it by setting the names of columns you want to update on the row,
124 // the timestamp to use in your update, etc. If no timestamp, the server
125 // applies current time to the edits.
126 Put p = new Put(Bytes.toBytes("myLittleRow"));
128 // To set the value you'd like to update in the row 'myLittleRow', specify
129 // the column family, column qualifier, and value of the table cell you'd
130 // like to update. The column family must already exist in your table
131 // schema. The qualifier can be anything. All must be specified as byte
132 // arrays as hbase is all about byte arrays. Lets pretend the table
133 // 'myLittleHBaseTable' was created with a family 'myLittleFamily'.
134 p.add(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
135 Bytes.toBytes("Some Value"));
137 // Once you've adorned your Put instance with all the updates you want to
138 // make, to commit it do the following (The HTable#put method takes the
139 // Put instance you've been building and pushes the changes you made into
140 // hbase)
141 table.put(p);
143 // Now, to retrieve the data we just wrote. The values that come back are
144 // Result instances. Generally, a Result is an object that will package up
145 // the hbase return into the form you find most palatable.
146 Get g = new Get(Bytes.toBytes("myLittleRow"));
147 Result r = table.get(g);
148 byte [] value = r.getValue(Bytes.toBytes("myLittleFamily"),
149 Bytes.toBytes("someQualifier"));
151 // If we convert the value bytes, we should get back 'Some Value', the
152 // value we inserted at this location.
153 String valueStr = Bytes.toString(value);
154 System.out.println("GET: " + valueStr);
156 // Sometimes, you won't know the row you're looking for. In this case, you
157 // use a Scanner. This will give you cursor-like interface to the contents
158 // of the table. To set up a Scanner, do like you did above making a Put
159 // and a Get, create a Scan. Adorn it with column names, etc.
160 Scan s = new Scan();
161 s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
162 ResultScanner scanner = table.getScanner(s);
163 try {
164 // Scanners return Result instances.
165 // Now, for the actual iteration. One way is to use a while loop like so:
166 for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
167 // print out the row we found and the columns we were looking for
168 System.out.println("Found row: " + rr);
171 // The other approach is to use a foreach loop. Scanners are iterable!
172 // for (Result rr : scanner) {
173 // System.out.println("Found row: " + rr);
174 // }
175 } finally {
176 // Make sure you close your scanners when you are done!
177 // Thats why we have it inside a try/finally clause
178 scanner.close();
181 // Close your table and cluster connection.
182 } finally {
183 if (table != null) table.close();
185 } finally {
186 connection.close();
190 </pre></blockquote>
191 </div>
193 <p>There are many other methods for putting data into and getting data out of
194 HBase, but these examples should get you started. See the Table javadoc for
195 more methods. Additionally, there are methods for managing tables in the
196 Admin class.</p>
198 <p>If your client is NOT Java, then you should consider the Thrift or REST
199 libraries.</p>
201 <h2><a name="related" >Related Documentation</a></h2>
202 <ul>
203 <li><a href="http://hbase.org/">HBase Home Page</a>
204 <li><a href="http://hadoop.apache.org/">Hadoop Home Page</a>
205 </ul>
206 <p>See also the section in the HBase Reference Guide where it discusses
207 <a href="http://hbase.apache.org/book.html#client">HBase Client</a>. It
208 has section on how to access HBase from inside your multithreaded environment
209 how to control resources consumed client-side, etc.</p>
211 package org.apache.hadoop.hbase.client;