hbase DDL(创建删除命名空间、表) DML(增改 查 以及 删除 DeleteFamily DeleteColumn Delete) API


1. DDL

1.1 hbase创建删除命名空间、表api

public class Hbase_DDL {

    private static Connection connection;
    private static Admin admin;

    static {
        //创建连接信息
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
        try {
            //创建连接器
            connection = ConnectionFactory.createConnection(configuration);
            //创建DDL操作对象
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //创建命名空间
    public static void createNS(String ns) throws IOException {
        //创建命名空间
        try {
            admin.createNamespace(NamespaceDescriptor.create(ns).build());
        } catch (NamespaceExistException e) {
            System.out.println("命名空间已存在");
        }
    }

    //创建表
    public static void createTable(String tableName, String... cfs) throws IOException {
        //判断是否有列族信息
        if (cfs.length <= 0) {
            System.out.println("未输入列族信息");
            return;
        }
        //判断表是否存在
        if (admin.tableExists(TableName.valueOf(tableName))) {
            System.out.println(tableName + " 表存在");
            return;
        }
        //创建表描述器
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
        for (String cf : cfs) {
            //创建列族描述器
            ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build();
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
        }
        TableDescriptor descriptor = tableDescriptorBuilder.build();

        //创建表
        admin.createTable(descriptor);
    }


    //删除表
    public static void dropTable(String tableName) throws IOException {
        TableName name = TableName.valueOf(tableName);
        if(!admin.tableExists(name)){
            System.out.println(tableName + " 表不存在");
            return;
        }
        //使表不可用
        admin.disableTable(TableName.valueOf(tableName));
        //删除表
        admin.deleteTable(TableName.valueOf(tableName));
    }

    public static void close() throws IOException {
        //释放连接
        admin.close();
        connection.close();
    }
}

1.2 创建命名空间、创建表

    public static void main(String[] args) throws IOException {
        createNS("sjj");
        createTable("sjj:stu", "info1", "info2");
        close();
    }

在这里插入图片描述

1.3 删除表

    public static void main(String[] args) throws IOException {
        dropTable("sjj:stu");
        close();
    }

在这里插入图片描述


2. DML

2.1 hbase增删查改api

public class Hbase_DML {
    private static Connection connection;

    static {
        //创建连接信息
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
        try {
            //创建连接器
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    //新增修改数据
    public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
        //获取DML的Table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建Put对象
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes("age"), 1655607479312L, Bytes.toBytes("20"));
        //执行插入数据操作
        table.put(put);
        //释放资源
        table.close();
    }

    //get数据查询
    public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
        //获取DML的Table对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建Get对象
        Get get = new Get(Bytes.toBytes(rowKey));

        if (StringUtils.isNotEmpty(cn) ) {
            //指定列族,列
            get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
        } else {
            //指定列族
            get.addFamily(Bytes.toBytes(cf));
        }
        //查询数据
        Result result = table.get(get);
        //获取结果
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println(" CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) + " CN: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " value: " + Bytes.toString(CellUtil.cloneValue(cell)));

        }
        //释放资源
        table.close();
    }

    //扫描数据
    public static void scanData(String tableName) throws IOException {
        //获取DML的Table对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建scan对象
        Scan scan = new Scan();
        //指定 startRow stopRow
        //   scan.withStartRow(Bytes.toBytes("1002"));
        //   scan.withStopRow(Bytes.toBytes("1003"));
        //扫描全表
        ResultScanner resultScanner = table.getScanner(scan);
        //迭代
        Iterator<Result> resultIterator = resultScanner.iterator();
        while (resultIterator.hasNext()) {
            Result result = resultIterator.next();
            //获取结果
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println(" CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) + " CN: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " value: " + Bytes.toString(CellUtil.cloneValue(cell)));

            }
        }
        //释放资源
        table.close();
    }

    //删除数据
    public static void deleteData(String tableName, String rowKey, String cf, String cn) throws IOException {
        //获取DML的Table对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建delete对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));

        if (StringUtils.isNotEmpty(cn)) { //指定列族 列删除
            delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
        } else if (StringUtils.isNotEmpty(cf)) { //指定列族删除
            delete.addFamily(Bytes.toBytes(cf));
        }

        //删除数据
        table.delete(delete);

        //释放资源
        table.close();
    }

    public static void close() throws IOException {
        connection.close();
    }
}

2.2 插入数据

    public static void main(String[] args) throws IOException {
        putData("sjj:stu", "1001", "info1", "name", "cz");
        close();
    }

在这里插入图片描述


2.3 查询数据

   public static void main(String[] args) throws IOException {
        getData("sjj:stu", "1002", "info1", null);
        close();
    }

在这里插入图片描述


2.4 扫描数据

    public static void main(String[] args) throws IOException {
        scanData("sjj:stu");
        close();
    }

在这里插入图片描述


2.5 删除数据

    public static void main(String[] args) throws IOException {
        deleteData("sjj:stu","1001",null,null);
        close();
    }

在这里插入图片描述

在这里插入图片描述

注:
在这里插入图片描述

DeleteFamily:执行删除整个RowKey数据所添加的标记,作用范围:当前列族小于等于标记时间戳的数据
在这里插入图片描述

DeleteColumn:执行删除指定到列族的列数据所添加的标记,作用范围:当前小于等于标记时间戳的数据
在这里插入图片描述

Delete:执行删除指定到列族的列数据锁添加的标记,作用范围:只作用于标记中所携带的时间戳的范围
在这里插入图片描述


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
您好!关于使用HBASE API创建tb_user以及增删改的问题,我可以为您提供一些参考信息。 首先,使用HBASE API创建需要以下步骤: 1. 创建Configuration对象,设置Zookeeper地址和HBase配置文件路径。 2. 创建HBaseAdmin对象,用于管理HBase集群。 3. 创建HTableDescriptor对象,设置名和列族信息。 4. 调用HBaseAdmin的createTable方法创建。 具体代码示例如下: ``` Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "zk01,zk02,zk03"); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.addResource("hbase-site.xml"); HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("tb_user")); tableDesc.addFamily(new HColumnDescriptor("info")); admin.createTable(tableDesc); ``` 其,"zk01,zk02,zk03"需要替换为实际的Zookeeper地址,"info"为列族信息。 接下来,使用HBase API进行增删改操作需要分别使用Put、Get、Delete和Scan类,具体代码示例如下: 1. 插入一条数据: ``` Table table = connection.getTable(TableName.valueOf("tb_user")); Put put = new Put(Bytes.toBytes("rowkey")); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("张三")); table.put(put); ``` 2. 询一条数据: ``` Table table = connection.getTable(TableName.valueOf("tb_user")); Get get = new Get(Bytes.toBytes("rowkey")); Result result = table.get(get); byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")); ``` 3. 删除一条数据: ``` Table table = connection.getTable(TableName.valueOf("tb_user")); Delete delete = new Delete(Bytes.toBytes("rowkey")); table.delete(delete); ``` 4. 扫描所有数据: ``` Table table = connection.getTable(TableName.valueOf("tb_user")); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")); } ``` 以上是关于使用HBASE API创建tb_user、以及增删改的一些参考信息。希望能对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

但行益事莫问前程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值