一、命令行操作HBase
# 进入hbase命令行
hbase shell
# 显示所有的hbase语句
help [指定命令]
# 查看hbase状态
status
# 创建表
create 'student', 'info', 'grade'
# 展示所有表
list
# 查看表结构
describe 'student'
desc 'student'
# 插入数据
put 'student','s01','info:name','Tom'
put 'student','s01','info:age','24'
# 查询数据,相当于select * from student
scan 'student'
# 查询数据:通过行键查询
get 'student', 's01'
# 清空表数据
truncate 'student'
二、使用JavaApi操作HBase
由于我使用的是docker不是的全分布式模式,测试的时候导致一直转圈,连接超时,需要修改本地hosts文件:
所以我的本地也需要修改如下
172.18.0.5 hbase1
172.18.0.6 hbase2
172.18.0.7 hbase3
导入依赖
<!-- 因为后面要写MapReduce所有可以导入这个 -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-mapreduce</artifactId>
<version>2.0.0</version>
</dependency>
package org.gykalc;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestHBase {
private static String quorum = "172.18.0.5";
private static Integer port = 2181;
/**
* 创建表
*
* @throws IOException
*/
@Test
public void testCreateTable() throws IOException {
// 配置Zookeeper地址
Configuration configuration = org.apache.hadoop.hbase.HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", quorum);// ip of hbase server(remote)
configuration.setInt("hbase.zookeeper.property.clientPort", port);// port: 2181 default
// 客户端
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
boolean exists = admin.tableExists(TableName.valueOf("mystudent02"));
System.out.println("表是否存在:" + exists);
if (!exists) {
// 创建表的描述符
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("mystudent02"));
// 表中的列族
htd.addFamily(new HColumnDescriptor("info"));
htd.addFamily(new HColumnDescriptor("grade"));
// 创建表
admin.createTable(htd);
}
admin.close();
}
/**
* 插入数据:单条数据或多条数据
*
* @throws Exception
*/
@Test
public void testInsertData() throws Exception {
// 配置Zookeeper地址
Configuration configuration = org.apache.hadoop.hbase.HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", quorum);// ip of hbase server(remote)
configuration.setInt("hbase.zookeeper.property.clientPort", port);// port: 2181 default
// 得到表的客户端
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
boolean mystudent = admin.tableExists(TableName.valueOf("mystudent"));
System.out.println("表是否存在:" + mystudent);
if (mystudent) {
Table table = connection.getTable(TableName.valueOf("mystudent"));
// 插入数据(参数:行键)
Put put1 = new Put(Bytes.toBytes("s002"));
put1.addColumn(
Bytes.toBytes("info"),
Bytes.toBytes("name"),
Bytes.toBytes("gyk"));
Put put2 = new Put(Bytes.toBytes("s002"));
put2.addColumn(
Bytes.toBytes("info"),
Bytes.toBytes("age"),
Bytes.toBytes("20"));
List<Put> puts = new ArrayList<>();
puts.add(put1);
puts.add(put2);
// 插入多条数据
table.put(puts);
table.close();
}
}
/**
* Get获取数据
*
* @throws Exception
*/
@Test
public void testGet() throws Exception {
// 配置Zookeeper地址
Configuration configuration = org.apache.hadoop.hbase.HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", quorum);// ip of hbase server(remote)
configuration.setInt("hbase.zookeeper.property.clientPort", port);// port: 2181 default
// 得到表的客户端
Connection connection = ConnectionFactory.createConnection(configuration);
// 得到表客户端
Table hTable = connection.getTable(TableName.valueOf("mystudent"));
// 构造一个Get对象,指定RowKey
Get s001 = new Get(Bytes.toBytes("s001"));
// 查询
Result result = hTable.get(s001);
// 获取name
String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
System.out.println(name);
hTable.close();
}
/**
* Scan获取数据
*
* @throws Exception
*/
@Test
public void testScan() throws Exception {
// 配置Zookeeper地址
Configuration configuration = org.apache.hadoop.hbase.HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", quorum);// ip of hbase server(remote)
configuration.setInt("hbase.zookeeper.property.clientPort", port);// port: 2181 default
// 得到表的客户端
Connection connection = ConnectionFactory.createConnection(configuration);
// 得到表客户端
Table hTable = connection.getTable(TableName.valueOf("mystudent"));
// 构造一个Scan
Scan scan = new Scan();
// 查询
ResultScanner rs = hTable.getScanner(scan);
for (Result r : rs) {
String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
System.out.println(name);
}
hTable.close();
}
/**
* 删除表
*
* @throws Exception
*/
@Test
public void testDropTable() throws Exception {
// 配置Zookeeper地址
Configuration configuration = org.apache.hadoop.hbase.HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", quorum);// ip of hbase server(remote)
configuration.setInt("hbase.zookeeper.property.clientPort", port);// port: 2181 default
// 得到表的客户端
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
// 删除表
admin.disableTable(TableName.valueOf("mystudent01"));
admin.deleteTable(TableName.valueOf("mystudent01"));
admin.close();
}
}