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