#博学谷IT学习技术支持#
在idea中创建Maven工程,导入HBase 客户端相关jar包,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>
<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>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>compile</scope>
</dependency>
</dependencies>
并在resouces下引入log4j.properties文件,可以在控制台清晰地看到日志信息:
log4j.rootLogger=INFO,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
Configuration conf = new Configuration();
conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
return addHbaseResources(conf);
封装创建链接和释放连接方法,创建连接时需要指定ZK集群的IP和端口信息,从而通过ZK集群找到HBase的元数据信息,建立连接:
public static Connection getConnection(String zkIPStr) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", zkIPStr);
return ConnectionFactory.createConnection(conf);
}
public static void closeConn(Connection conn, Admin admin, Table table) {
if (table != null) {
try {
table.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void createTable() throws IOException {
//创建连接
//获取管理对象
Admin admin = conn.getAdmin();
//进行操作
if (admin.tableExists(TableName.valueOf("WATER_BILL"))) {
return;
}
TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TableName.valueOf("WATER_BILL"))
.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder("C1".getBytes()).build()).build();
admin.createTable(tableDesc);
//查询结果集
//释放连接
}
@Test
public void putData() throws IOException {
table = conn.getTable(TableName.valueOf("WATER_BILL"));
Put put = new Put("rk001".getBytes());
put.addColumn("f1".getBytes(), "name".getBytes(), "登卫红".getBytes());
put.addColumn("f1".getBytes(), "address".getBytes(), "贵州省铜仁市".getBytes());
put.addColumn("f1".getBytes(), "sex".getBytes(), "男".getBytes());
table.put(put);
}
@Test
public void queryData() throws IOException {
table = conn.getTable(TableName.valueOf("WATER_BILL"));
Get get = new Get("rk001".getBytes());
Result result = table.get(get);
List<Cell> cells = result.listCells();
if (cells != null) {
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String columnName = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("rowkey: " + row + ", family:column " + family + ":"
+ columnName + ", value: " + value );
}
} else {
System.out.println("No data");
}
}
@Test
public void deleteData() throws IOException {
table = conn.getTable(TableName.valueOf("WATER_BILL"));
Delete delete = new Delete("rk001".getBytes());
table.delete(delete);
}
@Test
public void deleteTable() throws IOException {
admin = conn.getAdmin();
if (admin.isTableEnabled(TableName.valueOf("WATER_BILL"))) {
admin.disableTable(TableName.valueOf("WATER_BILL"));
}
admin.deleteTable(TableName.valueOf("WATER_BILL"));
}
@Test
public void scanTable() throws IOException {
Scan scan = new Scan();
table = conn.getTable(TableName.valueOf("WATER_BILL"));
BinaryComparator startTimeComp = new BinaryComparator("2020-06-01".getBytes());
BinaryComparator endTimeComp = new BinaryComparator("2020-06-30".getBytes());
SingleColumnValueFilter startTimeFilter
= new SingleColumnValueFilter("C1".getBytes(), "RECORD_DATE".getBytes(),
CompareOperator.GREATER_OR_EQUAL, startTimeComp);
SingleColumnValueFilter endTimeFilter
= new SingleColumnValueFilter("C1".getBytes(), "RECORD_DATE".getBytes(),
CompareOperator.LESS_OR_EQUAL, endTimeComp);
FilterList filterList = new FilterList();
filterList.addFilter(startTimeFilter);
filterList.addFilter(endTimeFilter);
scan.setFilter(filterList);
scan.addColumn("C1".getBytes(), "RECORD_DATE".getBytes());
scan.addColumn("C1".getBytes(), "NAME".getBytes());
scan.addColumn("C1".getBytes(), "NUM_USAGE".getBytes());
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()) {
Result next = iterator.next();
List<Cell> cells = next.listCells();
if (cells != null) {
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String columnName = Bytes.toString(CellUtil.cloneQualifier(cell));
Object value;
if ("NUM_USAGE".equals(columnName)) {
value = Bytes.toDouble(CellUtil.cloneValue(cell));
} else {
value = Bytes.toString(CellUtil.cloneValue(cell));
}
System.out.println("rowkey: " + row + ", family:column " + family + ":"
+ columnName + ", value: " + value );
}
} else {
System.out.println("No data");
}
}
}