HBase操作一

扫码查看
 package Hbase;

 import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.GetDataEncryptionKeyRequestProto;
import org.apache.hadoop.io.IOUtils; public class HbaseOperation { public static HTable getHTableByTableName(String tableName) throws IOException {
// get instance of default configuration
Configuration configuration = HBaseConfiguration.create();
// get table instance
HTable table = new HTable(configuration, tableName); return table;
} public static void getData(String tableName) throws IOException{
//String tableName = "user";
HTable table = getHTableByTableName(tableName);
//create get with rowkey
Get rowkey = new Get(Bytes.toBytes("10001"));
//****************************************************
//add column
rowkey.addColumn(//
Bytes.toBytes("info"),//
Bytes.toBytes("name")
); rowkey.addColumn(//
Bytes.toBytes("info"),//
Bytes.toBytes("age")
); //get data
Result result = table.get(rowkey);
//key : rewkey + cf +c +version
//value :value
for(Cell cell:result.rawCells()){
System.out.println(//
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->" //
+ Bytes.toString(CellUtil.cloneValue(cell))
);
}
//table close
table.close(); } public static void putData(String tableName) throws IOException {
//String tableName = "user";
HTable table = getHTableByTableName(tableName); Put put = new Put(Bytes.toBytes("10003"));
//add a column with value
put.add(//
Bytes.toBytes("info"),//
Bytes.toBytes("name"),//
Bytes.toBytes("wangwu")//
); put.add(//
Bytes.toBytes("info"),//
Bytes.toBytes("age"),//
Bytes.toBytes("26")//
); put.add(//
Bytes.toBytes("info"),//
Bytes.toBytes("address"),//
Bytes.toBytes("tianjing")//
); table.put(put); table.close();
} public static void deleteData(String tableName) throws IOException {
HTable table = getHTableByTableName(tableName);
Delete delete = new Delete(Bytes.toBytes("10003"));
//delete a certain column
// delete.deleteColumn(Bytes.toBytes("info"), //
// Bytes.toBytes("address")); //delete a familycolumn
delete.deleteFamily(Bytes.toBytes("info")); table.delete(delete);
table.close();
} public static void main(String[] args) throws IOException {
String tableName = "user";
// HTable table = getHTableByTableName(tableName);
// getData(tableName);
// putData(tableName);
// deleteData(tableName); HTable table = null;
ResultScanner resultScanner = null;
try{
table = getHTableByTableName(tableName); Scan scan = new Scan();
//the range of scan
scan.setStartRow(Bytes.toBytes("10001"));
scan.setStartRow(Bytes.toBytes("10004")); //scan the certain column or familycolumn
// scan.addColumn(family, qualifier);
// scan.addFamily(family); //another way to scan
//Scan scan2 = new Scan(startRow, stopRow); //PrefixFilter
//PageFilter
// scan.setFilter(filter); // scan.setCacheBlocks(cacheBlocks);
// scan.setCaching(caching); resultScanner = table.getScanner(scan);
for (Result result:resultScanner) {
System.out.println(Bytes.toString(result.getRow()));
//System.out.println(result); for(Cell cell:result.rawCells()){
System.out.println(//
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->" //
+ Bytes.toString(CellUtil.cloneValue(cell))
);
}
System.out.println("------------------------------");
}
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(resultScanner);
IOUtils.closeStream(table);
}
}
}
05-07 10:02
查看更多