java实现Hbase 增删改查

本文详细介绍了如何在Java中使用Maven构建项目,然后通过ApacheHBase的API进行数据库配置,创建命名空间和表,执行表的删除、数据插入、获取及扫描操作。主要步骤包括设置HBase连接信息,创建和管理表,以及数据的CRUD操作。

目录

一、新建一个maven工程

 二、代码实现

2.1、配置hbase信息,连接hbase数据库

2.2、创建命名空间

2.3、创建表

2.4、删除表,删除之前要设置为禁用状态

2.5、添加数据

2.6、获取命令表空间 / tables列表

2.7、get方法查看表的内容

2.8、scan方法查看表的内容


一、新建一个maven工程

 添加 pom依赖

  <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>

 二、代码实现

2.1、配置hbase信息,连接hbase数据库

1.、测试类添加@Before 和@After  便于初始化和关闭资源

2、将对象提到上面,变为全局对象

    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.91.180:9000/hbase");
        config.set(HConstants.ZOOKEEPER_QUORUM, "192.168.91.180");
        config.set(HConstants.CLIENT_PORT_STR, "2181");
        conn = ConnectionFactory.createConnection(config);
        admin = conn.getAdmin();
    }


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

    }

2.2、创建命名空间

    @Test
    public void createNameSpace() throws IOException {
        NamespaceDescriptor kb21 = NamespaceDescriptor.create("kb21").build();
        try {
            admin.createNamespace(kb21);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2.3、创建表

    @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);
    }

2.4、删除表,删除之前要设置为禁用状态

   @Test
    public void deleteTable() throws IOException {
        admin.disableTable(TableName.valueOf("kb21:student"));
        admin.deleteTable(TableName.valueOf("kb21:student"));

    }

2.5、添加数据

    @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());

        ArrayList<Put> list = new ArrayList<>();

        list.add(put2);
        list.add(put3);
        table.put(list);
    }

2.6、获取命令表空间 / tables列表

   @Test
    public void getAllNameSpace() throws IOException {
        String[] nps = admin.listNamespaces();
        String s = Arrays.toString(nps);
        System.out.println(s);
        //获得表的列族
        List<TableDescriptor> tableDesc = admin.listTableDescriptorsByNamespace("kb21".getBytes());
        System.out.println(tableDesc.toString());
    }

2.7、get方法查看表的内容

    @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));
    }

2.8、scan方法查看表的内容

    @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()));
        }
    }

好的,我可以帮您回答这个问题。下面是使用 Java 操作 HBase 数据库的增删改查示例代码: 1. 导入必要的包 ``` import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; ``` 2. 创建 HBase 配置对象 ``` Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "localhost"); // HBase 的 ZooKeeper 地址 configuration.set("hbase.zookeeper.property.clientPort", "2181"); // HBase 的 ZooKeeper 端口号 ``` 3. 创建 HBase 连接对象和表对象 ``` Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("test_table")); // test_table 是表名 ``` 4. 插入数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("value1")); // cf 是列族名,col1 是列名,value1 是值 table.put(put); ``` 5. 查询数据 ``` Get get = new Get(Bytes.toBytes("row1")); // row1 是行键名 Result result = table.get(get); for (Cell cell : result.listCells()) { String cf = Bytes.toString(CellUtil.cloneFamily(cell)); // 获取列族名 String col = Bytes.toString(CellUtil.cloneQualifier(cell)); // 获取列名 String value = Bytes.toString(CellUtil.cloneValue(cell)); // 获取值 System.out.println(cf + ":" + col + "=" + value); } ``` 6. 修改数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("new_value")); // cf 是列族名,col1 是列名,new_value 是新的值 table.put(put); ``` 7. 删除数据 ``` Delete delete = new Delete(Bytes.toBytes("row1")); // row1 是行键名 delete.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1")); // cf 是列族名,col1 是列名 table.delete(delete); ``` 以上就是使用 Java 操作 HBase 数据库的增删改查示例代码,希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值