1. 导入pom依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
2. 添加配置文件
在resource目录下创建hbase-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.zookeeper.quorum</name>
<value>node01,node02,node03</value>
<description>The directory shared by region servers.
</description>
</property>
</configuration>
3. 连接HBase
- 第一种方式: 读取配置文件,配置文件中配置Zookeeper
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
- 通过代码配置,无序设置配置文件,需要在代码中配置zk
Configuration configuration = new Configuration();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
connection = ConnectionFactory.createConnection();
4. 创建表
public static void main(String[] args) throws IOException {
//1. 连接HBase
//1.1 HBaseConfiguration.create(),获取配置文件对象
Configuration config = HBaseConfiguration.create();
//1.2 通过连接工厂,传入配置文件对象,创建一个连接
Connection connection = ConnectionFactory.createConnection(config);
//1.3 从连接中获得admin对象,
Admin admin = connection.getAdmin();
//2.创建表
//2.1 通过表名称类,传入表名称,获取表名称对象
TableName tableName = TableName.valueOf("user");
if (! admin.tableExists(tableName)) {
//2.2 如果表不存在就重新创建一个表
//创建表描述对象
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
//通过表描述对象,添加列簇
hTableDescriptor.addFamily(new HColumnDescriptor("base_info"));
//通过admin创建表,需要传入表描述对象
admin.createTable(hTableDescriptor);
System.out.println("创建表");
}
}
5. 打印表的信息
//1. 先连接HBase数据库
@Before
public void initConnection(){
try{
connection = ConnectionFactory.createConnection(config);
}catch(IOException e){
System.out.println("连接数据库失败");
}
}
@Test
public void tableInfo() throws IOException {
//1. 定义表名称对象
TableName tableName = TableName.valueOf("user");
//2. 获取表对象,通过连接获取表对象
Table table = connection.getTable(tableName);
//3. 获取表描述信息
HTableDescriptor tableDescriptor = table.getTableDescriptor();
//4. 获取表的列簇信息
HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
for(HColumnDescriptor columnFamily : columnFamilies) {
//5. 获取表的columnFamily的字节数组
byte[] name = columnFamily.getName();
//6. 使用HBase自带的bytes工具类转成String
String vlaue = Bytes.toString(name);
//7. 打印
System.out.println(value);
}
}
6. 添加数据(put)
public void initConnection() {
try{
connection = ConnectionFactory.createConnection(config);
} catch (IOException e) {
System.out.println("连接数据库");
}
}
@Test
public void put() throws IOException {
//1. 定义表的名称
TableName tableName = TableName.valueOf("user");
//2. 获取表对象
Table table = connection.getTable(tableName);
//3. 准备数据
String rowkey = "rowkey_10";
Put put= new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("张三"));
put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("sex"), Bytes.toBytes("1"));
put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("address"), Bytes.toBytes("北京市"));
put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("birthday"), Bytes.toBytes("2014-07-10"));
// 4. 添加数据
table.put(put);
table.close();
}
7. 获取数据(Get)
@Test
public void get() throws IOException {
// 1.定义表的名称
TableName tableName = TableName.valueOf("user");
// 2.获取表
Table table = connection.getTable(tableName);
// 3.准备数据
String rowKey = "rowkey_10";
// 4.拼装查询条件
Get get = new Get(Bytes.toBytes(rowKey));
// 5.查询数据
Result result = table.get(get);
// 6.打印数据 获取所有的单元格
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
// 打印rowkey,family,qualifier,value
System.out.println(Bytes.toString(CellUtil.cloneRow(cell))
+ "==> " + Bytes.toString(CellUtil.cloneFamily(cell))
+ "{" + Bytes.toString(CellUtil.cloneQualifier(cell))
+ ":" + Bytes.toString(CellUtil.cloneValue(cell)) + "}");
}
}
8. 全表扫描(scan慎用)
@Test
public void scan() throws IOException {
// 1.定义表的名称
TableName tableName = TableName.valueOf("user");
// 2.获取表
Table table = connection.getTable(tableName);
// 3.全表扫描
Scan scan = new Scan();
// 4.获取扫描结果
ResultScanner scanner = table.getScanner(scan);
Result result = null;
// 5. 迭代数据
while ((result = scanner.next()) != null) {
// 6.打印数据 获取所有的单元格
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
// 打印rowkey,family,qualifier,value
System.out.println(Bytes.toString(CellUtil.cloneRow(cell))
+ "==> " + Bytes.toString(CellUtil.cloneFamily(cell))
+ "{" + Bytes.toString(CellUtil.cloneQualifier(cell))
+ ":" + Bytes.toString(CellUtil.cloneValue(cell)) + "}");
}
}
}
9. 范围查询(开始行-结束行)
在上一步骤(全表扫描基础上)的基础上修改代码如下
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("rowkey_1"));
scan.setStopRow(Bytes.toBytes("rowkey_2"));
如果喜欢本文章,请用小手点个赞~