HBase Java API 例子
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.util.Bytes; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; /** * Created by Administrator on 2017/8/16. */ public class TestHBase { Configuration configuration = null; @Before public void setUp() throws IOException { configuration = HBaseConfiguration.create(); } @Test public void testCreateTable() throws IOException { String tableName = "jasontest"; HBaseAdmin admin = new HBaseAdmin(configuration); HTableDescriptor desc = new HTableDescriptor(tableName); desc.addFamily(new HColumnDescriptor("basic")); if(admin.tableExists(tableName)){ System.out.println("table exist"); }else{ admin.createTable(desc); System.out.println("create table successfully."); } admin.close(); } @Test public void testGetByRow() throws IOException { String tableName = "user"; HTable table = new HTable(configuration,tableName); byte[] row1 = Bytes.toBytes("1000"); Get get = new Get(row1); Result result = table.get(get); Cell[] cells = result.rawCells(); for(Cell cell:cells){ System.out.println(// Bytes.toString(CellUtil.cloneFamily(cell))+":" // + Bytes.toString(CellUtil.cloneQualifier(cell))+"->" // + Bytes.toString(CellUtil.cloneValue(cell)) // ); } table.close(); } @Test public void testGetColumn() throws IOException { String tableName = "user"; HTable table = new HTable(configuration,tableName); Get get = new Get(Bytes.toBytes("1000")); get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name")); get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age")); Result result = table.get(get); Cell[] cells = result.rawCells(); for(Cell cell:cells) { System.out.println( "Column Family is " + Bytes.toString(CellUtil.cloneFamily(cell)) + " | " // + "Column is " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " | " // + "Value is " + Bytes.toString(CellUtil.cloneValue(cell)) ); } table.close(); } @Test public void testScanTable() throws IOException { String tableName = "user"; HTable table = new HTable(configuration,tableName); Scan scan = new Scan(); // scan.setStartRow(Bytes.toBytes("1001")); // scan.setStopRow(Bytes.toBytes("1001")); // scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name")); Filter filer = new PrefixFilter(Bytes.toBytes("1000")); scan.setFilter(filer); ResultScanner resultScanner = table.getScanner(scan); for(Result result:resultScanner){ Cell[] cells = result.rawCells(); for(Cell cell:cells){ System.out.println( // Bytes.toString(CellUtil.cloneRow(cell))+":"// + Bytes.toString(CellUtil.cloneFamily(cell))+":" // + Bytes.toString(CellUtil.cloneQualifier(cell))+"->" // + Bytes.toString(CellUtil.cloneValue(cell)) // ); } } table.close(); } @Test public void testPut() throws IOException { String tableName = "user"; HTable table = new HTable(configuration,tableName); Put put = new Put(Bytes.toBytes("1004")); put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("Jason Zheng")); put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(28)); table.put(put); table.close(); } @Test public void testDelete() throws IOException { String tableName = "user"; HTable table = new HTable(configuration,tableName); Delete delete = new Delete(Bytes.toBytes("1004")); delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("name")); delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("age")); // delete.deleteFamily(Bytes.toBytes("info")); table.delete(delete); table.close(); } }