Skip to content

Instantly share code, notes, and snippets.

@jbenninghoff
Created August 18, 2015 19:00
Show Gist options
  • Save jbenninghoff/76993bdab314bcbb9c4f to your computer and use it in GitHub Desktop.
Save jbenninghoff/76993bdab314bcbb9c4f to your computer and use it in GitHub Desktop.
HBase Test Case
/*
* Compile and run with:
* javac -cp $(hbase classpath) TestHBase.java
* java -cp .:$(hbase classpath) TestHBase
*/
import java.net.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.*;
public class TestHBase {
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
//conf.set("hbase.zookeeper.quorum", "10.10.15.10:5181,10.10.15.11:5181,10.10.15.12:5181"); //This API is a No-Op
HBaseAdmin admin = new HBaseAdmin(conf);
try {
if (admin.isTableAvailable("javatable")) {
System.out.println("javatable exists, will be deleted then recreated");
admin.disableTable("javatable");
admin.deleteTable("javatable");
}
HTableDescriptor desc = new HTableDescriptor(Bytes.toBytes("javatable"));
HColumnDescriptor coldef = new HColumnDescriptor( Bytes.toBytes("cf1"));
desc.addFamily(coldef);
admin.createTable(desc);
//String tablePath = URI.create("maprfs:/mapr/qa/javatable").getPath();
// admin.createTable(tablePath);
HTable jtable = new HTable(conf, "javatable");
// String tablePath = URI.create("maprfs:/mapr/M7SE/tables/javatable").getPath();
//HTable jtable = new HTable(conf, tablePath);
Put put = new Put(Bytes.toBytes("test-key"));
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("q1"), Bytes.toBytes("test-value"));
jtable.put(put);
Get get = new Get(Bytes.toBytes("test-key"));
get.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("q1"));
Result jresult = jtable.get(get);
byte[] jval = jresult.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("q1"));
System.out.println("Value: " + Bytes.toString(jval));
} finally {
admin.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment