Database Reference
In-Depth Information
Clients
There are a number of client options for interacting with an HBase cluster.
Java
HBase, like Hadoop, is written in Java.
Example 20-1
shows the Java version of how you
would do the shell operations listed in the previous section.
Example 20-1. Basic table administration and access
public class
ExampleClient
{
public static
void
main
(
String
[]
args
)
throws
IOException
{
Configuration config
=
HBaseConfiguration
.
create
();
// Create table
HBaseAdmin admin
=
new
HBaseAdmin
(
config
);
try
{
TableName tableName
=
TableName
.
valueOf
(
"test"
);
HTableDescriptor htd
=
new
HTableDescriptor
(
tableName
);
HColumnDescriptor hcd
=
new
HColumnDescriptor
(
"data"
);
htd
.
addFamily
(
hcd
);
admin
.
createTable
(
htd
);
HTableDescriptor
[]
tables
=
admin
.
listTables
();
if
(
tables
.
length
!=
1
&&
Bytes
.
equals
(
tableName
.
getName
(),
tables
[
0
].
getTableName
().
getName
())) {
throw new
IOException
(
"Failed create of table"
);
}
// Run some operations -- three puts, a get, and a scan -- against
the table.
HTable table
=
new
HTable
(
config
,
tableName
);
try
{
for
(
int
i
=
1
;
i
<=
3
;
i
++) {
byte
[]
row
=
Bytes
.
toBytes
(
"row"
+
i
);
Put put
=
new
Put
(
row
);
byte
[]
columnFamily
=
Bytes
.
toBytes
(
"data"
);
byte
[]
qualifier
=
Bytes
.
toBytes
(
String
.
valueOf
(
i
));
byte
[]
value
=
Bytes
.
toBytes
(
"value"
+
i
);
put
.
add
(
columnFamily
,
qualifier
,
value
);
table
.
put
(
put
);
}
Get get
=
new
Get
(
Bytes
.
toBytes
(
"row1"
));
Result result
=
table
.
get
(
get
);
System
.
out
.
println
(
"Get: "
+
result
);
Scan scan
=
new
Scan
();