hbase java api最新版本,Hbase 最新版 Java Client DML API

本文介绍了如何使用HBase进行数据操作,包括创建配置、连接HBase集群,以及对'user_info1'表进行插入(Put)、删除(Delete)、更新(Update)和查询(Get)的操作。此外,还展示了使用Scan进行范围查询的方法。
摘要由CSDN通过智能技术生成

package HbaseDemo;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.CellScanner;

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;

import org.junit.Before;

import org.junit.Test;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Iterator;

import java.util.List;

/**

* @author HaoxuanLi Github:bestksl

* @version created date:2019-10-16 15:24

*/

public class HbaseDemoDML {

private Connection conn = null;

@Before

public void getConnection() throws IOException {

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "hadoop-01:2181,hadoop-02:2181,hadoop-03:2181");

conn = ConnectionFactory.createConnection(conf);

}

/*

*/

@Test

public void testPut() throws IOException {

// 获取table对象

Table table = conn.getTable(TableName.valueOf("user_info1"));

//创建put对象 同时指定 row key 一个put对象只能对应一个row key

Put put1 = new Put(Bytes.toBytes("002"));

Put put2 = new Put(Bytes.toBytes("003"));

Put put3 = new Put(Bytes.toBytes("004"));

// 分别为 列族名, 属性key, 属性值

put1.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("phll"));

put1.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("qz"));

put1.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("24"));

put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("wlxx"));

put2.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("ltfam"));

put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("142"));

put3.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("lt"));

put3.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("lf"));

put3.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("142"));

// 想表中插入put对象 注意可以单独插入一个put, 也可以插入一个List[put1,put2]

table.put(put1);

List puts = new ArrayList();

puts.add(put2);

puts.add(put3);

table.put(puts);

// 关闭连接

table.close();

conn.close();

}

/*

*/

@Test

public void testDelete() throws IOException {

// 获取 table 对象

Table table = conn.getTable(TableName.valueOf("user_info1"));

// 生成delete对象

Delete delete1 = new Delete(Bytes.toBytes("002")); // 如果不指定细节, 例如列族, 将会删掉row key对应的所有数据

delete1.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"));

Delete delete2 = new Delete(Bytes.toBytes("003"));

// 同样也可以单传 或者 list传

List deletes = new ArrayList();

deletes.add(delete1);

deletes.add(delete2);

// 提交

table.delete(deletes);

// 关闭连接

table.close();

conn.close();

}

/*

*/

@Test

public void testUpdate() {

}

/*

*/

@Test

public void testGet() throws IOException {

// 创建需要获取数据的表对象

Table table = conn.getTable(TableName.valueOf("user_info2"));

// 生成get对象 一个get 对象只能对应一个 row key

Get get = new Get("003".getBytes());

// 提交 并返回一个 result 对象

Result result = table.get(get);

// 获取指定value

String addr = new String(result.getValue("extra_info".getBytes(), "addr".getBytes()));

System.out.println(addr);

// 从 result 对象获取 CellScanner 迭代器

CellScanner cs = result.cellScanner();

// 遍历cell对象

while (cs.advance()) {

Cell cell = cs.current();

System.out.println("-----------------------------------");

System.out.println(new String(cell.getRowArray())); // row key

System.out.println(new String(cell.getFamilyArray())); // 列族名

System.out.println(new String(cell.getQualifierArray())); // 属性 key

System.out.println(new String(cell.getValueArray())); // 属性 value

}

// 关闭连接

table.close();

conn.close();

}

@Test

public void testScan() throws IOException {

// 生成表对象

Table table = conn.getTable(TableName.valueOf("user_info2"));

// 生成范围查找需要的 范围对象 Scan

Scan scan = new Scan("001".getBytes(), "005".getBytes());

// 提交 并接收一个 ResultScanner 对象 (也就是 多个 row 的 result 集合)

ResultScanner rs = table.getScanner(scan);

// 遍历 ResultScanner 中的 result对象

for (Result result : rs) {

// 通过单个result对象 获得 CellScanner 迭代器

CellScanner cs = result.cellScanner();

while (cs.advance()) {

Cell cell = cs.current();

System.out.println("----------------------------------");

//这里估计还是老API, 如果不指定 属性偏移量和长度 会乱码

System.out.println(new String(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())); // row key

System.out.println(new String(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength())); // 列族名

System.out.println(new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())); // 属性 key

System.out.println(new String(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); // 属性 value

}

}

// 关闭资源

table.close();

conn.close();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值