자바 로 Hbase api 조작 하기

30317 단어
package com.ygy.demo;

import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class test1 {
    public static void main(String[] args) throws IOException {
        //  
        createTable("company","info","data");
        //  
        putCell("company","001","info","name","Baidu");
        putCell("company","001","data","city","Beijing");

        putCell("company","002","info","name","Tencent");
        putCell("company","002","data","city","Shenzhen");

        putCell("company","003","info","name","HUAWEI");
        putCell("company","003","data","city","Dongguan");

        //    
        getTableAll("company");
        //  API  company      “001”   
        getRowKey("company","001");
        //  API  company      “002”   。
        deleteRowKey("company","002");
        dropTable("company");


    }
    private static Connection connection =null;
    static {
        HBaseConfiguration conf = new HBaseConfiguration();
        conf.set("hbase.zookeeper.quorum","hdp001,hdp002,hdp003");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *    
     * @param tableName   
     * @param familes   
     * @throws IOException
     */
    public static void createTable(String tableName,String... familes) throws IOException {
        Admin admin = connection.getAdmin();
        try {
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            for (String famile : familes) {
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(famile);
                descriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(descriptor);
            System.out.println("    ");
        } finally {
            admin.close();
        }
    }

    /**
     *     
     * @param tableName   
     * @param rowKey    rowKey
     * @param family     
     * @param column     
     * @param value  
     * @throws IOException
     */
    public static void putCell(String tableName,String rowKey,String family,String column,String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        try {
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
            table.put(put);
            System.out.println("    ");
        }finally {
            table.close();
        }
    }

    /**
     *       
     * @param tableName
     * @throws IOException
     */
    public static void getTableAll(String tableName) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                //rowKey
                byte[] row = cell.getRow();
                String strRow = Bytes.toString(row);
                //value
                byte[] value = cell.getValue();
                String strValue = Bytes.toString(value);
                //  
                System.out.println(strRow+" : "+strValue );
            }
        }
        scanner.close();
        table.close();
    }

    /**
     *   rowKey    
     * @param tableName   
     * @param rowKey    rowKey
     * @throws IOException
     */
    public static void getRowKey(String tableName,String rowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
            for (Cell cell : result.rawCells()) {
                //rowKey
                byte[] row = cell.getRow();
                String strRow = Bytes.toString(row);
                //value
                byte[] value = cell.getValue();
                String strValue = Bytes.toString(value);
                //  
                System.out.println(strRow+" : "+strValue );
            }
        table.close();
    }

    /**
     *     
     * @param tableName   
     * @param rowKey    RowKey
     * @throws IOException
     */
    public static void deleteRowKey(String tableName,String rowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        try {
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            table.delete(delete);
            System.out.println("      ");
        } finally {
            table.close();
        }
    }

    /**   
     * @param tableName   
     * @throws IOException
     */
    public static void dropTable(String tableName) throws IOException {
        Admin admin = connection.getAdmin();
        try {
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("     ");
        } finally {
            admin.close();
        }
    }
}

좋은 웹페이지 즐겨찾기