HBase使用JAVA操作

一、创建Maven项目并添加依赖

	<dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>2.3.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>2.3.5</version>
    </dependency>

二、创建初始化方法与关闭方法

    private static Configuration config = null;
    private static Connection conn = null;

    private static Admin admin = null;
    static {
        System.out.println("执行初始化方法");
        config = HBaseConfiguration.create();
        config.set(HConstants.HBASE_DIR,"hdfs://192.168.136.144:9000/hbase");
        config.set(HConstants.ZOOKEEPER_QUORUM,"192.168.136.144");
        config.set(HConstants.CLIENT_PORT_STR,"2181");
        try {
            conn = ConnectionFactory.createConnection(config);
            admin = conn.getAdmin();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void close(){
        System.out.println("执行close()");
        try {
            if (admin!=null){
                admin.close();
            }
            if (conn!=null){
                conn.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

三、创建命名空间

    /**
     * 创建命名空间
     */

    public void createNameSpace() throws IOException {
        NamespaceDescriptor xsqone = NamespaceDescriptor.create("xsqone").build();
        admin.createNamespace(xsqone);
        this.close();
    }

四、查询所有命名空间

    /**
     * 查询所有命名空间
     * @throws IOException
     */
    public void getAllNameSpace() throws IOException {
        String[] nps = admin.listNamespaces();
        String s = Arrays.toString(nps);
        System.out.println(s);
        this.close();
    }

五、查询命名空间中的表

    /**
     * 查询命名空间中的表
     * @throws IOException
     */
    public void getNameSpaceTable() throws IOException {

        List<TableDescriptor> tableDescriptors = admin.listTableDescriptorsByNamespace("xsqone".getBytes());
        System.out.println(tableDescriptors);

        this.close();
    }

六、创建表

    /**
     * 创建表的描述
     */
    public void createTable() throws IOException {
//        创建表的描述
        TableName tableName = TableName.valueOf("xsqone: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);
        this.close();
    }

七、删除表

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

八、插入数据

    /**
     * 插入数据
     * @throws IOException
     */
    public void insertData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("xsqone:student"));
        Put put2 = new Put(Bytes.toBytes("student1"));
        put2.addColumn("info1".getBytes(),"name".getBytes(),"zs".getBytes());
        put2.addColumn("info2".getBytes(),"school".getBytes(),"td".getBytes());
        Put put3 = new Put(Bytes.toBytes("student2"));
        put3.addColumn("info1".getBytes(),"name".getBytes(),"ls".getBytes());
        put3.addColumn("info2".getBytes(),"school".getBytes(),"aa".getBytes());
        Put put4 = new Put(Bytes.toBytes("student3"));
        put4.addColumn("info1".getBytes(),"name".getBytes(),"ww".getBytes());
        put4.addColumn("info2".getBytes(),"school".getBytes(),"bb".getBytes());
        ArrayList<Put> puts = new ArrayList<Put>();
        puts.add(put2);
        puts.add(put3);
        puts.add(put4);
        table.put(puts);
        this.close();
    }

九、查询数据

    /**
     * 查询数据
     */
    public void quaryData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("xsqone: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));
        byte[] value1 = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
        System.out.println(Bytes.toString(value1));
        this.close();
    }

    /**
     * scan 查询表
     */
    public void sccanData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("xsqone: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));
            byte[] value1 = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
            System.out.println(Bytes.toString(value1));
        }
        this.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值