Hbase API

hbase版本:2.3.5

1、创建maven工程,引入pom依赖


    
      junit
      junit
      4.11
    

    
      org.apache.hbase
      hbase-client
      2.3.5
    
    
      org.apache.hbase
      hbase-common
      2.3.5
    
  

2、编写测试类,与hbase创建连接

2.1 初始化配置

    private Connection connection = null;
    private Table table = null;

    @Before
    public void init() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","192.168.153.135");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        connection = ConnectionFactory.createConnection(conf);
    }

2.2 测试

@Test
    public void testConnection(){
        System.out.println(connection);
    }

效果图:

2.3 关闭连接

@After
    public void close() throws IOException {
        if (connection!=null)
            connection.close();
    }

3、创建表空间

@Test
    public void createNamespace() throws IOException {
        Admin admin = connection.getAdmin();
        NamespaceDescriptor.Builder builder = NamespaceDescriptor.create("bigdata888");
        NamespaceDescriptor namespaceDescriptor = builder.build();
        admin.createNamespace(namespaceDescriptor);
    }

4.创建表

@Test
    public void createTable() throws IOException {
        Admin admin = connection.getAdmin();
        TableName tableName = TableName.valueOf("bigdata888:test001");
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
        ColumnFamilyDescriptor columnFamilyDescriptor1 = ColumnFamilyDescriptorBuilder.of("baseinfo");
        ColumnFamilyDescriptor columnFamilyDescriptor2 = ColumnFamilyDescriptorBuilder.of("schoolinfo");
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor1);
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor2);
        TableDescriptor descriptor = tableDescriptorBuilder.build();
        admin.createTable(descriptor);
    }

5、插入数据

5.1 插入单行数据

@Test
    public void insertValue() throws IOException {
        table = connection.getTable(TableName.valueOf("bigdata888:test001"));
        Put rowkey1 = new Put(Bytes.toBytes("rowkey1"));
        rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
        rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
        rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("111"));
        rowkey1.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("njdx"));
        rowkey1.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("nj"));
        table.put(rowkey1);
    }

5.2 插入多行数据

@Test
    public void insertValue2() throws IOException {
        table = connection.getTable(TableName.valueOf("bigdata888:test001"));
        Put rowkey2 = new Put(Bytes.toBytes("rowkey2"));
        rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));
        rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("female"));
        rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("222"));
        rowkey2.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("bjdx"));
        rowkey2.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("bj"));

        Put rowkey3 = new Put(Bytes.toBytes("rowkey3"));
        rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("wangwu"));
        rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
        rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("333"));
        rowkey3.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("qhdx"));
        rowkey3.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("bj"));

        ArrayList list = new ArrayList();
        list.add(rowkey2);
        list.add(rowkey3);
        table.put(list);
    }

6、扫描表

@Test
    public void scanValue() throws IOException {
        table = connection.getTable(TableName.valueOf("bigdata888:test001"));
        Scan scan  = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result result :
                scanner) {
            byte[] stuname = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("name"));
            byte[] gender = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("gender"));
            byte[] pwd = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("pwd"));
            byte[] schoolname = result.getValue(Bytes.toBytes("schoolinfo"), Bytes.toBytes("name"));
            byte[] location = result.getValue(Bytes.toBytes("schoolinfo"), Bytes.toBytes("location"));
            System.out.println("-----------------------------------------");
            System.out.print(Bytes.toString(stuname)+"\t");
            System.out.print(Bytes.toString(gender)+"\t");
            System.out.print(Bytes.toString(pwd)+"\t");
            System.out.print(Bytes.toString(schoolname)+"\t");
            System.out.println(Bytes.toString(location)+"\t");
        }
    }

效果图:

Hbase API_第1张图片

7、查询单行数据

@Test
    public void getValue() throws IOException {
        table = connection.getTable(TableName.valueOf("bigdata888:test001"));
        Get zhangsan = new Get(Bytes.toBytes("rowkey2"));
        Result result = table.get(zhangsan);
        byte[] name = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("name"));
        System.out.println(Bytes.toString(name));
    }

效果图:

Hbase API_第2张图片

8、删除数据

    @Test
    public void del() throws IOException {
        table = connection.getTable(TableName.valueOf("bigdata888:test001"));
//        Delete wangwu = new Delete(Bytes.toBytes("rowkey3"));
//        lisi.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name")); // 删除具体列
//        table.delete(wangwu);
        Delete delwangwu = new Delete(Bytes.toBytes("rowkey3")); // 删除整行
        table.delete(delwangwu);
    }

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