Java对HBse操作(创建、删除、添加)

1、准备工作

1.1 pom文件

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

1.2 Bean对象

class Student {
    private String id;
    private String name;
    private int age;

    public Student(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2. 创建连接对象

    // 连接hbase
    public static Connection conn;

    static {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.191.128");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        try {
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3. 判断表是否存在

    // 判断表是否存在
    public static boolean isTableExist(String tableName) throws IOException {

        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
//        HBaseAdmin admin = new HBaseAdmin(getConfiguration()); // 该方法已被废弃

        return admin.tableExists(tableName);

    }

4. 创建表

    // 创建表
    public static void createTable(String tableName, String... columnFamily) throws IOException {
        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
        // 判断表是否存在
        if (isTableExist(tableName)) {
            System.out.println("表" + tableName + "已存在!");
        } else {
            HTableDescriptor htableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            // 创建多个列族
            for (String cf : columnFamily) {
                htableDescriptor.addFamily(new HColumnDescriptor(cf));
            }
            // 根据对表的配置创建表
            admin.createTable(htableDescriptor);
            System.out.println("表" + tableName + "创建成功!");
        }
    }

5. 删除表

    // 删除表
    public static void dropTable(String tableName) throws IOException {
        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();

        if (isTableExist(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println("表" + tableName + "已删除!");
        } else {
            System.out.println("表" + tableName + "不存在!");
        }

    }

6. 添加单行数据

    // 添加单行数据
    public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
        // 创建HTable表对象
        Table table = conn.getTable(TableName.valueOf(tableName));

        Put put = new Put(Bytes.toBytes(rowKey));

        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));

        table.put(put);
        table.close();
        System.out.println("插入数据成功!");
    }

7. 添加多行数据

    // 添加多行数据
    public static void addbatchData(String tableName, String columnFamily, JSONArray jsonArray)
            throws IOException {
        // 创建表连接
        Table table = conn.getTable(TableName.valueOf(tableName));


        List<Put> putList = new ArrayList<Put>();

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = JSON.parseObject(jsonArray.get(i).toString());

            Put put = new Put(Bytes.toBytes(jsonObject.getString("id")));

            String name = jsonObject.getString("name");
            String age = jsonObject.getString("age");

            put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("name"), Bytes.toBytes(name));
            put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("age"), Bytes.toBytes(age));

            putList.add(put);
        }
        table.put(putList); 
        table.close();
    }

8. 删除多行数据

    // 删除多行数据
    public static void deleteMutilRow(String tableName, String...rows) throws IOException {
        Table table = conn.getTable(TableName.valueOf(tableName));

        List<Delete> deleteList = new ArrayList<Delete>();
        for (String row:rows){// row时rowkey
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
        }
        table.delete(deleteList);
        table.close();
    }

9. 获取多行数据

    // 获取多行数据
    public static void getAllRows(String tableName) throws IOException {
        Table table = conn.getTable(TableName.valueOf(tableName));
        // 得到用于扫面的region对象
        Scan scan = new Scan();
        //
        ResultScanner resultScanner = table.getScanner(scan);
        for(Result result:resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell:cells){
                // 得到rowkey
                System.out.println("行键:"+Bytes.toString(CellUtil.cloneRow(cell)));
                // 得到列族
                System.out.println("列族:"+Bytes.toString(CellUtil.cloneFamily(cell)));
                //得到列名
                System.out.println("列:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
                // 得到值
                System.out.println("值:"+Bytes.toString(CellUtil.cloneValue(cell))+"\n");
            }
        }
    }

10. 获取某一行数据

    // 获取某一行数据
    public static void getRow(String tableName, String rowkey) throws IOException {
        Table table = conn.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowkey));
//        get.setMaxVersions(); // 显示所有版本
        Result result = table.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println("行键:" + Bytes.toString(result.getRow()));
            System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\n");
        }
    }

11. 获取某一行“列族:列”的数据

    public static void getRowQualifier(String tableName, String rowkey
            , String family, String qualifier) throws IOException {

        Table table = conn.getTable(TableName.valueOf(tableName));

        Get get = new Get(Bytes.toBytes(rowkey));
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        Result result = table.get(get);

        for (Cell cell : result.rawCells()) {
            System.out.println("行键:" + Bytes.toString(result.getRow()));
            System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值