package  com. shujia. base ; import  org. apache. hadoop. conf.  Configuration ; 
import  org. apache. hadoop. hbase.  * ; 
import  org. apache. hadoop. hbase. client.  * ; 
import  org. apache. hadoop. hbase. util.  Bytes ; 
import  org. junit.  After ; 
import  org. junit.  Before ; 
import  org. junit.  Test ; import  java. io.  * ; 
import  java. util.  ArrayList ; 
import  java. util.  Iterator ; 
import  java. util.  List ; 
public  class  HbaseAPI  { private  Connection  conn; private  Admin  admin; @Before public  void  connection ( )  { try  { 
Configuration  conf =  HBaseConfiguration . create ( ) ; conf. set ( "hbase.zookeeper.quorum" ,  "master:2181,node1:2181,node2:2181" ) ; conn =  ConnectionFactory . createConnection ( conf) ; 
admin =  conn. getAdmin ( ) ; System . out. println ( "成功获取数据库连接对象:"  +  conn) ; System . out. println ( "成功获取数据库操作对象:"  +  admin) ; System . out. println ( "=================================================" ) ; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } @Test public  void  createOneTable ( )  { TableName  name =  TableName . valueOf ( "students" ) ; 
TableDescriptorBuilder  test2 =  TableDescriptorBuilder . newBuilder ( name) ; try  { 
ColumnFamilyDescriptor  info =  ColumnFamilyDescriptorBuilder . of ( "info" ) ; 
test2. setColumnFamily ( info) ; if  ( admin. tableExists ( name) )  { System . out. println ( Bytes . toString ( name. getName ( ) )  +  " 表已经存在!" ) ; return ; } admin. createTable ( test2. build ( ) ) ; System . out. println ( Bytes . toString ( test2. build ( ) . getTableName ( ) . getName ( ) )  +  "表创建 成功 SUCCEED!" ) ; }  catch  ( Exception  e)  { System . out. println ( Bytes . toString ( test2. build ( ) . getTableName ( ) . getName ( ) )  +  "表创建 失败!FAILED!" ) ; e. printStackTrace ( ) ; } } @Test public  void  deleteOneTable ( )  { try  { TableName  name =  TableName . valueOf ( "test2" ) ; if  ( ! admin. tableExists ( name) )  { System . out. println ( Bytes . toString ( name. getName ( ) )  +  " 表不存在!无法删除!" ) ; return ; } admin. disableTable ( name) ; admin. deleteTable ( name) ; System . out. println ( Bytes . toString ( name. getName ( ) )  +  "表删除 成功 SUCCEED!" ) ; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } @Test public  void  putOneColData ( )  { try  { TableName  name =  TableName . valueOf ( "students" ) ; if  ( ! admin. tableExists ( name) )  { System . out. println ( Bytes . toString ( name. getName ( ) )  +  " 表不存在!无法添加数据!" ) ; return ; } Table  test2 =  conn. getTable ( name) ; Put  put =  new  Put ( Bytes . toBytes ( "1500100001" ) ) ; 
KeyValue  keyValue =  new  KeyValue ( Bytes . toBytes ( "1500100001" ) , Bytes . toBytes ( "info" ) , Bytes . toBytes ( "age" ) , Bytes . toBytes ( "22" ) ) ; put. add ( keyValue) ; test2. put ( put) ; System . out. println ( "一列数据添加完毕!" ) ; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } @Test public  void  putMoreData ( )  { ArrayList < Put > =  new  ArrayList < > ( ) ; try  { TableName  tableName =  TableName . valueOf ( "students" ) ; if  ( ! admin. tableExists ( tableName) )  { System . out. println ( Bytes . toString ( tableName. getName ( ) )  +  " 表不存在!无法添加数据!" ) ; return ; } Table  students =  conn. getTable ( tableName) ; BufferedReader  br =  new  BufferedReader ( new  FileReader ( "data/students.txt" ) ) ; String  line =  null ; Put  put =  null ; while  ( ( line =  br. readLine ( ) )  !=  null )  { String [ ]  infos =  line. split ( "," ) ; byte [ ]  rk =  Bytes . toBytes ( infos[ 0 ] ) ; byte [ ]  name =  Bytes . toBytes ( infos[ 1 ] ) ; put =  new  Put ( rk) ; put. addColumn ( Bytes . toBytes ( "info" ) ,  Bytes . toBytes ( "name" ) ,  name) ; puts. add ( put) ; byte [ ]  age =  Bytes . toBytes ( infos[ 2 ] ) ; put =  new  Put ( rk) ; put. addColumn ( Bytes . toBytes ( "info" ) ,  Bytes . toBytes ( "age" ) ,  age) ; puts. add ( put) ; byte [ ]  gender =  Bytes . toBytes ( infos[ 3 ] ) ; put =  new  Put ( rk) ; put. addColumn ( Bytes . toBytes ( "info" ) ,  Bytes . toBytes ( "gender" ) ,  gender) ; puts. add ( put) ; byte [ ]  clazz =  Bytes . toBytes ( infos[ 4 ] ) ; put =  new  Put ( rk) ; put. addColumn ( Bytes . toBytes ( "info" ) ,  Bytes . toBytes ( "clazz" ) ,  clazz) ; puts. add ( put) ; } students. put ( puts) ; System . out. println ( Bytes . toString ( tableName. getName ( ) )  +  " 表所有列数据添加完毕!!" ) ; }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } @Test public  void  getOneData ( )  { try  { TableName  tableName =  TableName . valueOf ( "students" ) ; if  ( ! admin. tableExists ( tableName) )  { System . out. println ( Bytes . toString ( tableName. getName ( ) )  +  " 表不存在!无法添加数据!" ) ; return ; } Table  students =  conn. getTable ( tableName) ; Get  get =  new  Get ( Bytes . toBytes ( "1500100001" ) ) ; Result  result =  students. get ( get) ; 
List < Cell > =  result. listCells ( ) ; for  ( Cell  cell :  cells)  { String  id =  Bytes . toString ( CellUtil . cloneRow ( cell) ) ; String  cf =  Bytes . toString ( CellUtil . cloneFamily ( cell) ) ; String  colName =  Bytes . toString ( CellUtil . cloneQualifier ( cell) ) ; String  colValue =  Bytes . toString ( CellUtil . cloneValue ( cell) ) ; System . out. println ( "行键:"  +  id +  ", 列簇:"  +  cf +  ", 列名:"  +  colName +  ", 列值:"  +  colValue) ; System . out. println ( "--------------------------------------------" ) ; } }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } @Test public  void  scanMoreData ( )  { try  { TableName  tableName =  TableName . valueOf ( "students" ) ; if  ( ! admin. tableExists ( tableName) )  { System . out. println ( Bytes . toString ( tableName. getName ( ) )  +  " 表不存在!无法添加数据!" ) ; return ; } Table  students =  conn. getTable ( tableName) ; Scan  scan =  new  Scan ( ) ;  
scan. withStartRow ( Bytes . toBytes ( "1500100013" ) ) ; scan. withStopRow ( Bytes . toBytes ( "1500100021" ) ,  true ) ; ResultScanner  resultScanner =  students. getScanner ( scan) ; Iterator < Result > =  resultScanner. iterator ( ) ; StringBuilder  sb =  null ; while  ( resultIterator. hasNext ( ) )  { Result  result =  resultIterator. next ( ) ; String  id =  Bytes . toString ( result. getRow ( ) ) ; 
List < Cell > =  result. listCells ( ) ; sb =  new  StringBuilder ( ) ; sb. append ( "id:" ) . append ( id) . append ( ", " ) ; for  ( int  i =  0 ;  i <  cells. size ( ) ;  i++ )  { String  colName =  Bytes . toString ( CellUtil . cloneQualifier ( cells. get ( i) ) ) ; String  colValue =  Bytes . toString ( CellUtil . cloneValue ( cells. get ( i) ) ) ; if  ( i !=  cells. size ( )  -  1 )  { sb. append ( colName) . append ( ":" ) . append ( colValue) . append ( ", " ) ; }  else  { sb. append ( colName) . append ( ":" ) . append ( colValue) ; } } System . out. println ( sb) ; System . out. println ( "--------------------------------------------" ) ; } }  catch  ( Exception  e)  { e. printStackTrace ( ) ; } } @Test public  void  createSplitTable ( ) { TableName  name =  TableName . valueOf ( "tb_split2" ) ; TableDescriptorBuilder  test2 =  TableDescriptorBuilder . newBuilder ( name) ; try  { ColumnFamilyDescriptor  info =  ColumnFamilyDescriptorBuilder . of ( "info" ) ; test2. setColumnFamily ( info) ; if  ( admin. tableExists ( name) )  { System . out. println ( Bytes . toString ( name. getName ( ) )  +  " 表已经存在!" ) ; return ; } byte [ ] [ ]  splitKeys =  { Bytes . toBytes ( "e" ) , Bytes . toBytes ( "h" ) , Bytes . toBytes ( "l" ) , Bytes . toBytes ( "r" ) } ; admin. createTable ( test2. build ( ) , splitKeys) ; System . out. println ( Bytes . toString ( test2. build ( ) . getTableName ( ) . getName ( ) )  +  "表创建 成功 SUCCEED!" ) ; }  catch  ( Exception  e)  { System . out. println ( Bytes . toString ( test2. build ( ) . getTableName ( ) . getName ( ) )  +  "表创建 失败!FAILED!" ) ; e. printStackTrace ( ) ; } } @After public  void  close ( )  { if  ( admin !=  null )  { try  { admin. close ( ) ; }  catch  ( IOException  e)  { e. printStackTrace ( ) ; } } if  ( conn !=  null )  { try  { conn. close ( ) ; }  catch  ( IOException  e)  { e. printStackTrace ( ) ; } } } }