HBase JavaAPI

1. DDL

static Connection conn = null;
static Admin admin = null;

/**
 * 初始化连接对象
 * @throws IOException
 */
@Before
public void init() throws IOException {
    Configuration conf = HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
    conn = ConnectionFactory.createConnection(conf);
    admin = conn.getAdmin();
}

/**
 * 关闭资源
 *
 * @throws IOException
 */
@After
public void close() throws IOException {
    if (admin != null) {
        admin.close();
    }
    if (conn != null) {
        conn.close();
    }
}

1.1 创建命名空间

/**
 * 创建命名空间
 */
@Test
public void createNameSpace() {
    NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("myNM").build();
    try {
        // 该方法无返回值
        admin.createNamespace(namespaceDescriptor);
    } catch (NamespaceExistException e) {
        System.out.println("该命名空间已经存在,无法创建!");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1.2 删除命名空间

/**
 * 删除命名空间
 */
@Test
public void deleteNameSpace() {
    try {
        // 该方法无返回值
        admin.deleteNamespace("myNM");
    } catch (NamespaceNotFoundException e) {
        System.out.println("该命名空间不存在,无法删除!");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1.3 判断表是否存在

/**
 * 判断表是否存在
 * @throws IOException
 */
@Test
public void isExists() throws IOException {
    boolean exists = admin.tableExists(TableName.valueOf("tea"));
    if (exists) {
        System.out.println("该表存在!");
    } else {
        System.out.println("该表不存在!");
    }
}

1.4 创建表

/**
 * 创建表
 * @throws IOException
 */
@Test
public void createTable() throws IOException {
    // 判断该表是否存在
    boolean exists = admin.tableExists(TableName.valueOf("myNM:stu"));
    // 若该表不存在,则创建
    if (!exists) {
        System.out.println("正在创建表...");
        // 表名
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("myNM:stu"));
        // 列族
        hTableDescriptor.addFamily(new HColumnDescriptor("info"));
        // 创建表
        admin.createTable(hTableDescriptor);
    }
    // 再一次判断该表是否存在
    boolean flag = admin.tableExists(TableName.valueOf("myNM:stu"));
    // 若该表存在,则证明已创建
    if (flag) {
        System.out.println("该表已经创建");
    }
}

1.5 删除表

/**
 * 删除表
 * @throws IOException
 */
@Test
public void deleteTable() throws IOException {
    // 判断该表是否存在
    boolean exists = admin.tableExists(TableName.valueOf("tea"));
    // 判断该表是否存在
    if (exists) {
        System.out.println("正在删除表...");
        // 先禁用该表
        admin.disableTable(TableName.valueOf("tea"));
        // 再将该表删除
        admin.deleteTable(TableName.valueOf("tea"));
    }
    // 再一次判断该表是否存在
    boolean flag = admin.tableExists(TableName.valueOf("tea"));
    // 若该表不存在,则证明已删除
    if (!flag) {
        System.out.println("该表已经删除");
    }
}

2. DML

private Connection conn = null;
private Table tab = null;

/**
 * 初始化连接对象
 * @throws IOException
 */
@Before
public void init() throws IOException {
    Configuration conf = HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
    conn = ConnectionFactory.createConnection(conf);
}

/**
 * 关闭资源
 * @throws IOException
 */
@After
public void close() throws IOException {
    if (tab != null) {
        tab.close();
    }
    if (conn != null) {
        conn.close();
    }
}

2.1 插入数据

/**
 * 向表中插入数据
 * @throws IOException
 */
@Test
public void putData() throws IOException {

    // 获取表对象
    tab = conn.getTable(TableName.valueOf("myNM:stu"));
    // 获取 put 对象(参数:rowKey)
    Put put = new Put(Bytes.toBytes("1001"));

    // 向 put 对象添加数据(参数:columnFamily、columnName、value)
    put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("JOEL"));
    put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("male"));
    // 向表中添加数据
    tab.put(put);
}

2.2 查询数据

2.2.1 get

/**
 * 使用 get 获取数据
 * @throws IOException
 */
@Test
public void getData() throws IOException {

    // 获取表对象
    tab = conn.getTable(TableName.valueOf("stu"));

    // 获取 get 对象(参数:rowKey)
    Get get = new Get(Bytes.toBytes("1001"));
    // 1.指定列组
    // get.addFamily(Bytes.toBytes("info"));
    // 2.指定列组和列
    // get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"));
    // 3.指定版本数
    get.getMaxVersions();

    Result result = tab.get(get);

    // 打印数据
    for (Cell cell : result.rawCells()) {
        System.out.println("ColumnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t"
                           + "ColumnName: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t"
                           + "Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
    }
}

2.2.2 scan

/**
 * 使用 scan 获取数据
 */
@Test
public void scanData() throws IOException {

    // 获取表对象
    tab = conn.getTable(TableName.valueOf("stu"));

    // 获取 scan 对象
    Scan scan = null;

    // 1.无参,全查
    // scan = new Scan();
    // 2.指定 StartRow
    // scan = new Scan(Bytes.toBytes("1001"));
    // 3.指定 StartRow、StopRow
    scan = new Scan(Bytes.toBytes("1001"), Bytes.toBytes("1004"));

    // 获取数据
    ResultScanner results = tab.getScanner(scan);

    // 遍历结果集 方法1:
    Iterator<Result> iterator = results.iterator();
    while (iterator.hasNext()){
        for (Cell cell : iterator.next().rawCells()) {
            System.out.println("RowKey: " + Bytes.toString(CellUtil.cloneRow(cell)) + "\tm"
                               + "ColumnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t"
                               + "ColumnName: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t"
                               + "Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

    // 遍历结果集 方法2:
    for (Result result : results) {
        for (Cell cell : result.rawCells()) {
            System.out.println("RowKey: " + Bytes.toString(CellUtil.cloneRow(cell)) + "\tm"
                               + "ColumnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t"
                               + "ColumnName: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t"
                               + "Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

}

2.3 删除数据

方法无时间戳的删除有时间戳的删除
none删除整行,即所有列的所有版本从所有列族的所有列中删除与给定时间戳相同或者更旧的版本
addColumn()只删除给定列的最新版本,保留但旧版本只删除与时间戳匹配的给定列的指定版本,如果不存在则不删除
addColumns()删除给定列的所有版本从给定列中删除给定时间戳相等的和更旧的版本
addFamily()删除给定列族的所有列从给定列族的所有列中删除与给定时间戳相等或者更旧的版本
/**
 * 删除数据
 */
@Test
public void deleteData() throws IOException {

    // 获取表对象
    tab = conn.getTable(TableName.valueOf("stu"));

    // 获取 delete 对象
    // 1.根据rowKey删除
    Delete delete = new Delete(Bytes.toBytes("1001"));
    // 2.根据列族删除
    delete.addFamily(Bytes.toBytes("info"));
    // 3.根据列删除
    delete.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
    // 4.根据列删除
    delete.addColumns(Bytes.toBytes("info"), Bytes.toBytes("name"));

    // 删除数据
    tab.delete(delete);
}

 


❤️ END ❤️
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JOEL-T99

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

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

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

打赏作者

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

抵扣说明:

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

余额充值