Invi6666 該用戶已被刪除 | 本帖最後由 Invi6666 於 2012-11-16 02:34 PM 編輯
在這比較少看到Hbase的程式碼,剛好最近寫好了一個Hbase的程式在此分享給大家,希望可以幫到一些人。
雖然是一個蠻普通的程式碼,就純粹透過程式對Hbase新增資料表,新增資料等等的功能- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- 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.HTablePool;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.client.Result;
- import org.apache.hadoop.hbase.client.ResultScanner;
- import org.apache.hadoop.hbase.client.Scan;
- import org.apache.hadoop.hbase.filter.Filter;
- import org.apache.hadoop.hbase.filter.FilterList;
- import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
- import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
- import org.apache.hadoop.hbase.util.Bytes;
- public class test { //start class
-
- public static Configuration conf;
- static {
- conf = HBaseConfiguration.create();
- }
-
- /**
- * 查詢所有數據
- * @param tableName
- */
-
- public static void createTable(String tableName, int column)
- {
-
- Scanner scanner = new Scanner(System.in);
- System.out.println("Start create table ......");
-
- try
- {
- HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
- if (hBaseAdmin.tableExists(tableName)) // 如果要創建的表已經存在,將會提示資料表已存在
- {
-
- System.out.println(tableName + " is exist");
- return ;
- }
- else
- {
- HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
-
- for(int i=0 ; i < column ; i++ )
- {
- System.out.println(i+1 + ") Insert the columnName:");
- String columnName= scanner.next();
- tableDescriptor.addFamily(new HColumnDescriptor(columnName));
- }
-
- hBaseAdmin.createTable(tableDescriptor);
- }
-
- }
- catch (MasterNotRunningException e){
- e.printStackTrace();
- }
- catch (ZooKeeperConnectionException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("End create table ......");
-
-
- }
-
- /**
- * 插入數據
- * @param tableName
- */
- public static void insertData(String tableName)
- {
-
- Scanner scanner = new Scanner(System.in);
- System.out.println("start insert data ......");
-
- try
- {
- HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
- if (!hBaseAdmin.tableExists(tableName)) // 如果存在要創建的表,
- {
-
- System.out.println(tableName + " no exist");
- return ;
- }
- else
- {
-
- HTable table = new HTable(conf,tableName);
-
-
- System.out.println("How many number of row you want to insert?");
- int row =scanner.nextInt();
-
-
- for(int i=0;i<row;i++)
- {
- System.out.println(i+1 + ") Insert Row Name:");
- String rowName= scanner.next();
-
- Put put = new Put(rowName.getBytes());
-
- System.out.println("How many number of column you want to insert?");
- int column =scanner.nextInt();
-
- for(int j=0; j<column; j++)
- {
- System.out.println(j+1 + ") Insert Column Name:");
- String columnName= scanner.next();
- System.out.println(j+1 + ") Insert Key value");
- String keyValue= scanner.next();
- put.add(columnName.getBytes(), null, keyValue.getBytes());
-
- }
- table.put(put);
-
- }
- }
- }
- catch(ZooKeeperConnectionException e)
- {
- e.printStackTrace();
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
-
- System.out.println("end insert data ......");
- }
-
- /**
- * 查詢所有數據
- * @param tableName
- */
- public static void QueryAll(String tableName) {
-
- System.out.println("start query all data ......");
-
- try {
- HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
- if (!hBaseAdmin.tableExists(tableName)) // 如果資料表不存在,將會提示錯誤信息
- {
-
- System.out.println(tableName + " no exist");
- return ;
- }
- else
- {
- HTable table = new HTable(conf,tableName);
- ResultScanner rs = table.getScanner(new Scan());
- for (Result r : rs)
- {
- System.out.println("獲得到rowkey:" + new String(r.getRow()));
- for (KeyValue keyValue : r.raw())
- {
- System.out.println("列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue()));
- }
- }
- }
-
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- System.out.println("end query all data ......");
- }
-
-
- /**
- * 根據 rowkey刪除一條記錄
- * @param tablename
- * @param rowkey
- */
-
- public static void deleteRow(String tablename, String rowkey) {
-
- System.out.println("start delete row ......");
-
- try {
- HTable table = new HTable(conf, tablename);
- List list = new ArrayList();
- Delete d1 = new Delete(rowkey.getBytes());
- list.add(d1);
-
- table.delete(list);
- System.out.println(rowkey + "delete successful");
-
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- System.out.println("end delete row ......");
- }
-
-
-
- /**
- * 刪除一張表
- * @param tableName
- */
- public static void dropTable(String tableName)
- {
- System.out.println("Start delete table ......");
- System.out.println(tableName);
- try
- {
-
- HBaseAdmin admin = new HBaseAdmin(conf);
- admin.disableTable(tableName);
- admin.deleteTable(tableName);
- }
- catch (MasterNotRunningException e) {
- e.printStackTrace();
- } catch (ZooKeeperConnectionException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- System.out.println("End delete table ......");
-
- }
-
-
-
- public static void main(String[] args) { //start main
-
- final int CREATETABLE=1;
- final int INSERTDATA=2;
- final int DELETETABLE=3;
- final int QUERYALL=4;
- final int DELETEROW=5;
- final int EXIT=6;
-
-
- int chosen=0;
- String tableName="";
- int column=0;
-
- while(chosen!=EXIT)
- {
- System.out.println("Choose the operation:");
- System.out.println("1) Create Table");
- System.out.println("2) Insert Data");
- System.out.println("3) Delete Table");
- System.out.println("4) QueryALL");
- System.out.println("5) Delete row");
- System.out.println("4) Exit");
-
- Scanner scanner = new Scanner(System.in);
- chosen=scanner.nextInt();
-
- switch(chosen)
- {
- case CREATETABLE:
- System.out.println("Insert the Table Name");
- tableName=scanner.next();
- System.out.println("How many number of familyName you want to create?");
- column =scanner.nextInt();
- createTable(tableName,column);
- break;
- case INSERTDATA:
- System.out.println("Insert the Table Name");
- tableName=scanner.next();
- insertData(tableName);
- break;
- case DELETETABLE:
- System.out.println("Insert the Table Name");
- tableName=scanner.next();
- dropTable(tableName);
- break;
- case QUERYALL:
- System.out.println("Insert the Table Name");
- tableName=scanner.next();
- QueryAll(tableName);
- break;
- case DELETEROW:
- System.out.println("Insert the Table Name");
- tableName=scanner.next();
- System.out.println("Insert the Row key that you wan delete");
- String rowKey=scanner.next();
- deleteRow(tableName,rowKey);
- break;
- case EXIT:
- System.out.println("Exit Program");
- break;
- default:
- System.out.println("Please Insert the number of operation provide");
- }
- }
- } // end main
- }// end class
複製代碼 ... |
|