Hbase[03]Hbase Java API:常用命令、DDL和DML练习
准备环境
本次使用到的软件/工具如下
软件 | 软件版本 |
---|---|
VMware | VMware® Workstation 16 Pro |
Xshell | 6 |
IDEA | 2020.3 |
安装Hbase环境
详见链接:Hbase[01]安装Hbase
启动zookeeper、hdfs、hbase服务
xshell登录虚拟机
hbase shell
进入hbase
注意
hbase中:ctrl+backspace
是向左删除字符
backspace
是向右删除字符
Hbase_Demo
IDEA创建普通Maven项目
pom.xml
pom.xml中添加hadoop、hbase相关依赖
<properties>
<hadoop.version>2.6.4</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>0.98.9-hadoop2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.98.9-hadoop2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
TestHbase.java
在项目的src/main/java目录创建TestHbase.java
创建HBaseAdmin对象操作hbase
创建before方法加载hbase java api
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 org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
public class TestHbase {
HBaseAdmin admin;
Configuration configuration;
@Before
public void before() throws IOException{
configuration =new Configuration();
//zookeeper 主机节点
configuration.set("hbase.zookeeper.quorum","Toozky,Toozky2,Toozky3");
admin=new HBaseAdmin(configuration);
}
}
判断表是否存在
//判断t_user表是否存在
@Test
public void test1() throws IOException {
boolean b = admin.tableExists("ns1:t_emp");
System.out.println(b);
}
创建表
@Test
public void test2() throws IOException {
//创建表名
TableName tname=TableName.valueOf("ns1:t_emp");
//生成表
HTableDescriptor htd=new HTableDescriptor(tname);
//创建列族
HColumnDescriptor hcd=new HColumnDescriptor("emp_info");
//添加列族
htd.addFamily(hcd);
admin.createTable(htd);
admin.close();
}
创建namespace
//新建namespace
@Test
public void test3() throws IOException {
NamespaceDescriptor ns2 = NamespaceDescriptor.create("ns3").build();
//创建命名空间
admin.createNamespace(ns2);
admin.close();
}
查看所有namespace
等效hbase shell语句list_namespace
//查看所有namespace
@Test
public void test4() throws IOException {
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
System.out.println(namespaceDescriptor);
}
System.out.println("********");
for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
System.out.println(namespaceDescriptor.getName());
}
admin.close();
}//list_namespace
修改表
通过hbase shell语句desc 'ns1:t_emp'
验证
//修改表
@Test
public void test5() throws IOException {
//创建表名
TableName tname=TableName.valueOf("ns1:t_emp");
//生成表
HTableDescriptor htd=new HTableDescriptor(tname);
//创建列族
HColumnDescriptor hcd=new HColumnDescriptor("dept_info");
htd.addFamily(hcd);
HColumnDescriptor hcd1=new HColumnDescriptor("emp_info");
htd.addFamily(hcd1);
HColumnDescriptor hcd2=new HColumnDescriptor("test_info");
htd.addFamily(hcd2);
admin.modifyTable(tname,htd);
admin.close();
}//desc 'ns1:t_emp'
删除列族
通过hbase shell语句desc 'ns1:t_emp'
验证
//删除列族
@Test
public void test6() throws IOException {
admin.deleteColumn(TableName.valueOf("ns1:t_emp"), Bytes.toBytes("test_info"));
admin.close();
}//desc 'ns1:t_emp'
查询所有列族
//查询所有列族
@Test
public void test7() throws IOException {
HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf("ns1:t_emp"));
HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
for (HColumnDescriptor columnFamily : columnFamilies) {
System.out.println(columnFamily.getNameAsString());
}
admin.close();
}
Put对象录入数据
//Put对象 向hbase中录入数据
@Test
public void test8() throws IOException {
HTable hTable=new HTable(configuration,TableName.valueOf("ns1:t_emp"));
Put put1=new Put(Bytes.toBytes("001"));
put1.add(Bytes.toBytes("dept_info"),Bytes.toBytes("name"),Bytes.toBytes("sale"));
put1.add(Bytes.toBytes("emp_info"),Bytes.toBytes("name"),Bytes.toBytes("张三"));
Put put2=new Put(Bytes.toBytes("002"));
put2.add(Bytes.toBytes("emp_info"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));
Put put3=new Put(Bytes.toBytes("003"));
put3.add(Bytes.toBytes("emp_info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
List<Put> list=new ArrayList<Put>();
list.add(put1);
list.add(put2);
list.add(put3);
hTable.put(list);
}//scan 'ns1:t_emp'
获取列值
//获取列值
//先向测试表ns1:t_emp中录入row-key为001的数据
//put 'ns1:t_emp', '001', 'emp_info:name', '张三'
@Test
public void test9() throws IOException {
HTable ht=new HTable(configuration,TableName.valueOf("ns1:t_emp"));
Get get=new Get(Bytes.toBytes("001"));
Result result = ht.get(get);
NavigableMap<byte[], byte[]> emp_info = result.getFamilyMap(Bytes.toBytes("emp_info"));
for (Map.Entry<byte[], byte[]> entry : emp_info.entrySet()) {
String k = new String(entry.getKey());
String v = new String(entry.getValue());
System.out.println(k + " : " + v);
}
}
get查询 以row-key查单行数据
//先向测试表ns1:t_emp中录入row-key为003的数据
//put 'ns1:t_emp', '003', 'emp_info:name', 'zhangsan'
//get查询
@Test
public void test10() throws IOException {
//1. 获取Table对象
HTable table = new HTable(configuration, TableName.valueOf("ns1:t_emp"));
//2. Get
Get get = new Get(Bytes.toBytes("003"));
//3. 获取到结果对象
Result result = table.get(get);
//4. 表格扫描器
CellScanner cellScanner = result.cellScanner();
//5. 遍历扫描器
while (cellScanner.advance()) {
//6. 获取到一个表格
Cell cell = cellScanner.current();
//6.1 列簇
System.out.println(new String(CellUtil.cloneFamily(cell), "utf-8"));
//6.2 列名
System.out.println(new String(CellUtil.cloneQualifier(cell), "utf-8"));
//6.3 列值
System.out.println(new String(CellUtil.cloneValue(cell), "utf-8"));
}
}
get查询 遍历输出多个row-key的数据
//get查询 遍历输出
@Test
public void test11() throws IOException {
//1. 获取Table对象
HTable table = new HTable(configuration, TableName.valueOf("ns1:t_emp"));
//2. Get
Get get1 = new Get(Bytes.toBytes("001"));
Get get2 = new Get(Bytes.toBytes("002"));
Get get3 = new Get(Bytes.toBytes("003"));
//3. 获取结果数组
List<Get> list = new ArrayList<>();
list.add(get1);
list.add(get2);
list.add(get3);
Result[] results = table.get(list);
//4. 遍历
System.out.println("ROWKEY\t列族\t\t\t列名\t\t列值");
for (Result result : results) {
//4. 表格扫描器
CellScanner cellScanner = result.cellScanner();
//5. 遍历扫描器
while (cellScanner.advance()) {
//6. 获取到一个表格
Cell cell = cellScanner.current();
System.out.print(new String(CellUtil.cloneRow(cell), "utf-8")+"\t\t");
//6.1 列簇
System.out.print(new String(CellUtil.cloneFamily(cell), "utf-8")+"\t");
//6.2 列名
System.out.print(new String(CellUtil.cloneQualifier(cell), "utf-8")+"\t");
//6.3 列值
System.out.print(new String(CellUtil.cloneValue(cell), "utf-8")+"\n");
}
}
}
按row-key删除一条数据
//按row-key删除一条数据
@Test
public void test12() throws IOException {
String tablename="ns1:t_emp";
String row_key="003";
HTable htable = new HTable(configuration, tablename);
List delete=new ArrayList();
Delete d=new Delete(row_key.getBytes());
delete.add(d);
htable.delete(delete);
test11();
}
以上就是本期总结的全部内容,愿大家相互学习,共同进步!