Can hbase be embedded in Java applications?
2 answers
You can definitely write a Java application to use Hbase, there is a Java API that is provided in the main Hbase distribution.
You should grab the latest version from the official site and get a jar hbase-0.xx.x.jar
that you can use to build your application. If you want to see the class dependencies for hbase once you have installed hbase, you can simply do hbase classpath
and that should print out a list of the ATMs you need.
You can probably find many examples of Java applications doing Hbase operations on Google, but here is an example for common operations:
// get the hbase config
Configuration config = HBaseConfiguration.create();
// specify which table you want to use
HTable table = new HTable(config, "mytable");
// add a row to your table
Put p = new Put(Bytes.toBytes("myrow"));
// specify the column family, column qualifier and column value
p.add(Bytes.toBytes("myfamily"), Bytes.toBytes("myqualifier"), Bytes.toBytes("myvalue"));
// commit to your table
table.put(p);
// define which row you want to get
Get g = new Get(Bytes.toBytes("myrow"));
// get your row
Result r = table.get(g);
// choose what you want to extract from your row
byte[] value = r.getValue(Bytes.toBytes("myfamily"), Bytes.toBytes("myqualifier"));
// convert to a string
System.out.println("GET: " + Bytes.toString(value));
// do a scan operation
Scan s = new Scan();
s.addColumn(Bytes.toBytes("myfamily"), Bytes.toBytes("myqualifier"));
ResultScanner scanner = table.getScanner(s);
+3
source to share