V、 HBase-JavaApi

package top.gujm.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.HttpServerUtil;
import org.apache.velocity.runtime.directive.Foreach;

import java.io.IOException;

public class JavaApi {
    public static Configuration conf = null;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.113");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("zookeeper.znode.parent", "/hbase");
    }

    public static void main(String[] args) throws IOException {
//        System.out.println(existsTable("123"));
//        createTable("zhangsan", "sf1", "sf2");
//        for (int i = 0; i < 100; i++){
//            insertData("zhangsan", String.valueOf(i), "sf1","name", String.valueOf(i));
//            insertData("zhangsan", String.valueOf(i), "sf2","age", String.valueOf(i+500));
//        }
        scan("zhangsan");
    }

    /**
     * 扫描整张表
     * @param tableName
     * @throws IOException
     */
    public static  void scan(String tableName) throws IOException {
        HTable table = new HTable(conf, tableName);
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for(Result result : scanner){

            System.out.print(Bytes.toString(result.getRow())+"\t");
            Cell[] cells = result.rawCells();
            int i = 0;
            for (Cell cell : cells){
                System.out.print(i==0?"":",");
                System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+":"+Bytes.toString(CellUtil.cloneQualifier(cell))+"="+Bytes.toString(CellUtil.cloneValue(cell)));
                i++;
            }
            System.out.println();
        }
    }
    /**
     * 插入数据
     * @param tableName 表名
     * @param rowkey 行键
     * @param columnFamily 列簇
     * @param column 列名
     * @param value 值
     */
    public static void insertData(String tableName, String rowkey, String columnFamily, String column, String value) throws IOException {
        HTable table = new HTable(conf, tableName);
        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        table.put(put);
        table.close();
    }
    /**
     * 创建表
     * @param tableName 表名
     * @param columnFamilys 列族
     * @throws IOException
     */
    public static void createTable(String tableName, String... columnFamilys) throws IOException {
        if(!existsTable(tableName)) {
            Connection connection = null;
            HBaseAdmin admin = null;
            try {
                connection = ConnectionFactory.createConnection(conf);
                admin = (HBaseAdmin) connection.getAdmin();

                HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
                for (String columnFamily : columnFamilys){
                    descriptor.addFamily(new HColumnDescriptor(columnFamily));
                }
                admin.createTable(descriptor);

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                close(connection, admin);
            }
        }else {
            System.out.println("表已存在!");
        }
    }

    /**
     * 判断表是否存在
     * @param tableName
     * @return
     * @throws IOException
     */
    public static boolean existsTable(String tableName) throws IOException {
        Connection connection = null;
        HBaseAdmin admin = null;
        try {
            connection = ConnectionFactory.createConnection(conf);
            admin = (HBaseAdmin) connection.getAdmin();
            return admin.tableExists(tableName);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            close(connection, admin);
        }
        return  false;
    }

    /**
     * 关闭资源
     */
    public static  void close(Connection connection, HBaseAdmin admin){
        if(admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(V、 HBase-JavaApi)