暑假周進度報告(八)

HBASE java

http://dblab.xmu.edu.cn/blog/install-hbase/數據庫

  1. cd /usr/local/hadoop
  2. ./sbin/start-dfs.sh
  3. cd /usr/local/hbase
  4. ./bin/start-hbase.sh

 數據庫鏈接關閉增刪改查樣例apache

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
 
public class ExampleForHbase{
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
 
    //主函數中的語句請逐句執行,只需刪除其前的//便可,如:執行insertRow時請將其餘語句註釋
    public static void main(String[] args)throws IOException{
        //建立一個表,表名爲Score,列族爲sname,course
        createTable("Score",new String[]{"sname","course"});
 
        //在Score表中插入一條數據,其行鍵爲95001,sname爲Mary(由於sname列族下沒有子列因此第四個參數爲空)
        //等價命令:put 'Score','95001','sname','Mary'
        //insertRow("Score", "95001", "sname", "", "Mary");
        //在Score表中插入一條數據,其行鍵爲95001,course:Math爲88(course爲列族,Math爲course下的子列)
        //等價命令:put 'Score','95001','score:Math','88'
        //insertRow("Score", "95001", "course", "Math", "88");
        //在Score表中插入一條數據,其行鍵爲95001,course:English爲85(course爲列族,English爲course下的子列)
        //等價命令:put 'Score','95001','score:English','85'
        //insertRow("Score", "95001", "course", "English", "85");
 
        //一、刪除Score表中指定列數據,其行鍵爲95001,列族爲course,列爲Math
        //執行這句代碼前請deleteRow方法的定義中,將刪除指定列數據的代碼取消註釋註釋,將刪除制定列族的代碼註釋
        //等價命令:delete 'Score','95001','score:Math'
        //deleteRow("Score", "95001", "course", "Math");
 
        //二、刪除Score表中指定列族數據,其行鍵爲95001,列族爲course(95001的Math和English的值都會被刪除)
        //執行這句代碼前請deleteRow方法的定義中,將刪除指定列數據的代碼註釋,將刪除制定列族的代碼取消註釋
        //等價命令:delete 'Score','95001','score'
        //deleteRow("Score", "95001", "course", "");
 
        //三、刪除Score表中指定行數據,其行鍵爲95001
        //執行這句代碼前請deleteRow方法的定義中,將刪除指定列數據的代碼註釋,以及將刪除制定列族的代碼註釋
        //等價命令:deleteall 'Score','95001'
        //deleteRow("Score", "95001", "", "");
 
        //查詢Score表中,行鍵爲95001,列族爲course,列爲Math的值
        //getData("Score", "95001", "course", "Math");
        //查詢Score表中,行鍵爲95001,列族爲sname的值(由於sname列族下沒有子列因此第四個參數爲空)
        //getData("Score", "95001", "sname", "");
 
        //刪除Score表
        //deleteTable("Score");
    }
 
    //創建鏈接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //關閉鏈接
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
 
    /**
     * 建表。HBase的表中會有一個系統默認的屬性做爲主鍵,主鍵無需自行建立,默認爲put命令操做中表名後第一個數據,所以此處無需建立id列
     * @param myTableName 表名
     * @param colFamily 列族名
     * @throws IOException
     */
    public static void createTable(String myTableName,String[] colFamily) throws IOException {
 
        init();
        TableName tableName = TableName.valueOf(myTableName);
 
        if(admin.tableExists(tableName)){
            System.out.println("talbe is exists!");
        }else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for(String str:colFamily){
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
            System.out.println("create table success");
        }
        close();
    }
    /**
     * 刪除指定表
     * @param tableName 表名
     * @throws IOException
     */
    public static void deleteTable(String tableName) throws IOException {
        init();
        TableName tn = TableName.valueOf(tableName);
        if (admin.tableExists(tn)) {
            admin.disableTable(tn);
            admin.deleteTable(tn);
        }
        close();
    }
 
    /**
     * 查看已有表
     * @throws IOException
     */
    public static void listTables() throws IOException {
        init();
        HTableDescriptor hTableDescriptors[] = admin.listTables();
        for(HTableDescriptor hTableDescriptor :hTableDescriptors){
            System.out.println(hTableDescriptor.getNameAsString());
        }
        close();
    }
    /**
     * 向某一行的某一列插入數據
     * @param tableName 表名
     * @param rowKey 行鍵
     * @param colFamily 列族名
     * @param col 列名(若是其列族下沒有子列,此參數可爲空)
     * @param val 值
     * @throws IOException
     */
    public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
        table.put(put);
        table.close();
        close();
    }
 
    /**
     * 刪除數據
     * @param tableName 表名
     * @param rowKey 行鍵
     * @param colFamily 列族名
     * @param col 列名
     * @throws IOException
     */
    public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(rowKey.getBytes());
        //刪除指定列族的全部數據
        //delete.addFamily(colFamily.getBytes());
        //刪除指定列的數據
        //delete.addColumn(colFamily.getBytes(), col.getBytes());
 
        table.delete(delete);
        table.close();
        close();
    }
    /**
     * 根據行鍵rowkey查找數據
     * @param tableName 表名
     * @param rowKey 行鍵
     * @param colFamily 列族名
     * @param col 列名
     * @throws IOException
     */
    public static void getData(String tableName,String rowKey,String colFamily,String col)throws  IOException{
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        showCell(result);
        table.close();
        close();
    }
    /**
     * 格式化輸出
     * @param result
     */
    public static void showCell(Result result){
        Cell[] cells = result.rawCells();
        for(Cell cell:cells){
            System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
            System.out.println("Timetamp:"+cell.getTimestamp()+" ");
            System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
            System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
            System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
        }
    }
}
相關文章
相關標籤/搜索