Hbase之java操作

一.pom配置

<dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>1.1.2</version>
       <!-- <scope>provided</scope> -->
</dependency>

二.代码

package com.antg.main;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class HbaseOp {
    // 用于hbase连接
    public Connection connection;
    // hbase配置
    public static Configuration configuration = HBaseConfiguration.create();

    //初始化Hbase操作对象
    public HbaseOp() throws IOException {
        // ad = new HBaseAdmin(configuration); //过期了,推荐使用Admin
        configuration.set("hbase.zookeeper.quorum", "192.168.1.34,192.168.1.31,192.168.1.32,192.168.1.41");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
        // 对connection初始化
        connection = ConnectionFactory.createConnection(configuration);
    }

    //创建表
    public void createTable(String tableName,String... cf1) throws IOException {
        //获取admin对象
        Admin admin = connection.getAdmin();
        //创建tableName对象
        TableName tName = TableName.valueOf(tableName);
        //创建描述信息
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tName);
        //判断表是否已经存在
        if(admin.tableExists(tName)){
            System.out.println("表"+tableName+"已经存在");
            return;
        }
        //列族
        for (String s : cf1) {
            hTableDescriptor.addFamily(new HColumnDescriptor(s));
        }
        //创建表
        admin.createTable(hTableDescriptor);
        System.out.println("表"+tableName+"创建成功");
    }


    //删除表
    public void delTable(String name) throws IOException {
        //获取Admin
        Admin admin = connection.getAdmin();
        //表名
        TableName tableName = TableName.valueOf(name);

        //判断是否存在
        if(admin.tableExists(tableName)){
            //先禁用
            admin.disableTable(tableName);
            //后删除
            admin.deleteTable(tableName);
            System.out.println(tableName+"删除成功");
        }else {
            System.out.println("表不存在");
        }
    }

    //插入数据
    public void put(String tableName) throws IOException {
        TableName tName = TableName.valueOf(tableName);
        //获取数据
        Table table = connection.getTable(tName);
        Random random = new Random();
        //创建一个记录容器
        List<Put> puts = new ArrayList<>();
        for(int i = 0;i<10;i++){
            //创建一条记录i
            Put put = new Put(Bytes.toBytes("r_"+i));
            //加入列(列族,列,值)
            put.addColumn(Bytes.toBytes("user"),Bytes.toBytes("username"),Bytes.toBytes("user_"+i));
            put.addColumn(Bytes.toBytes("user"),Bytes.toBytes("age"),Bytes.toBytes(i+""));
            put.addColumn(Bytes.toBytes("content"),Bytes.toBytes("phone"),Bytes.toBytes("8888"+i));
            put.addColumn(Bytes.toBytes("content"),Bytes.toBytes("age"),Bytes.toBytes("aaaa"+i));
            //添加到容器中
            puts.add(put);
        }
        table.put(puts);
        System.out.println("插入数据成功");
    }

    //查询数据
    public void search(String tableName) throws IOException {
        //TableName
        TableName tName = TableName.valueOf(tableName);
        //获取Table
        Table table = connection.getTable(tName);

        //获取Get对象
        List<Get> gets = new ArrayList<>();
        for(int i = 0;i<5;i++){
            Get get = new Get(Bytes.toBytes("r_"+i));
            gets.add(get);
        }
        //获取数据
        Result[] results = table.get(gets);
        
        //打印数据
        for (Result result : results) {
            //使用cell获取其中的数据
            CellScanner cellScanner = result.cellScanner();
            //逐个获取
            while (cellScanner.advance()){
                Cell cell = cellScanner.current();
                String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualify = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println("rowkey:" + rowKey + ",columnfamily:"
                        + family + ",qualify:" + qualify + ",value:" + value);
            }
            
        }
    }
    //关闭连接
    public void clean() throws IOException {
        connection.close();
    }
    //测试数据
    public static void main(String[] args) throws IOException {
        HbaseOp hbaseOp = new HbaseOp();
        //创建表
        hbaseOp.createTable("fjh001","user","content");
        //插入数据
        hbaseOp.put("fjh001");
        //查询数据
        hbaseOp.search("fjh001");
        //删除表
        hbaseOp.delTable("fjh001");
        //关闭连接
        hbaseOp.clean();
    }
}

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用Java API连接HBase并进行操作的示例代码: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class HBaseJavaAPIExample { private static Configuration conf = null; private static Connection conn = null; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); // ZooKeeper的地址 conf.set("hbase.zookeeper.property.clientPort", "2181"); // ZooKeeper的端口号 try { conn = ConnectionFactory.createConnection(conf); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try { Table table = conn.getTable(TableName.valueOf("test_table")); // 获取表对象 String rowKey = "row1"; String cf1 = "cf1"; String cf2 = "cf2"; String column1 = "column1"; String column2 = "column2"; String value1 = "value1"; String value2 = "value2"; // 添加数据 Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(cf1), Bytes.toBytes(column1), Bytes.toBytes(value1)); put.addColumn(Bytes.toBytes(cf2), Bytes.toBytes(column2), Bytes.toBytes(value2)); table.put(put); // 查询数据 Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); byte[] value1Bytes = result.getValue(Bytes.toBytes(cf1), Bytes.toBytes(column1)); byte[] value2Bytes = result.getValue(Bytes.toBytes(cf2), Bytes.toBytes(column2)); String value1Query = Bytes.toString(value1Bytes); String value2Query = Bytes.toString(value2Bytes); System.out.println("查询结果:" + value1Query + "," + value2Query); // 删除数据 Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(Bytes.toBytes(cf1), Bytes.toBytes(column1)); table.delete(delete); table.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例,我们创建了一个HBaseConfiguration对象,并通过它设置了ZooKeeper的地址和端口号。然后我们通过ConnectionFactory来创建一个Connection对象。接着我们使用该Connection对象来获取Table对象,然后执行添加、查询和删除数据的操作。最后我们关闭了Table和Connection对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mizui_i

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

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

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

打赏作者

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

抵扣说明:

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

余额充值