Create a HBaseConfiguration object to connect to a HBase server. You need to tell configuration object that where to read the HBase configuration from. to do this add a resource to the HBaseConfiguration object.
HBaseConfiguration conf = new HBaseConfiguration();
conf.addResource(new Path("/opt/hbase-0.19.3/conf/hbase-site.xml"));
Create a HTable object to a table in HBase. HTable object connects you to a table in HBase.
HTable table = new HTable(conf, "test_table");
Create a BatchUpdate object on a row to perform update operations (like put and delete)
BatchUpdate batchUpdate = new BatchUpdate("test_row1");
batchUpdate.put("columnfamily:column1", Bytes.toBytes("some value"));
batchUpdate.delete("column1");
Commit the changes to table using HTable#commit() method.
table.commit(batchUpdate);
To read one column value from a row use HTable#get() method.
Cell cell = table.get("test_row1", "columnfamily1:column1");
if (cell != null) {
String valueStr = Bytes.toString(cell.getValue());
System.out.println("test_row1:columnfamily1:column1 " + valueStr);
}
To read one row with given columns, use HTable#getRow() method.
RowResult singleRow = table.getRow(Bytes.toBytes("test_row1"));
Cell cell = singleRow.get(Bytes.toBytes("columnfamily1:column1"));
if(cell!=null) {
System.out.println(Bytes.toString(cell.getValue()));
}
cell = singleRow.get(Bytes.toBytes("columnfamily1:column2"));
if(cell!=null) {
System.out.println(Bytes.toString(cell.getValue()));
}
To get multiple rows use Scanner and iterate throw to get values.
Scanner scanner = table.getScanner(
new String[] { "columnfamily1:column1" });
//First aproach to iterate the scanner.
RowResult rowResult = scanner.next();
while (rowResult != null) {
System.out.println("Found row: " + Bytes.toString(rowResult.getRow())
+ " with value: " +
rowResult.get(Bytes.toBytes("columnfamily1:column1")));
rowResult = scanner.next();
}
// The other approach is to use a foreach loop. Scanners are iterable!
for (RowResult result : scanner) {
System.out.println("Found row: " + Bytes.toString(result.getRow())
+ " with value: " +
result.get(Bytes.toBytes("columnfamily1:column1")));
}
scanner.close();
HBaseConfiguration conf = new HBaseConfiguration();
conf.addResource(new Path("/opt/hbase-0.19.3/conf/hbase-site.xml"));
Create a HTable object to a table in HBase. HTable object connects you to a table in HBase.
HTable table = new HTable(conf, "test_table");
Create a BatchUpdate object on a row to perform update operations (like put and delete)
BatchUpdate batchUpdate = new BatchUpdate("test_row1");
batchUpdate.put("columnfamily:column1", Bytes.toBytes("some value"));
batchUpdate.delete("column1");
Commit the changes to table using HTable#commit() method.
table.commit(batchUpdate);
To read one column value from a row use HTable#get() method.
Cell cell = table.get("test_row1", "columnfamily1:column1");
if (cell != null) {
String valueStr = Bytes.toString(cell.getValue());
System.out.println("test_row1:columnfamily1:column1 " + valueStr);
}
To read one row with given columns, use HTable#getRow() method.
RowResult singleRow = table.getRow(Bytes.toBytes("test_row1"));
Cell cell = singleRow.get(Bytes.toBytes("columnfamily1:column1"));
if(cell!=null) {
System.out.println(Bytes.toString(cell.getValue()));
}
cell = singleRow.get(Bytes.toBytes("columnfamily1:column2"));
if(cell!=null) {
System.out.println(Bytes.toString(cell.getValue()));
}
To get multiple rows use Scanner and iterate throw to get values.
Scanner scanner = table.getScanner(
new String[] { "columnfamily1:column1" });
//First aproach to iterate the scanner.
RowResult rowResult = scanner.next();
while (rowResult != null) {
System.out.println("Found row: " + Bytes.toString(rowResult.getRow())
+ " with value: " +
rowResult.get(Bytes.toBytes("columnfamily1:column1")));
rowResult = scanner.next();
}
// The other approach is to use a foreach loop. Scanners are iterable!
for (RowResult result : scanner) {
System.out.println("Found row: " + Bytes.toString(result.getRow())
+ " with value: " +
result.get(Bytes.toBytes("columnfamily1:column1")));
}
scanner.close();
No comments:
Post a Comment