每日总结12
HBase学习5(HBase java编程:创建项目,创建删除表,数据增删改查)
1.准备工作
1.1 创建IDEA Maven项目
其中名字为hbase_op,groupid为cn.itcast
然后导入pom依赖
<repositories><!-- 代码库 -->
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- HBase的java客户端 -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build>
1.2复制HBase和Hadoop配置文件
将虚拟机中的hbase-site.xml、core-site.xml、log4j.properties复制到resource目录中
从Linux中下载:sz /export/server/hbase-2.1.0/conf/hbase-site.xml
从Linux中下载:sz /export/server/hadoop-2.7.5/etc/hadoop/core-site.xml

1.3 创建包结构和类
test目录创建 cn.itcast.hbase.admin.api_test 包结构再创建TableAmdinTest类
1.4 创建Hbase连接以及admin管理对象
要操作Hbase也需要建立Hbase的连接。此处我们仍然使用TestNG来编写测试。使用@BeforeTest初始化HBase连接,创建admin对象、@AfterTest关闭连接。
步骤:
- 使用HbaseConfiguration.create()创建Hbase配置
- 使用ConnectionFactory.createConnection()创建Hbase连接
- 要创建表,需要基于Hbase连接获取admin管理对象
- 使用admin.close、connection.close关闭连接
public class TableAdminTest {
private Connection connection;
private Admin admin;
@BeforeTest
public void beforeTest() throws IOException {
// 1. 使用HbaseConfiguration.create()创建Hbase配置
Configuration configuration = HBaseConfiguration.create();
// 2. 使用ConnectionFactory.createConnection()创建Hbase连接
connection = ConnectionFactory.createConnection(configuration);
// 3. 要创建表,需要基于Hbase连接获取admin管理对象
// 要创建表、删除表需要和HMaster连接,所以需要有一个admin对象
admin = connection.getAdmin();
}
@AfterTest
public void afterTest() throws IOException {
// 4. 使用admin.close、connection.close关闭连接
admin.close();
connection.close();
}
}
2.创建删除表
实现步骤:
- 判断表是否存在,存在则退出,不存在则进行下一步
- 使用TableDescriptorBuilder.newBuilder构建表描述构建器
- 使用ColumnFamilyDescriptorBuilder.newBuilder构建列蔟描述构建器
- 构建列蔟描述,构建表描述
- 创建表
例:创建一个表名WATER_BILL,列簇为C1
@Test
public void createTableTest() throws IOException {
//创建表名为WATER_BILL,列簇为C1
TableName tableName = TableName.valueOf("WATER_BILL");
// 1. 判断表是否存在
if(admin.tableExists(tableName)) {
// a) 存在,则退出
return;
}
// 构建表
// 2. 使用TableDescriptorBuilder.newBuilder构建表描述构建器
// TableDescriptor: 表描述器,描述这个表有几个列蔟、其他的属性都是在这里可以配置
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
// 3. 使用ColumnFamilyDescriptorBuilder.newBuilder构建列蔟描述构建器
// 创建列蔟也需要有列蔟的描述器,需要用一个构建起来构建ColumnFamilyDescriptor
// 经常会使用到一个工具类:Bytes(hbase包下的Bytes工具类)
// 这个工具类可以将字符串、long、double类型转换成byte[]数组
// 也可以将byte[]数组转换为指定类型
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("C1"));
// 4. 构建列蔟描述,构建表描述
ColumnFamilyDescriptor cfDes = columnFamilyDescriptorBuilder.build();
// 建立表和列蔟的关联
tableDescriptorBuilder.setColumnFamily(cfDes);
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
// 5. 创建表
admin.createTable(tableDescriptor);
}
3.删除表
实现步骤:
- 判断表是否存在
- 如果存在,则禁用表
- 再删除表
@Test
public void deleteTableTest() throws IOException {
TableName tableName = TableName.valueOf("WATER_BILL");
// 1. 判断表是否存在
if(admin.tableExists(tableName)) {
// 2.如果存在,则禁用表
admin.disableTable(tableName);
// 3.再删除表
admin.deleteTable(tableName);
}
}
4.对数据进行增删改查
4.1 先创建包(该包专门对数据进行操作)
- 在 test 目录中创建 cn.itcast.hbase.data.api_test 包
- 创建DataOpTest类
4.2 初始化Hbase连接
在@BeforeTest中初始化HBase连接,在@AfterTest中关闭Hbase连接。
public class DataOpTest {
// Connection是一个重量级的对象,不能频繁去创建Connection
// Connection是线程安全的
private Connection connection;
private TableName TABLE_NAME = TableName.valueOf("WATER_BILL");
@BeforeTest
public void beforeTest() throws IOException {
// 1. 使用HbaseConfiguration.create()创建Hbase配置
Configuration configuration = HBaseConfiguration.create();
// 2. 使用ConnectionFactory.createConnection()创建Hbase连接
connection = ConnectionFactory.createConnection(configuration);
}
@AfterTest
public void afterTest() throws IOException {
connection.close();
}
}
4.3添加/插入数据
实现步骤:
- 使用Hbase连接获取Htable
- 构建ROWKEY、列蔟名、列名
- 构建Put对象(对应put命令)
- 添加姓名列
- 使用Htable表对象执行put操作
- 关闭Htable表对象
例子:
@Test
public void putTest() throws IOException {
// 1. 使用Hbase连接获取Htable
Table table = connection.getTable(TABLE_NAME);
// 2. 构建ROWKEY、列蔟名、列名
String rowkey = "4944191";
String columnFamily = "C1";//列簇
String columnName = "NAME";//列名
String columnNameADDRESS = "ADDRESS";//列名
String columnNameSEX = "SEX";//列名
String columnNamePAY_DATE = "PAY_DATE";
String columnNameNUM_CURRENT = "NUM_CURRENT";
String columnNameNUM_PREVIOUS = "NUM_PREVIOUS";
String columnNameNUM_USAGE = "NUM_USAGE";
String columnNameTOTAL_MONEY = "TOTAL_MONEY";
String columnNameRECORD_DATE = "RECORD_DATE";
String columnNameLATEST_DATE = "LATEST_DATE";
// value:
// 3. 构建Put对象(对应put命令)
Put put = new Put(Bytes.toBytes(rowkey));
// 4. 添加姓名列
// 使用alt + 鼠标左键列编辑,按住ctrl + shift + 左箭头/右箭头选择单词
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), Bytes.toBytes("登卫红"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameADDRESS),Bytes.toBytes("贵州省铜仁市德江县7单元267室"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameSEX),Bytes.toBytes("男"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNamePAY_DATE),Bytes.toBytes("2020-05-10"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameNUM_CURRENT),Bytes.toBytes("308.1"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameNUM_PREVIOUS),Bytes.toBytes("283.1"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameNUM_USAGE),Bytes.toBytes("25"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameTOTAL_MONEY),Bytes.toBytes("150"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameRECORD_DATE),Bytes.toBytes("2020-04-25"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameLATEST_DATE),Bytes.toBytes("2020-06-09"));
// 5. 使用Htable表对象执行put操作
table.put(put);
// 6. 关闭Htable表对象
// HTable是一个轻量级的对象,可以经常创建
// HTable它是一个非线程安全的API
table.close();
}
4.4 获取/查询数据
get 'WATER_BILL','4944191',{FORMATTER => 'toString'}
实现步骤:
- 获取HTable
- 使用rowkey构建Get对象
- 执行get请求
- 获取所有单元格
- 打印rowkey
- 迭代单元格列表
- 关闭表
例子:
@Test
public void getTest() throws IOException {
// 1. 获取HTable
Table table = connection.getTable(TABLE_NAME);
// 2. 使用rowkey构建Get对象
Get get = new Get(Bytes.toBytes("4944191"));
// 3. 执行get请求
Result result = table.get(get);
// 4. 获取所有单元格
// 列出所有的单元格
List<Cell> cellList = result.listCells();
// 5. 打印rowkey
byte[] rowkey = result.getRow();
System.out.println(Bytes.toString(rowkey));
// 6. 迭代单元格列表
for (Cell cell : cellList) {
// 将字节数组转换为字符串
// 获取列蔟的名称
String cf = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
// 获取列的名称
String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
// 获取值
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(cf + ":" + columnName + " -> " + value);
}
// 7. 关闭表
table.close();
}
4.5 删除数据
实现步骤:
- 获取HTable对象
- 根据rowkey构建delete对象
- 执行delete请求
- 关闭表
例子:
@Test
public void deleteTest() throws IOException {
// 1. 获取HTable对象
Table table = connection.getTable(TABLE_NAME);
// 2. 根据rowkey构建delete对象
Delete delete = new Delete(Bytes.toBytes("4944191"));
// 3. 执行delete请求
table.delete(delete);
// 4. 关闭表
table.close();
}
总代码
1.TableAdminTest.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package cn.itcast.hbase.admin.api_test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.util.Bytes;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TableAdminTest {
private Connection connection;
private Admin admin;
public TableAdminTest() {
}
@BeforeTest
public void beforeTest() throws IOException {
Configuration configuration = HBaseConfiguration.create();
this.connection = ConnectionFactory.createConnection(configuration);
this.admin = this.connection.getAdmin();
}
@Test
public void createTableTest() throws IOException {
TableName tableName = TableName.valueOf("WATER_BILL");
if (!this.admin.tableExists(tableName)) {
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("C1"));
ColumnFamilyDescriptor cfDes = columnFamilyDescriptorBuilder.build();
tableDescriptorBuilder.setColumnFamily(cfDes);
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
this.admin.createTable(tableDescriptor);
}
}
@Test
public void deleteTableTest() throws IOException {
TableName tableName = TableName.valueOf("WATER_BILL");
if (this.admin.tableExists(tableName)) {
this.admin.disableTable(tableName);
this.admin.deleteTable(tableName);
}
}
@AfterTest
public void afterTest() throws IOException {
this.admin.close();
this.connection.close();
}
}
2.DataOpTest.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package cn.itcast.hbase.data.api_test;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DataOpTest {
private Connection connection;
private TableName TABLE_NAME = TableName.valueOf("WATER_BILL");
public DataOpTest() {
}
@BeforeTest
public void beforeTest() throws IOException {
Configuration configuration = HBaseConfiguration.create();
this.connection = ConnectionFactory.createConnection(configuration);
}
@Test
public void putTest() throws IOException {
Table table = this.connection.getTable(this.TABLE_NAME);
String rowkey = "4944191";
String columnFamily = "C1";
String columnName = "NAME";
String columnNameADDRESS = "ADDRESS";
String columnNameSEX = "SEX";
String columnNamePAY_DATE = "PAY_DATE";
String columnNameNUM_CURRENT = "NUM_CURRENT";
String columnNameNUM_PREVIOUS = "NUM_PREVIOUS";
String columnNameNUM_USAGE = "NUM_USAGE";
String columnNameTOTAL_MONEY = "TOTAL_MONEY";
String columnNameRECORD_DATE = "RECORD_DATE";
String columnNameLATEST_DATE = "LATEST_DATE";
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), Bytes.toBytes("登卫红"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameADDRESS), Bytes.toBytes("贵州省铜仁市德江县7单元267室"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameSEX), Bytes.toBytes("男"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNamePAY_DATE), Bytes.toBytes("2020-05-10"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameNUM_CURRENT), Bytes.toBytes("308.1"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameNUM_PREVIOUS), Bytes.toBytes("283.1"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameNUM_USAGE), Bytes.toBytes("25"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameTOTAL_MONEY), Bytes.toBytes("150"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameRECORD_DATE), Bytes.toBytes("2020-04-25"));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnNameLATEST_DATE), Bytes.toBytes("2020-06-09"));
table.put(put);
table.close();
}
@Test
public void getTest() throws IOException {
Table table = this.connection.getTable(this.TABLE_NAME);
Get get = new Get(Bytes.toBytes("4944191"));
Result result = table.get(get);
List<Cell> cellList = result.listCells();
byte[] rowkey = result.getRow();
System.out.println(Bytes.toString(rowkey));
Iterator var6 = cellList.iterator();
while(var6.hasNext()) {
Cell cell = (Cell)var6.next();
String cf = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(cf + ":" + columnName + " -> " + value);
}
table.close();
}
@Test
public void deleteTest() throws IOException {
Table table = this.connection.getTable(this.TABLE_NAME);
Delete delete = new Delete(Bytes.toBytes("4944191"));
table.delete(delete);
table.close();
}
@AfterTest
public void afterTest() throws IOException {
this.connection.close();
}
}


浙公网安备 33010602011771号