进入shell指令窗口
[root@HadoopNode00 hbase-1.2.4]# hbase shell
1. help 帮助命令
help '命令名字'
2. hbase中数据库的概念
namespace
2.1 显示所有的数据库
list_namespace
默认 default
hbase
2.2 显示当前数据库中所有的表
list_namespace_tables 'hbase'
2.3 创建一个数据库
create_namespace 'ns'
2.4 描述数据库
describe_namespace 'ns'
2.5 修改数据库
alter_namespace
2.6 删除数据库
drop_namespace 'ns'
3. 创建hbase中的表【重点】
3.1 基本的建表方式 【默认default库中】
create 't1','cf1'
3.2 创建多个列簇【默认default库中】
create 't1','cf1','cf2'
3.3 指定表所属的数据库
create 'ns:t1','cf1'
3.4 详细描述列簇的相关属性
create 'ns:t2',{NAME=>'cf1',VERSIONS=>2},{NAME=>'cf2'}
4. 描述表
describe 'ns:t1'
5. 修改表
alter 'ns:t2',{NAME=>'cf2',VERSIONS=>2}
6. 删除表
disable 'ns:t2'
drop 'ns:t2'
7. 失效 生效表相关命令
enable 'ns:t2'
disable 'ns:t2'
enable_all 'ns:t2'
disable_all 'ns:t2'
is_enable 'ns:t2'
is_disable 'ns:t2'
8. 判断表是否存在
exists 'ns:t2'
9. 表的查找命令
list 'ns:t.*'
10. 插入数据
put 't1',’rowkey‘,'family:qualify','value'
put 'ns:t1',’rowkey‘,'family:qualify','value'
11. 删除数据
delete 'ns:t1' ,'rowkey','family:qualify','timestamp'
12. 全表扫描
scan '表名'
scan 'ns1:tb1', {STARTROW => '20170521_10001',STOPROW => '20170521_10003'}
scan 'ns:user',{STARTROW=>'001',STOPROW=>'004'}
不包括stoprow的值
13. 某条数据的查询
get 'ns:user','001'
get 'ns:user','001','base:name'
Java API操作
# java访问HBase的核心API
Configruation HBase相关的配置
Htable HBase中的表
Put 插入数据
Get 查询数据
Scan 扫描数据
BytesUtil 字节处理
- 导入依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-protocol</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
- 开发应用
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* java api 操作hbase NOSQL数据库
*/
public class HBaseTest {
// 管理员对象 DDL
private Admin admin = null;
// 连接对象 DML
private Connection connection = null;
/**
* 初始化方法
*/
@Before
public void doBefore() throws IOException {
Configuration conf = HBaseConfiguration.create();
// zk服务主机名, HBase集群入口在ZK存的,client需要连接zk获取访问入口
conf.set(HConstants.ZOOKEEPER_QUORUM, "HadoopNode00");
conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "2181");
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
}
/**
* NameSpace操作
*/
@Test
public void test1() {
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("ns").build();
try {
// 创建
admin.createNamespace(namespaceDescriptor);
// admin.deleteNamespace("ns");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* DDL操作
*
* <p>
* 表(增)
*/
@Test
public void test2() {
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("ns:t_user"));
HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
cf1.setMaxVersions(3);
HColumnDescriptor cf2 = new HColumnDescriptor("cf2");
cf2.setInMemory(true);
cf2.setTimeToLive(3600 * 24 * 7);
hTableDescriptor.addFamily(cf1);
hTableDescriptor.addFamily(cf2);
try {
admin.createTable(hTableDescriptor);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* DDL操作
*
* <p>
* 表(删 改 查)
*/
@Test
public void test3() {
try {
// admin.deleteTable(TableName.valueOf("ns:t_user"));
// HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
// cf1.setMaxVersions(5);
// admin.modifyColumn(TableName.valueOf("ns:t_user"),cf1);
HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf("ns:t_user"));
HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
for (HColumnDescriptor columnFamily : columnFamilies) {
System.out.println(new String(columnFamily.getName()) + ", 多版本:" + columnFamily.getMaxVersions());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* DML操作【插值】
* <p>
* put 'ns:t_user','user001','cf1:name','zs'
*/
@Test
public void test4() throws IOException {
Table table = connection.getTable(TableName.valueOf("ns:t_user"));
// Put p1 = new Put("user001".getBytes());
// p1.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("zs1"));
// // 注意: Bytes是HBase提供的一个工具类,主要功能是对象的序列化和反序列化
// Put p2 = new Put(Bytes.toBytes("user002"));
// p2.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("ls1"));
// table.put(Arrays.asList(p1, p2));
Put p3 = new Put("person001".getBytes());
p3.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("wb"));
table.put(Arrays.asList(p3));
}
/**
* DML操作【取值】
* <p>
* get 'ns:t_user','user001'
* get 'baizhi2:tt_user', 'user001', {COLUMN => 'cf1:name',VERSIONS => 3}
*/
@Test
public void test5() throws IOException {
Table table = connection.getTable(TableName.valueOf("ns:t_user"));
Get get = new Get(Bytes.toBytes("user001"));
/*
Result result = table.get(get);
String name = Bytes.toString(result.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("name")));
System.out.println("name=" + name);
*/
get.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"));
get.setMaxVersions(3);
Result result = table.get(get);
List<Cell> cells = result.getColumnCells(Bytes.toBytes("cf1"), Bytes.toBytes("name"));
cells.forEach(cell -> {
String name = Bytes.toString(cell.getValue());
Long version = cell.getTimestamp();
System.out.println(name + "\t" + version);
});
}
/**
* DML操作【删除】
* <p>
* delete 'baizhi2:tt_user','user001','cf1:name',1576512334603
*/
@Test
public void test6() throws IOException {
Table table = connection.getTable(TableName.valueOf("ns:t_user"));
Delete delete = new Delete(Bytes.toBytes("user002"));
// 删除单元格内容 如果需要删除整行则无需添加列信息
delete.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), 1576517018101L);
table.delete(delete);
}
/**
* DML操作【扫描】
* <p>
* delete 'baizhi2:tt_user','user001','cf1:name',1576512334603
*/
@Test
public void test7() throws IOException {
Table table = connection.getTable(TableName.valueOf("ns:t_user"));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("user001"));
scan.setStopRow(Bytes.toBytes("user003"));
scan.setFilter(new PrefixFilter(Bytes.toBytes("user"))); // rowkey以user开头所有数据行
// result结果集合
ResultScanner rs = table.getScanner(scan);
for (Result result : rs) {
String rowkey = Bytes.toString(result.getRow());
String name = Bytes.toString(result.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("name")));
// Integer age = Bytes.toInt(result.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("age")));
// Boolean sex = Bytes.toBoolean(result.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("sex")));
System.out.println(rowkey + "\t" + name);
}
}
/**
* 释放资源
*/
@After
public void doAfter() {
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}