hbase之前版本有些api已经deprecated了,下面是1.2的api,没有deprecated
java.io.IOException; java.util.ArrayList; java.util.List; java.util.concurrent.ExecutorService; java.util.concurrent.Executors; org.apache.hadoop.conf.Configuration; org.apache.hadoop.hbase.Cell; org.apache.hadoop.hbase.HBaseConfiguration; org.apache.hadoop.hbase.HColumnDescriptor; org.apache.hadoop.hbase.HTableDescriptor; org.apache.hadoop.hbase.MasterNotRunningException; org.apache.hadoop.hbase.TableName; org.apache.hadoop.hbase.ZooKeeperConnectionException; org.apache.hadoop.hbase.client.Admin; org.apache.hadoop.hbase.client.Connection; org.apache.hadoop.hbase.client.ConnectionFactory; org.apache.hadoop.hbase.client.Delete; org.apache.hadoop.hbase.client.Get; org.apache.hadoop.hbase.client.Put; org.apache.hadoop.hbase.client.Result; org.apache.hadoop.hbase.client.ResultScanner; org.apache.hadoop.hbase.client.Scan; org.apache.hadoop.hbase.client.Table; org.apache.hadoop.hbase.util.Bytes; { Configuration conf HBaseConfiguration.create(); ExecutorService poolExecutors.newScheduledThreadPool(); Connection connection ; HBaseUtils instance; HBaseUtils(){ (connection){ { connectionConnectionFactory.createConnection(conf, pool); } (IOException e) { e.printStackTrace(); } } } HBaseUtils getInstance(){ (instance){ instance HBaseUtils(); } instance; } createTable( tableName, [] columns) IOException { Admin admin connection.getAdmin(); TableName nameTableName.valueOf(tableName); (admin.tableExists(name)) { admin.disableTable(name); admin.deleteTable(name); } { HTableDescriptor desc admin.getTableDescriptor(name); ( column : columns) { desc.addFamily( HColumnDescriptor(column)); } admin.createTable(desc); } } insertRecord( tableName, row, columnFamily, [] columns, [] values) { { TableName nameTableName.valueOf(tableName); Table table connection.getTable(name); Put put Put(Bytes.toBytes(row)); ( i ; i columns.length; i) { put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(.valueOf(columns[i])), Bytes.toBytes(values[i])); table.put(put); } } (IOException e) { e.printStackTrace(); } } deleteRow( tablename, rowkey) IOException { TableName nameTableName.valueOf(tablename); Table table connection.getTable(name); ListDelete list ArrayListDelete(); Delete d1 Delete(rowkey.getBytes()); list.add(d1); table.delete(list); } selectRow( tablename, rowKey) IOException { TableName nameTableName.valueOf(tablename); Table table connection.getTable(name); Get g Get(rowKey.getBytes()); Result rs table.get(g); (Cell cell : rs.rawCells()) { System.out.print( (cell.getRowArray()) ); System.out.print( (cell.getFamilyArray()) ); System.out.print( (cell.getQualifierArray()) ); System.out.print(cell.getTimestamp() ); System.out.println( (cell.getValueArray())); } } List scanAllRecord( tablename) { { List entityList ArrayList(); TableName nameTableName.valueOf(tablename); Table table connection.getTable(name); Scan s Scan(); ResultScanner rs table.getScanner(s); (Result result : rs){ (Cell cell : result.rawCells()) { HBaseBaseEntity entity HBaseBaseEntity(); entity.setRowKey( (CellUtil.cloneRow(cell))); entity.setFamily( (CellUtil.cloneFamily(cell))); entity.setQualifier( (CellUtil.cloneQualifier(cell))); entity.setTimestamp(cell.getTimestamp()); entity.setValue(CellUtil.cloneValue(cell)); entityList.add(entity); } } entityList; } (IOException e) { e.printStackTrace(); } ; } deleteTable( tablename) IOException { { TableName nameTableName.valueOf(tablename); (admin.tableExists(name)){ admin.disableTable(name); admin.deleteTable(name); } } (MasterNotRunningException e) { e.printStackTrace(); } (ZooKeeperConnectionException e) { e.printStackTrace(); } } }