HBase Java programming example
HelloWorld.zip
- Package elementary;
- Import java. io. IOException;
- Import java. text. SimpleDateFormat;
- Import java. util. ArrayList;
- Import java. util. Date;
- Import java. util. List;
- Import java. util. concurrent. atomic. AtomicInteger;
- Import java. util. concurrent. ExecutorService;
- Import java. util. concurrent. Executors;
- Import java. util. concurrent. TimeUnit;
- Import org. apache. hadoop. conf. Configuration;
- Import org. apache. hadoop. hbase. Cell;
- Import org. apache. hadoop. hbase. HBaseConfiguration;
- Import org. apache. hadoop. hbase. HColumnDescriptor;
- Import org. apache. hadoop. hbase. HTableDescriptor;
- Import org. apache. hadoop. hbase. MasterNotRunningException;
- Import org. apache. hadoop. hbase. TableName;
- Import org. apache. hadoop. hbase. ZooKeeperConnectionException;
- Import org. apache. hadoop. hbase. client. Delete;
- Import org. apache. hadoop. hbase. client. Get;
- Import org. apache. hadoop. hbase. client. Admin;
- Import org. apache. hadoop. hbase. client. BufferedMutator;
- Import org. apache. hadoop. hbase. client. BufferedMutatorParams;
- Import org. apache. hadoop. hbase. client. Connection;
- Import org. apache. hadoop. hbase. client. ConnectionFactory;
- Import org. apache. hadoop. hbase. client. Table;
- Import org. apache. hadoop. hbase. client. Put;
- Import org. apache. hadoop. hbase. client. Result;
- Import org. apache. hadoop. hbase. client. resultworkflow;
- Import org. apache. hadoop. hbase. client. Scan;
- Import org. apache. hadoop. hbase. util. Bytes;
- Import org. apache. hadoop. util. ThreadUtil;
- Public class HelloWorld {
- Private static Configuration conf = null;
- Private static Connection conn = null;
- Private static Admin admin = null;
- Public static AtomicInteger count = new AtomicInteger ();
- /**
- * Initialization Configuration
- */
- Static {
- Conf = HBaseConfiguration. create ();
- // If no configuration file exists, you must remember the manual announcement.
- Conf. set ("hbase. zookeeper. quorum", "10.148.137.143 ");
- Conf. set ("hbase. zookeeper. property. clientPort", "2181 ");
- }
- Static {
- Try {
- Conn = ConnectionFactory. createConnection ();
- Admin = conn. getAdmin ();
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- Static public class MyThread extends Thread
- {
- Int _ start;
- String _ tablename;
- Connection conn;
- // BufferedMutator table;
- Table table;
- Public MyThread (int start, String tablename ){
- _ Start = start;
- _ Tablename = tablename;
- }
- Public void run (){
- String tablename = _ tablename;
- Thread current = Thread. currentThread ();
- Long thread_id = current. getId ();
- System. out. printf ("thread [% d] run \ n", thread_id );
- Try {
- Conn = ConnectionFactory. createConnection ();
- // BufferedMutatorParams params = new BufferedMutatorParams (TableName. valueOf (tablename ));
- // Params. writeBufferSize (1024*4 );
- // Table = conn. getBufferedMutator (params );
- Table = conn. getTable (TableName. valueOf (tablename ));
- For (int j = _ start; j <100; ++ j ){
- For (int I = 0; I <10000000; ++ I ){
- // Zkb_0_0
- String zkb = "zkb _" + String. valueOf (_ start) + "_" + String. valueOf (I );
- Put put = new Put (Bytes. toBytes (zkb ));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field1"), Bytes. toBytes (String. valueOf (I + 0 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field2"), Bytes. toBytes (String. valueOf (I + 1 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field3"), Bytes. toBytes (String. valueOf (I + 2 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field4"), Bytes. toBytes (String. valueOf (I + 3 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field5"), Bytes. toBytes (String. valueOf (I + 4 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field6"), Bytes. toBytes (String. valueOf (I + 5 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field7"), Bytes. toBytes (String. valueOf (I + 6 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field8"), Bytes. toBytes (String. valueOf (I + 7 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field9"), Bytes. toBytes (String. valueOf (I + 8 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field10"), Bytes. toBytes (String. valueOf (I + 9 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field11"), Bytes. toBytes (String. valueOf (I + 10 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field12"), Bytes. toBytes (String. valueOf (I + 11 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field13"), Bytes. toBytes (String. valueOf (I + 12 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field14"), Bytes. toBytes (String. valueOf (I + 13 )));
- Put. addColumn (Bytes. toBytes ("grade"), Bytes. toBytes ("field15"), Bytes. toBytes (String. valueOf (I + 14 )));
- // Table. mutate (put );
- Table. put (put );
- Int m = HelloWorld. count. incrementAndGet ();
- If (m % 10000 = 0 ){
- Date dt = new Date ();
- SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss aa ");
- String now = sdf. format (dt );
- System. out. printf ("[% s] thread [% d] m = % d, j = % d, I = % d \ n", now, thread_id, m, j, I );
- }
- }
- }
- System. out. printf ("thread [% d] over \ n", thread_id );
- }
- Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- }
- /**
- * Create a table
- * @ Param tablename
- * @ Param cfs
- */
- Public static void createTable (String tablename, String [] cfs ){
- Try {
- If (admin. tableExists (TableName. valueOf (tablename ))){
- System. out. println ("table already exists! ");
- } Else {
- HTableDescriptor tableDesc = new HTableDescriptor (TableName. valueOf (tablename ));
- For (int I = 0; I <cfs. length; I ++ ){
- HColumnDescriptor desc = new HColumnDescriptor (cfs [I]);
- Desc. setMaxVersions (3650 );
- TableDesc. addFamily (desc );
- }
- Byte [] [] splitKeys = new byte [] [] {
- Bytes. toBytes ("zkb_0_0 "),
- Bytes. toBytes ("zkb_10_0 "),
- Bytes. toBytes ("zkb_20_0 "),
- Bytes. toBytes ("zkb_30_0 "),
- Bytes. toBytes ("zkb_40_0 "),
- Bytes. toBytes ("zkb_50_0 "),
- Bytes. toBytes ("zkb_60_0 "),
- Bytes. toBytes ("zkb_70_0 "),
- Bytes. toBytes ("zkb_80_0 "),
- Bytes. toBytes ("zkb_90_0 "),
- Bytes. toBytes ("zkb_100_0 ")
- };
- Admin. createTable (tableDesc, splitKeys );
- Admin. close ();
- System. out. println ("create table" + tablename + "OK .");
- }
- } Catch (MasterNotRunningException e ){
- E. printStackTrace ();
- } Catch (ZooKeeperConnectionException e ){
- E. printStackTrace ();
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- /**
- * Except tables
- * @ Param tablename
- */
- Public static void deleteTable (String tablename ){
- Try {
- // Connection conn = ConnectionFactory. createConnection ();
- // Admin admin = conn. getAdmin ();
- Admin. disableTable (TableName. valueOf (tablename ));
- Admin. deleteTable (TableName. valueOf (tablename ));
- System. out. println ("delete table" + tablename + "OK .");
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- /**
- * Just remove one item
- * @ Param tableName
- * @ Param rowKey
- */
- Public static void delRecord (String tableName, String rowKey ){
- Try {
- Table table = conn. getTable (TableName. valueOf (tableName ));
- List <Delete> list = new ArrayList <Delete> ();
- Delete del = new Delete (rowKey. getBytes ());
- List. add (del );
- Table. delete (list );
- System. out. println ("del recored" + rowKey + "OK .");
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- /**
- * Obtain a metadata
- * @ Param tableName
- * @ Param rowKey
- */
- Public static void getOneRecord (String tableName, String rowKey ){
- Try {
- Table table = conn. getTable (TableName. valueOf (tableName ));
-
- Get get = new Get (rowKey. getBytes ());
- Result rs = table. get (get );
- List <Cell> list = rs. listCells ();
- For (Cell cell: list ){
- System. out. print (new String (cell. getRowArray (), cell. getRowOffset (), cell. getRowLength () + "");
- System. out. print (new String (cell. getFamilyArray (), cell. getFamilyOffset (), cell. getFamilyLength () + ":");
- System. out. print (new String (cell. getQualifierArray (), cell. getQualifierOffset (), cell. getQualifierLength () + "");
- System. out. print (cell. getTimestamp () + "");
- System. out. print (new String (cell. getValueArray (), cell. getValueOffset (), cell. getValueLength () + "");
- System. out. println ("");
- }
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- /**
- * Retrieve all information
- * @ Param tableName
- */
- Public static void getAllRecord (String tableName ){
- Try {
- // Connection conn = ConnectionFactory. createConnection ();
- Table table = conn. getTable (TableName. valueOf (tableName ));
- Scan scan = new Scan ();
- Resultpartition = table. getpartition (scan );
- For (Result rs: resulttasks ){
- List <Cell> list = rs. listCells ();
- For (Cell cell: list ){
- System. out. print (new String (cell. getRowArray (), cell. getRowOffset (), cell. getRowLength () + "");
- System. out. print (new String (cell. getFamilyArray (), cell. getFamilyOffset (), cell. getFamilyLength () + ":");
- System. out. print (new String (cell. getQualifierArray (), cell. getQualifierOffset (), cell. getQualifierLength () + "");
- System. out. print (cell. getTimestamp () + "");
- System. out. print (new String (cell. getValueArray (), cell. getValueOffset (), cell. getValueLength () + "");
- System. out. println ("");
- }
- }
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- /**
- * Retrieve the Family ticket
- * @ Param tableName
- * @ Return
- */
- Public static ArrayList <String> getAllFamilyName (String tableName ){
- ArrayList <String> familyname_list = new ArrayList <String> ();
- Try {
- // Connection conn = ConnectionFactory. createConnection ();
- Table table = conn. getTable (TableName. valueOf (tableName ));
- HTableDescriptor htabledescriptor = table. getTableDescriptor ();
- HColumnDescriptor [] hdlist = htabledescriptor. getColumnFamilies ();
- For (int I = 0; I
- HColumnDescriptor hd = hdlist [I];
- Familyname_list.add (hd. getNameAsString ());
- }
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- Return familyname_list;
- }
- // Java-cp HelloWorld. jar: 'ls lib/*. jar | awk '{printf ("% s:", $0)} ''elementary. HelloWorld 5
- Public static void main (String [] args ){
- System. out. println ("HelloWorldX ");
- If (args. length> 0)
- System. out. println (args [0]);
- Int start = 0;
- If (args. length> 1)
- Start = Integer. valueOf (args [1]);
- If (start <0)
- Start = 0;
- Int num_threads = 16;
- If (args. length> 2)
- Num_threads = Integer. valueOf (args [2]);
- Try {
- String tablename = "scores ";
- String [] familys = {"grade", "course "};
- HelloWorld. createTable (tablename, familys );
- // ExecutorService thread_pool = Executors. newSingleThreadExecutor ();
- ExecutorService thread_pool = Executors. newFixedThreadPool (num_threads );
- Thread [] pool = new HelloWorld. MyThread [80];
- For (int I = 0; I <pool. length; ++ I ){
- Pool [I] = new HelloWorld. MyThread (I, tablename );
- Thread_pool.execute (pool [I]);
- }
- Thread_pool.shutdown ();
- System. out. println ("over ");
- }
- Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- }