大数据技术之HBase(三)JDBC连接HBase

一、添加依赖


    org.apache.hbase
    hbase-client
    2.3.5


    org.apache.hbase
    hbase-server
    2.3.5

二、windows主机映射

修改hosts文件,配置映射

三、代码编写

log4j.properties

hadoop.root.logger=DEBUG, console
log4j.rootLogger = ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
public class AppTest 
{
    static Configuration config  = null;
    private Connection conn = null;
    private Admin admin = null;

    @Before
    public void init() throws IOException {
        System.out.println("执行init()方法");

        config = HBaseConfiguration.create();
        config.set(HConstants.HBASE_DIR,"hdfs://192.168.153.139:9000/hbase");
        config.set(HConstants.ZOOKEEPER_QUORUM,"192.168.153.139");
        config.set(HConstants.CLIENT_PORT_STR,"2181");

        conn = ConnectionFactory.createConnection(config);

        admin = conn.getAdmin();
    }

    @Test
    public void test1(){
        System.out.println(conn);
        System.out.println("执行test1方法");
    }

    /**
     * 创建命名空间
     */
    @Test
    public void createNameSpace() throws IOException {
        NamespaceDescriptor kb21 = NamespaceDescriptor.create("kb21").build();

        admin.createNamespace(kb21);
    }

    /**
     * 创建表
     */
    @Test
    public void createTable() throws IOException {
        // 创建表的描述类
        TableName tableName = TableName.valueOf("kb21:student");
        HTableDescriptor desc = new HTableDescriptor(tableName);

        // 创建列族的描述类
        HColumnDescriptor family1 = new HColumnDescriptor("info1");
        HColumnDescriptor family2 = new HColumnDescriptor("info2");
        desc.addFamily(family1);
        desc.addFamily(family2);

        admin.createTable(desc);
    }

    /**
     * 删除表
     */
    @Test
    public void deleteTable() throws IOException {
        admin.disableTable(TableName.valueOf("kb21:student"));
        admin.deleteTable(TableName.valueOf("kb21:student"));
    }

    @Test
    public void queryData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("kb21:student"));
        Get get = new Get(Bytes.toBytes("student1"));
        Result result = table.get(get);
        byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
        System.out.println("姓名:" + Bytes.toString(value));
        value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
        System.out.println("学校:" + Bytes.toString(value));
    }

    @Test
    public void scanData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("kb21:student"));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result result :
                scanner) {
            byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
            System.out.println("姓名:" + Bytes.toString(value));
            value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
            System.out.println("学校:" + Bytes.toString(value));
            System.out.println(Bytes.toString(result.getRow()));
        }

    }

    /**
     * 获取namespace/table列表
     * @throws IOException
     */
    @Test
    public void getAllNamespace() throws IOException {
//        String[] nps = admin.listNamespaces();
//        String s = Arrays.toString(nps);
//        System.out.println(s);

        List tableDesc = admin.listTableDescriptorsByNamespace("kb21".getBytes());
        System.out.println(tableDesc.toString());
    }

    @Test
    public void insertData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("kb21:student"));

//        Put put = new Put(Bytes.toBytes("student1"));
//        put.addColumn("info1".getBytes(),"name".getBytes(),"zs".getBytes());
//        put.addColumn("info2".getBytes(),"school".getBytes(),"njzb".getBytes());

        Put put2 = new Put(Bytes.toBytes("student2"));
        put2.addColumn("info1".getBytes(),"name".getBytes(),"zss".getBytes());
        put2.addColumn("info2".getBytes(),"school".getBytes(),"njzb".getBytes());
        Put put3 = new Put(Bytes.toBytes("student3"));
        put3.addColumn("info1".getBytes(),"name".getBytes(),"zsr".getBytes());
        put3.addColumn("info2".getBytes(),"school".getBytes(),"njzb".getBytes());

//        table.put(put);
        ArrayList list = new ArrayList<>();
        list.add(put2);
        list.add(put3);
        table.put(list);
    }

    @After
    public void close() throws IOException {
        System.out.println("执行close()方法");
        if(admin!=null){
            admin.close();
        }
        if(conn!=null){
            conn.close();
        }
    }
}

你可能感兴趣的:(大数据,hbase)