1. idea 连接 hbase
1、idea
创建一个 maven
项目
2、添加相关依赖(hbase
客户端(最主要)、服务端),pom.xml
:
<dependencies>
<dependency>
<groupid>org.apache.hbase</groupid>
<artifactid>hbase-server</artifactid>
<version>1.2.6</version>
</dependency>
<dependency>
<groupid>org.apache.hbase</groupid>
<artifactid>hbase-client</artifactid>
<version>1.2.6</version>
</dependency>
</dependencies>
3、在 resources
中添加 core-site.xml、hbase-site.xml
4、修改本机的 hosts
文件(在c:\windows\system32\drivers\etc
下),添加集群的 ip
名称:
192.168.131.137 hadoop1
192.168.131.138 hadoop2
192.168.131.139 hadoop3
项目结构:
f:.
│ pom.xml
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─jun
│ │ │ └─test
│ │ │ testapi.java
│ │ │
│ │ └─resources
│ │ core-site.xml
│ │ hbase-site.xml
│ │
│ └─test
│ └─java
└─target
│ test_one.jar
│
├─classes
│ │ core-site.xml
│ │ hbase-site.xml
│ │
│ └─com
│ └─jun
│ └─test
│ testapi.class
2. ddl 表操作
ddl
与表操作有关,比如:
- 判断表是否存在
- 创建、删除、修改表
- 创建命名空间
2.1 配置连接
package com.jun.test;
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.admin;
import org.apache.hadoop.hbase.client.connection;
import org.apache.hadoop.hbase.client.connectionfactory;
import java.io.ioexception;
public class testapi {
public static connection connection = null;
public static admin admin = null;
static {
try {
// 获取配置信息
configuration configuration = hbaseconfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop1,hadoop2,hadoop3");
// 创建连接对象
connection = connectionfactory.createconnection(configuration);
// 创建 admin 对象
admin = connection.getadmin();
} catch (ioexception e) {
e.printstacktrace();
}
}
// 关闭连接
public static void close() {
if (admin != null) {
try {
admin.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
public static void main(string[] args) throws ioexception {
// 判断表是否存在
// system.out.println(istableexists("t1"));
// 创建表
// createtable("t2", "info");
// 将表创建到命名空间中
// createtable("0808:t2", "info");
// 删除表
// deletetable("t2");
// 创建命名空间
// createnamespace("0808");
// 关闭资源
close();
}
}
2.2 判断表是否存在
// 判断表是否存在
public static boolean istableexists(string tablename) throws ioexception {
boolean exists = admin.tableexists(tablename.valueof(tablename));
return exists;
}
2.3 创建表
创建表包括:添加列族信息、还可以将表添加到命名空间,创建之前可以先检查该表是否存在:
// 创建表
public static void createtable(string tablename, string... cfs) throws ioexception {
// 判断列族信息
if (cfs.length <= 0) {
system.out.println("请设置列族信息!!!");
return;
}
// 判断表是否存在
if (istableexists(tablename)) {
system.out.println(tablename + " 表已存在!!!");
return;
}
// 创建表描述器对象
htabledescriptor descriptor = new htabledescriptor(tablename.valueof(tablename));
// 循环添加列族
for (string cf : cfs) {
descriptor.addfamily(new hcolumndescriptor(cf));
}
// 创建表
admin.createtable(descriptor);
system.out.println("表 " + tablename + " 创建成功!!!");
}
2.4 删除表
// 删除表
public static void deletetable(string tablename) throws ioexception {
if (istableexists(tablename)) {
admin.disabletable(tablename.valueof(tablename)); // 先停用表,再删除
admin.deletetable(tablename.valueof(tablename));
system.out.println("表 " + tablename + " 删除成功!!!");
} else {
system.out.println("表 " + tablename + " 不存在!!!");
}
}
2.5 创建命名空间
// 创建命名空间
public static void createnamespace(string ns) {
// 创建命名空间描述器
namespacedescriptor namespacedescriptor = namespacedescriptor.create(ns).build();
// 创建命名空间
try {
admin.createnamespace(namespacedescriptor);
system.out.println(ns + "命名空间创建成功!!!");
} catch (namespaceexistexception e) {
system.out.println("命名空间 " + ns + " 已存在!!!");
}
catch (ioexception e) {
e.printstacktrace();
}
}
可以在 hbase shell
中使用 list_namespace
查看命名空间:
hbase(main):012:0> list_namespace
namespace 0808 default hbase
3 row(s) in 0.0420 seconds
// t2 表在 0808 命名空间里
hbase(main):014:0> list
table 0808:t2 t1
2 row(s) in 0.0430 seconds
=> ["0808:t2", "t1"]
3. dml 表记录操作
dml
主要是针对表的记录的操作,如插入、删除记录、查询记录等
3.1 插入数据
插入单条数据
// 插入数据
public static void putdata(string tablename, string rowkey, string cf, string cn, string value) throws ioexception {
// 获取表对象
table table = connection.gettable(tablename.valueof(tablename));
// 获取 put 对象
put put = new put(bytes.tobytes(rowkey));
// 给 put 对象赋值
put.addcolumn(bytes.tobytes(cf), bytes.tobytes(cn), bytes.tobytes(value));
// 插入数据
table.put(put);
system.out.println("数据插入成功!!!");
// 关闭表连接
table.close();
}
测试:
// 插入数据
putdata("t2", "10001", "info", "name", "rose");
插入多条数据
使用集合:
public static byte[] getbytes(string value) {
return bytes.tobytes(value);
}
// 插入多条数据
public static void putmanydata(string tablename, string cf, string cn, string value) throws ioexception {
table table = connection.gettable(tablename.valueof(tablename));
// 定义一个集合
list<put> puts = new arraylist<>();
int row_key = 10001;
for (int i = 1; i <= 5; i++) {
row_key = row_key + i;
put put = new put(getbytes(string.valueof(row_key)));
put.addcolumn(getbytes(cf), getbytes(cn + i), getbytes(value + i));
puts.add(put);
}
table.put(puts);
system.out.println("成功插入多行数据!");
table.close();
}
测试:
// 插入多条数据
putmanydata("t2", "info", "alias", "jun");
// scan t2
hbase(main):005:0> scan 't2'
row column+cell
10001 column=info:name, timestamp=1628369721705, value=rose 10002 column=info:alias1, timestamp=1628383262854, value=jun1 10004 column=info:alias2, timestamp=1628383262854, value=jun2 10007 column=info:alias3, timestamp=1628383262854, value=jun3 10011 column=info:alias4, timestamp=1628383262854, value=jun4 10016 column=info:alias5, timestamp=1628383262854, value=jun5
6 row(s) in 0.0860 seconds
3.2 get 数据
获取某行数据
// get 单条数据
public static void getdata(string tablename, string rowkey) throws ioexception {
table table = connection.gettable(tablename.valueof(tablename));
get get = new get((bytes.tobytes(rowkey)));
// 获取最大版本数
system.out.println(get.getmaxversions());
// system.out.println(get.settimestamp());
// 获取整行数据
result result = table.get(get);
for (cell cell : result.rawcells()) {
system.out.println("行键: " + bytes.tostring(result.getrow()));
system.out.println("列族: " + bytes.tostring(cellutil.clonefamily(cell)));
system.out.println("列: " + bytes.tostring(cellutil.clonequalifier(cell)));
system.out.println("值: " + bytes.tostring(cellutil.clonevalue(cell)));
system.out.println("时间戳: " + cell.gettimestamp());
}
table.close();
}
测试数据:
// 获取数据
getdata("t2", "10001");
1
行键: 10001
列族: info
列: name
值: rose
时间戳: 1628369721705
指定列族、列名
// 指定列族
get.addfamily(bytes.tobytes(cf));
// 指定列名
get.addcolumn(bytes.tobytes(cn));
3.3 scan 数据
// scan 所有数据
public static void scandata(string tablename) throws ioexception {
table table = connection.gettable(tablename.valueof(tablename));
scan scan = new scan();
resultscanner resultscanner = table.getscanner(scan);
for (result result : resultscanner) {
cell[] cells = result.rawcells();
for (cell cell : cells) {
system.out.println("行键: " + bytes.tostring(result.getrow()));
system.out.println("列族: " + bytes.tostring(cellutil.clonefamily(cell)));
system.out.println("列: " + bytes.tostring(cellutil.clonequalifier(cell)));
system.out.println("值: " + bytes.tostring(cellutil.clonevalue(cell)));
system.out.println("时间戳: " + cell.gettimestamp());
system.out.println("-------------------------------------------\n\n");
}
}
table.close();
}
测试数据:
// 扫描所有数据
scandata("t2");
行键: 10001
列族: info
列: name
值: rose
时间戳: 1628369721705
-------------------------------------------
行键: 10002
列族: info
列: alias1
值: jun1
时间戳: 1628383262854
-------------------------------------------
行键: 10004
列族: info
列: alias2
值: jun2
时间戳: 1628383262854
-------------------------------------------
行键: 10007
列族: info
列: alias3
值: jun3
时间戳: 1628383262854
-------------------------------------------
行键: 10011
列族: info
列: alias4
值: jun4
时间戳: 1628383262854
-------------------------------------------
行键: 10016
列族: info
列: alias5
值: jun5
时间戳: 1628383262854
-------------------------------------------
建议
使用 scan
扫描全表时,最好设置 startrow、endrow
,亦或者是带过滤条件的翻页,而非全表扫描:
// 设置 startrow
scan(bytes.tobytes("startrow"))
// 设置 startrow、endrow
scan(bytes.tobytes("startrow"), bytes.tobytes("endrow"))
// 实际工作中一般不知道 endrow,而是带过滤条件的翻页
scan(bytes.tobytes("startrow"), filter filter)
resultscanner resultscanner = table.getscanner(scan);
3.4 删除数据
删除单行数据
// 删除单行数据
public static void deletedata(string tablename, string rowkey) throws ioexception {
table table = connection.gettable(tablename.valueof(tablename));
// 获取 delete 对象
delete delete = new delete(bytes.tobytes(rowkey));
// 删除指定的列族、列名
// delete.addcolumn(bytes.tobytes(cf), bytes.tobytes(cn));
table.delete(delete);
system.out.println(rowkey + " 删除成功!!!");
table.close();
}
测试:
// 删除单行数据
deletedata("t2", "10001");
hbase(main):007:0> scan 't2'
row column+cell
10002 column=info:alias1, timestamp=1628383262854, value=jun1
10004 column=info:alias2, timestamp=1628383262854, value=jun2
10007 column=info:alias3, timestamp=1628383262854, value=jun3
10011 column=info:alias4, timestamp=1628383262854, value=jun4
10016 column=info:alias5, timestamp=1628383262854, value=jun5
5 row(s) in 0.1290 seconds
删除多行数据
// 删除多行数据
public static void deletemanydata(string tablename, string... rows) throws ioexception {
table table = connection.gettable(tablename.valueof(tablename));
list<delete> deletes = new arraylist<>();
for (string row : rows) {
delete delete = new delete(bytes.tobytes(row));
deletes.add(delete);
}
table.delete(deletes);
system.out.println("删除成功!!!");
table.close();
}
测试:
// 删除多行数据
deletemanydata("t2", "10002", "10007");
hbase(main):008:0> scan 't2'
row column+cell
10004 column=info:alias2, timestamp=1628383262854, value=jun2
10011 column=info:alias4, timestamp=1628383262854, value=jun4
10016 column=info:alias5, timestamp=1628383262854, value=jun5
3 row(s) in 0.1140 seconds
发表评论