Packagecom. cma. hbase. test; importjava. io. bufferedInputStream; importjava. io. bufferedReader; importjava. io. file; importjava. io. fileInputStream; importjava. io. fileNotFoundException; importjava. io. fileReader; importjava. io. IOException
Package com. cma. hbase. test; import java. io. bufferedInputStream; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileReader; import java. io. IOException
Package com. cma. hbase. test;
Import java. io. BufferedInputStream;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileReader;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. Reader;
Import java. util. ArrayList;
Import java. util. List;
Import org. apache. commons. io. FileUtils;
Import org. apache. commons. lang. StringUtils;
Import org. apache. hadoop. conf. Configuration;
Import org. apache. hadoop. hbase. HColumnDescriptor;
Import org. apache. hadoop. hbase. HTableDescriptor;
Import org. apache. hadoop. hbase. KeyValue;
Import org. apache. hadoop. hbase. MasterNotRunningException;
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. HBaseAdmin;
Import org. apache. hadoop. hbase. client. HTable;
Import org. apache. hadoop. hbase. client. Put;
Import org. apache. hadoop. hbase. client. Result;
Import org. apache. hadoop. hbase. util. Bytes;
Import com. cma. hbase. Constants;
Import com. cma. hbase. entity. DataModel;
Import com. cma. hbase. tools. HbaseUtils;
Public class HbaseTest {
?? ? Public static void main (String [] args ){
?? ??? ? Create a table
?? ??? ? CreateTable ("hello_baby", new String [] {"code", "ws", "wd", "t", "ps", "rh", "vis ", "r "});
?? ??? ? Write a data record
?? ??? ? WriteRecord ("hello_baby", "row1", "code", "", "code ");
?? ??? ? WriteRecord ("hello_baby", "row1", "ws", "", "ws ");
?? ??? ? WriteRecord ("hello_baby", "row1", "wd", "", "wd ");
?? ??? ? WriteRecord ("hello_baby", "row1", "t", "", "t ");
?? ??? ? WriteRecord ("hello_baby", "row1", "ps", "", "ps ");
?? ??? ? WriteRecord ("hello_baby", "row1", "rh", "", "rh ");
?? ??? ? WriteRecord ("hello_baby", "row1", "vis", "", "vis ");
?? ??? ? WriteRecord ("hello_baby", "row1", "r", "", "r ");
?? ??? ? Write a group of data
?? ??? ? WriteRecordList ("hello_baby ");
?? ??? ? // Query a piece of data
?? ??? ? GetRecord ("hello_baby", "row1 ");
?? ??? ? Delete a row
?? ??? ? DeleteRecord ("hello_baby", "row1 ");
?? ??? ? Delete a table
?? ??? ? DropTable ("hello_baby ");
?? ??? ? Clear a table
?? ??? ? ClearTable ("hello_baby ");
?? ?}
?? ? /**
?? ? * Clear a table
?? ? * @ Param string
?? ? */
?? ? Private static void clearTable (String tableName ){
?? ??? ? Configuration cfg = HbaseUtils. getCfg ();
?? ??? ?
?? ??? ? Try {
?? ??? ??? ? HBaseAdmin admin = new HBaseAdmin (cfg );
?? ??? ??? ?
?? ??? ?} Catch (MasterNotRunningException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Catch (ZooKeeperConnectionException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Catch (IOException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Finally {
?? ??? ??? ? System. out. println ("tableName:" + tableName + "drop over! ");
?? ??? ?}
?? ?}
?? ? /**
?? ? * Write a group of data
?? ? * @ Param tableName
?? ? */
?? ? Private static void writeRecordList (String tableName ){
?? ??? ? Long start = System. currentTimeMillis ();
?? ??? ? Configuration cfg = HbaseUtils. getCfg ();
?? ??? ? Try {
?? ??? ??? ? HTable htable = new HTable (cfg, tableName );
?? ??? ??? ? List Puts = getPuts ();
?? ??? ??? ? System. out. println (puts. size ());
?? ??? ??? ? Htable. put (puts );
?? ??? ?} Catch (IOException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Finally {
?? ??? ??? ? System. out. println ("tableName:" + tableName + "write over! ");
?? ??? ?}
?? ??? ? Long end = System. currentTimeMillis ();
?? ??? ? System. out. println ("cost time:" + (end-start ));
?? ?}
?? ? Private static List GetPuts (){
?? ??? ? List PutList = new ArrayList ();
?? ??? ? List PutRecord = null;
?? ??? ? Try {
?? ??? ??? ? List Lines = FileUtils. readLines (new File ("/home/guest/data/201307310800.csv "));
?? ??? ??? ? For (int I = 1; I <lines. size (); I ++ ){
?? ??? ??? ??? ? PutRecord = getPutsByLine (lines. get (I ));
?? ??? ??? ??? ? PutList. addAll (putRecord );
?? ??? ??? ?}
?? ??? ?} Catch (IOException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?}
?? ??? ? Return putList;
?? ?}
?? ? /**
?? ? * Get a set of Put
?? ? * @ Param line
?? ? * @ Return
?? ? */
?? ? Private static List GetPutsByLine (String line ){
?? ??? ? List Puts = new ArrayList ();
?? ??? ? Put put = null;
?? ??? ?
?? ??? ? If (StringUtils. isNotBlank (line )){
?? ??? ??? ? String [] columns = line. split (",");
?? ??? ??? ? String [] families = Constants. FAMILIES;
?? ??? ??? ? String rowKey = "201307310800" + columns [0];
?? ??? ??? ? For (int I = 0; I <columns. length; I ++ ){
?? ??? ??? ??? ? String family = families [I];
?? ??? ??? ??? ? String qualifier = "";
?? ??? ??? ??? ? String value = columns [I];
?? ??? ??? ??? ? Put = getPut (rowKey, family, qualifier, value );
?? ??? ??? ??? ? Puts. add (put );
?? ??? ??? ?}
?? ??? ?}
?? ??? ? Return puts;
?? ?}
?? ? /**
?? ? * Assemble a Put
?? ? * @ Param rowKey
?? ? * @ Param family
?? ? * @ Param qualifier
?? ? * @ Param value
?? ? * @ Return
?? ? */
?? ? Private static Put getPut (String rowKey, String family, String qualifier,
?? ??? ??? ? String value ){
?? ??? ? Put put = new Put (Bytes. toBytes (rowKey ));
?? ??? ? Put. add (Bytes. toBytes (family), Bytes. toBytes (qualifier), Bytes. toBytes (value ));
?? ??? ? Return put;
?? ?}
?? ? /**
?? ? * A piece of data is queried.
?? ? * @ Param tableName
?? ? * @ Param rowKey
?? ? */
?? ? Private static void getRecord (String tableName, String rowKey ){
?? ??? ? Configuration cfg = HbaseUtils. getCfg ();
?? ??? ? Try {
?? ??? ??? ? HTable htable = new HTable (cfg, tableName );
?? ??? ??? ? Get get = new Get (Bytes. toBytes (rowKey ));
?? ??? ??? ? Result rs = htable. get (get );
?? ??? ??? ?
?? ??? ??? ? For (KeyValue kv: rs. raw ()){
?? ??? ??? ??? ? System. out. print (new String (kv. getRow () + "***");
?? ??? ??? ??? ? System. out. print (new String (kv. getFamily () + "***");
?? ??? ??? ??? ? System. out. print (new String (kv. getQualifier () + "***");
?? ??? ??? ??? ? System. out. print (new String (kv. getValue ()));
?? ??? ??? ??? ? System. out. println ();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?} Catch (IOException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?}
?? ?}
?? ? /**
?? ? * Delete a table
?? ? * @ Param tableName
?? ? */
?? ? Private static void dropTable (String tableName ){
?? ??? ? Configuration cfg = HbaseUtils. getCfg ();
?? ??? ?
?? ??? ? Try {
?? ??? ??? ? HBaseAdmin admin = new HBaseAdmin (cfg );
?? ??? ??? ? Admin. disableTable (tableName );
?? ??? ??? ? Admin. deleteTable (tableName );
?? ??? ?} Catch (MasterNotRunningException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Catch (ZooKeeperConnectionException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Catch (IOException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Finally {
?? ??? ??? ? System. out. println ("tableName:" + tableName + "drop over! ");
?? ??? ?}
?? ?}
?? ? /**
?? ? * Delete a row
?? ? * @ Param tableName
?? ? * @ Param rowKey
?? ? */
?? ? Private static void deleteRecord (String tableName, String rowKey ){
?? ??? ? Configuration cfg = HbaseUtils. getCfg ();
?? ??? ?
?? ??? ? Try {
?? ??? ??? ? HTable htable = new HTable (cfg, tableName );
?? ??? ??? ? Delete del = new Delete (Bytes. toBytes (rowKey ));
?? ??? ??? ? Htable. delete (del );
?? ??? ?} Catch (IOException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Finally {
?? ??? ??? ? System. out. println ("rowKey:" + rowKey + "delete over! ");
?? ??? ?}
?? ?}
?? ? /**
?? ? * Store a column
?? ? * @ Param tableName
?? ? * @ Param rowKey
?? ? * @ Param family
?? ? * @ Param qualifier
?? ? * @ Param value
?? ? */
?? ? Private static void writeRecord (String tableName, String rowKey, String family, String qualifier, String value ){
?? ??? ? Configuration cfg = HbaseUtils. getCfg ();
?? ??? ? Try {
?? ??? ??? ? HTable htable = new HTable (cfg, tableName );
?? ??? ??? ? Put put = new Put (Bytes. toBytes (rowKey ));
?? ??? ??? ? Put. add (Bytes. toBytes (family), Bytes. toBytes (qualifier), Bytes. toBytes (value ));
?? ??? ??? ? Htable. put (put );
?? ??? ??? ?
?? ??? ?} Catch (IOException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Finally {
?? ??? ??? ? System. out. println ("family:" + family + "put over! ");
?? ??? ?}
?? ?}
?? ? /**
?? ? * Create a table
?? ? * @ Param tableName
?? ? * @ Param families
?? ? */
?? ? Private static void createTable (String tableName, String [] families ){
?? ??? ? HBaseAdmin admin = null;
?? ??? ? Try {
?? ??? ??? ? Configuration cfg = HbaseUtils. getCfg ();
?? ??? ??? ? System. out. println (cfg );
?? ??? ??? ? Admin = new HBaseAdmin (cfg );
?? ??? ??? ? If (admin. tableExists (tableName )){
?? ??? ??? ??? ? System. out. println ("table:" + tableName + "already exists! ");
?? ??? ??? ?} Else {
?? ??? ??? ??? ? HTableDescriptor tableDesc = new HTableDescriptor (tableName );
?? ??? ??? ??? ? For (String column: families ){
?? ??? ??? ??? ??? ? TableDesc. addFamily (new HColumnDescriptor (column ));
?? ??? ??? ??? ?}
?? ??? ??? ??? ? Admin. createTable (tableDesc );
?? ??? ??? ?}
?? ??? ?} Catch (MasterNotRunningException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Catch (ZooKeeperConnectionException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Catch (IOException e ){
?? ??? ??? ? E. printStackTrace ();
?? ??? ?} Finally {
?? ??? ??? ? System. out. println ("over! ");
?? ??? ?}
?? ?}
?? ?
}
Author: lovemelovemycode was published at 20:35:24, November 25 ,.
Read: 97 comments: 0 view comments
Original article address: the basic example of operating Hbase in JAVA. Thank you for sharing it with the original author.