hbase 0.98 java_【Hbase学习之三】Hbase Java API(示例代码)

packagehbase;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.Cell;importorg.apache.hadoop.hbase.CellUtil;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.KeyValue;importorg.apache.hadoop.hbase.MasterNotRunningException;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.ZooKeeperConnectionException;importorg.apache.hadoop.hbase.client.Delete;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.HBaseAdmin;importorg.apache.hadoop.hbase.client.HConnection;importorg.apache.hadoop.hbase.client.HConnectionManager;importorg.apache.hadoop.hbase.client.HTable;importorg.apache.hadoop.hbase.client.HTableInterface;importorg.apache.hadoop.hbase.client.HTablePool;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.filter.BinaryComparator;importorg.apache.hadoop.hbase.filter.CompareFilter.CompareOp;importorg.apache.hadoop.hbase.filter.Filter;importorg.apache.hadoop.hbase.filter.FilterList;importorg.apache.hadoop.hbase.filter.PrefixFilter;importorg.apache.hadoop.hbase.filter.RowFilter;importorg.apache.hadoop.hbase.filter.SingleColumnValueFilter;importorg.apache.hadoop.hbase.filter.SubstringComparator;importorg.apache.hadoop.hbase.util.Bytes;importorg.junit.Test;public classHBaseDAOImp {

HConnection hTablePool= null;static Configuration conf =null;publicHBaseDAOImp()

{

conf= newConfiguration();

String zk_list= "node1,node2,node3";

conf.set("hbase.zookeeper.quorum", zk_list);try{

hTablePool=HConnectionManager.createConnection(conf) ;

}catch(IOException e) {

e.printStackTrace();

}

}public voidsave(Put put, String tableName) {//TODO Auto-generated method stub

HTableInterface table = null;try{

table=hTablePool.getTable(tableName) ;

table.put(put) ;

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}

}/*** 插入一个cell

*@paramtableName

*@paramrowKey

*@paramfamily

*@paramquailifer

*@paramvalue*/

public voidinsert(String tableName, String rowKey, String family,

String quailifer, String value) {//TODO Auto-generated method stub

HTableInterface table = null;try{

table=hTablePool.getTable(tableName) ;

Put put= newPut(rowKey.getBytes());

put.add(family.getBytes(), quailifer.getBytes(), value.getBytes()) ;

table.put(put);

}catch(Exception e) {

e.printStackTrace();

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}

}/*** 在一个列族下插入多个单元格

*@paramtableName

*@paramrowKey

*@paramfamily

*@paramquailifer

*@paramvalue*/

public voidinsert(String tableName,String rowKey,String family,String quailifer[],String value[])

{

HTableInterface table= null;try{

table=hTablePool.getTable(tableName) ;

Put put= newPut(rowKey.getBytes());//批量添加

for (int i = 0; i < quailifer.length; i++) {

String col=quailifer[i];

String val=value[i];

put.add(family.getBytes(), col.getBytes(), val.getBytes());

}

table.put(put);

}catch(Exception e) {

e.printStackTrace();

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}

}public void save(ListPut, String tableName) {//TODO Auto-generated method stub

HTableInterface table = null;try{

table=hTablePool.getTable(tableName) ;

table.put(Put) ;

}catch(Exception e) {//TODO: handle exception

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}

}publicResult getOneRow(String tableName, String rowKey) {//TODO Auto-generated method stub

HTableInterface table = null;

Result rsResult= null;try{

table=hTablePool.getTable(tableName) ;

Get get= newGet(rowKey.getBytes()) ;

rsResult=table.get(get) ;

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}returnrsResult;

}/*** 最常用的方法,优化查询

* 查询一行数据,

*@paramtableName

*@paramrowKey

*@paramcols

*@return

*/

publicResult getOneRowAndMultiColumn(String tableName, String rowKey,String[] cols) {//TODO Auto-generated method stub

HTableInterface table = null;

Result rsResult= null;try{

table=hTablePool.getTable(tableName) ;

Get get= newGet(rowKey.getBytes()) ;for (int i = 0; i < cols.length; i++) {

get.addColumn("cf".getBytes(), cols[i].getBytes()) ;

}

rsResult=table.get(get) ;

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}returnrsResult;

}public ListgetRows(String tableName, String rowKeyLike) {//TODO Auto-generated method stub

HTableInterface table = null;

List list = null;try{

FilterList fl= newFilterList(FilterList.Operator.MUST_PASS_ALL);

table=hTablePool.getTable(tableName) ;

PrefixFilter filter= newPrefixFilter(rowKeyLike.getBytes());

SingleColumnValueFilter filter1= newSingleColumnValueFilter("order".getBytes(),"order_type".getBytes(),

CompareOp.EQUAL,

Bytes.toBytes("1")

);

fl.addFilter(filter);

fl.addFilter(filter1);

Scan scan= newScan();

scan.setFilter(fl);

ResultScanner scanner=table.getScanner(scan) ;

list= new ArrayList() ;for(Result rs : scanner) {

list.add(rs) ;

}

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}returnlist;

}public ListgetRows(String tableName, String rowKeyLike ,String cols[]) {//TODO Auto-generated method stub

HTableInterface table = null;

List list = null;try{

table=hTablePool.getTable(tableName) ;

PrefixFilter filter= newPrefixFilter(rowKeyLike.getBytes());

Scan scan= newScan();for (int i = 0; i < cols.length; i++) {

scan.addColumn("cf".getBytes(), cols[i].getBytes()) ;

}

scan.setFilter(filter);

ResultScanner scanner=table.getScanner(scan) ;

list= new ArrayList() ;for(Result rs : scanner) {

list.add(rs) ;

}

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}returnlist;

}public ListgetRowsByOneKey(String tableName, String rowKeyLike ,String cols[]) {//TODO Auto-generated method stub

HTableInterface table = null;

List list = null;try{

table=hTablePool.getTable(tableName) ;

PrefixFilter filter= newPrefixFilter(rowKeyLike.getBytes());

Scan scan= newScan();for (int i = 0; i < cols.length; i++) {

scan.addColumn("cf".getBytes(), cols[i].getBytes()) ;

}

scan.setFilter(filter);

ResultScanner scanner=table.getScanner(scan) ;

list= new ArrayList() ;for(Result rs : scanner) {

list.add(rs) ;

}

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}returnlist;

}/*** 范围查询

*@paramtableName

*@paramstartRow

*@paramstopRow

*@return

*/

public ListgetRows(String tableName,String startRow,String stopRow)

{

HTableInterface table= null;

List list = null;try{

table=hTablePool.getTable(tableName) ;

Scan scan= newScan() ;

scan.setStartRow(startRow.getBytes()) ;

scan.setStopRow(stopRow.getBytes()) ;

ResultScanner scanner=table.getScanner(scan) ;

list= new ArrayList() ;for(Result rsResult : scanner) {

list.add(rsResult) ;

}

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}returnlist;

}public voiddeleteRecords(String tableName, String rowKeyLike){

HTableInterface table= null;try{

table=hTablePool.getTable(tableName) ;

PrefixFilter filter= newPrefixFilter(rowKeyLike.getBytes());

Scan scan= newScan();

scan.setFilter(filter);

ResultScanner scanner=table.getScanner(scan) ;

List list = new ArrayList() ;for(Result rs : scanner) {

Delete del= newDelete(rs.getRow());

list.add(del) ;

}

table.delete(list);

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}

}public voiddeleteCell(String tableName, String rowkey,String cf,String column){

HTableInterface table= null;try{

table=hTablePool.getTable(tableName) ;

Delete del= newDelete(rowkey.getBytes());

del.deleteColumn(cf.getBytes(), column.getBytes());

table.delete(del);

}catch(Exception e) {

e.printStackTrace() ;

}finally{try{

table.close() ;

}catch(IOException e) {

e.printStackTrace();

}

}

}public voidcreateTable(String tableName, String[] columnFamilys){try{//admin 对象

HBaseAdmin admin = newHBaseAdmin(conf);if(admin.tableExists(tableName)) {

System.err.println("此表,已存在!");

}else{

HTableDescriptor tableDesc= newHTableDescriptor(

TableName.valueOf(tableName));for(String columnFamily : columnFamilys) {

tableDesc.addFamily(newHColumnDescriptor(columnFamily));

}

admin.createTable(tableDesc);

System.err.println("建表成功!");

}

admin.close();//关闭释放资源

} catch(MasterNotRunningException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(ZooKeeperConnectionException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}/*** 删除一个表

*

*@paramtableName

* 删除的表名

**/

public voiddeleteTable(String tableName) {try{

HBaseAdmin admin= newHBaseAdmin(conf);if(admin.tableExists(tableName)) {

admin.disableTable(tableName);//禁用表

admin.deleteTable(tableName);//删除表

System.err.println("删除表成功!");

}else{

System.err.println("删除的表不存在!");

}

admin.close();

}catch(MasterNotRunningException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(ZooKeeperConnectionException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}/*** 查询表中所有行

*@paramtablename*/

public voidscaner(String tablename) {try{

HTable table=newHTable(conf, tablename);

Scan s=newScan();//s.addColumn(family, qualifier)//s.addColumn(family, qualifier)

ResultScanner rs =table.getScanner(s);for(Result r : rs) {for(Cell cell:r.rawCells()){

System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");

System.out.println("Timetamp:"+cell.getTimestamp()+" ");

System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");

System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");

System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");

}

}

}catch(IOException e) {

e.printStackTrace();

}

}public voidscanerByColumn(String tablename) {try{

HTable table=newHTable(conf, tablename);

Scan s=newScan();

s.addColumn("cf".getBytes(), "201504052237".getBytes());

s.addColumn("cf".getBytes(), "201504052237".getBytes());

ResultScanner rs=table.getScanner(s);for(Result r : rs) {for(Cell cell:r.rawCells()){

System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");

System.out.println("Timetamp:"+cell.getTimestamp()+" ");

System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");

System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");

System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");

}

}

}catch(IOException e) {

e.printStackTrace();

}

}public static voidmain(String[] args) {//创建表//String tableName="test";//String cfs[] = {"cf"};//dao.createTable(tableName,cfs);//存入一条数据//Put put = new Put("bjsxt".getBytes());//put.add("cf".getBytes(), "name".getBytes(), "cai10".getBytes()) ;//dao.save(put, "test") ;//插入多列数据//Put put = new Put("bjsxt".getBytes());//List list = new ArrayList();//put.add("cf".getBytes(), "addr".getBytes(), "shanghai1".getBytes()) ;//put.add("cf".getBytes(), "age".getBytes(), "30".getBytes()) ;//put.add("cf".getBytes(), "tel".getBytes(), "13889891818".getBytes()) ;//list.add(put) ;//dao.save(list, "test");//插入单行数据//dao.insert("test", "testrow", "cf", "age", "35") ;//dao.insert("test", "testrow", "cf", "cardid", "12312312335") ;//dao.insert("test", "testrow", "cf", "tel", "13512312345") ;//List list = dao.getRows("test", "testrow",new String[]{"age"}) ;//for(Result rs : list)//{//for(Cell cell:rs.rawCells()){//System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");//System.out.println("Timetamp:"+cell.getTimestamp()+" ");//System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");//System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");//System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");//}//}//Result rs = dao.getOneRow("test", "testrow");//System.out.println(new String(rs.getValue("cf".getBytes(), "age".getBytes())));//Result rs = dao.getOneRowAndMultiColumn("cell_monitor_table", "29448-513332015-04-05", new String[]{"201504052236","201504052237"});//for(Cell cell:rs.rawCells()){//System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");//System.out.println("Timetamp:"+cell.getTimestamp()+" ");//System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");//System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");//System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");//}//dao.deleteTable("cell_monitor_table");//创建表

String tableName="cell_monitor_table";

String cfs[]= {"cf"};//dao.createTable(tableName,cfs);

}public static voidtestRowFilter(String tableName){try{

HTable table=newHTable(conf, tableName);

Scan scan= newScan();

scan.addColumn(Bytes.toBytes("column1"),Bytes.toBytes("qqqq"));

Filter filter1= new RowFilter(CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes("laoxia157")));

scan.setFilter(filter1);

ResultScanner scanner1=table.getScanner(scan);for(Result res : scanner1) {

System.out.println(res);

}

scanner1.close();//

//Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("laoxia4\d{2}"));//scan.setFilter(filter2);//ResultScanner scanner2 = table.getScanner(scan);//for (Result res : scanner2) {//System.out.println(res);//}//scanner2.close();

Filter filter3= new RowFilter( CompareOp.EQUAL,new SubstringComparator("laoxia407"));

scan.setFilter(filter3);

ResultScanner scanner3=table.getScanner(scan);for(Result res : scanner3) {

System.out.println(res);

}

scanner3.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

@Testpublic voidtestTrasaction(){try{

HTableInterface table= null;

table= hTablePool.getTable("t_test".getBytes());//Put put1 =new Put("002".getBytes());//put1.add("cf1".getBytes(), "name".getBytes(), "王五".getBytes());//table.put(put1);

Put newput =new Put("001".getBytes());

newput.add("cf1".getBytes(), "like".getBytes(), "看书".getBytes());boolean f= table.checkAndPut("001".getBytes(), "cf1".getBytes(), "age".getBytes(), "24".getBytes(), newput);

System.out.println(f);

}catch(Exception e){

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值