Hadoop组件之文件存储系统HBase的API操作(二)
-
环境准备
新建项目,在pom.xml文件中添加依赖 <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-mapreduce --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-mapreduce</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13-rc-1</version> <scope>test</scope> </dependency>
-
HBaseAPI
- 为了后期方便调用,所以我们将Hbase的一些代码抽取出来,专门写一个工具类,方便后面API操作调用其方法
package com.ityouxin.utils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @program: hbase
* @description: api工具类
* @author: lhx
* @create: 2019-12-16 11:14
**/
public class HbaseHelper {
private static final Logger logger = LoggerFactory.getLogger(HbaseHelper.class);
private Configuration configuration ;
private Connection connection;
private Admin admin;
public HbaseHelper(){
}
//得到HbaseHelper对象的属性
private HbaseHelper(Configuration configuration) throws IOException {
this.configuration = configuration;
connection = ConnectionFactory.createConnection(this.configuration);
// connection = ConnectionFactory.createConnection(this.configuration);
admin = this.connection.getAdmin();
}
//得到HbaseHelper对象
public static HbaseHelper getHelper() throws IOException {
Configuration configuration = HBaseConfiguration.create();
HbaseHelper helper = new HbaseHelper(configuration);
return helper;
}
public Configuration getConfiguration() {
return this.configuration;
}
public Connection getConnection() {
return this.connection;
}
public Admin getAdmin() {
return this.admin;
}
//判断表是否存在
public boolean tableExists(String table) throws IOException {
TableName tableName = TableName.valueOf(table);
Admin admin = this.getAdmin();
boolean tableExists = admin.tableExists(tableName);
return tableExists;
}
/**补零
*@Description: source 源字符串
* @return: len 要求的长度
**/
public String lpad(String source,int len){
if (source!=null){
StringBuffer stringBuffer = new StringBuffer(source);
while (source.length()<len){
source = stringBuffer.insert(0, "0").toString();
}
}
return source;
}
//创建表
public void createTable(String table, List<String> failiesList) throws IOException {
//将string类型的表名转换成TableName对象
TableName tableName = TableName.valueOf(table);
//把传入的string类型的列族转换成ColumFamilyDescriptor对象
Collection<ColumnFamilyDescriptor> families = new ArrayList<ColumnFamilyDescriptor>();
for (String family : failiesList){
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder
.newBuilder(Bytes.toBytes(family))
.setMaxVersions(1)
.setMinVersions(1)
.build();
///将转换ColumnFamilyDescriptor添加到集合中
families.add(columnFamilyDescriptor);
}
TableDescriptor tableDescriptor = TableDescriptorBuilder
.newBuilder(tableName)
.setColumnFamilies(families)
.build();
this.getAdmin().createTable(tableDescriptor);
}
//禁用表
public void disableTaable(String table) throws IOException {
TableName tableName = TableName.valueOf(table);
getAdmin().disableTable(tableName);
}
//删除表
public void deleteTable(String table) throws IOException {
TableName tableName = TableName.valueOf(table);
this.disableTaable(table);
getAdmin().deleteTable(tableName);
}
//put,得到表的对象
public Table getTable(String table) throws IOException {
TableName tableName = TableName.valueOf(table);
return this.getConnection().getTable(tableName);
}
/**
*@Description:直接创建Put对象
* @return:
**/
public static Put createPut(String rowKey,List<String []> columnList){
Put put = new Put(Bytes.toBytes(rowKey));
for (String[] item : columnList) {
if(item[0].contains(":")){
String[] split = item[0].split(":");
if(item.length==2){
put.addColumn(
Bytes.toBytes(split[0]),
Bytes.toBytes(split[1]) ,
Bytes.toBytes( item[1]));
}else if(item.length==3){
put.addColumn(
Bytes.toBytes(split[0]),
Bytes.toBytes(split[1]) ,
Long.parseLong(item[2]),
Bytes.toBytes( item[1]));
}
}else {
if(item.length>=3) {
// info Aplle Red
put.addColumn(
Bytes.toBytes(item[0]),
Bytes.toBytes(item[1]),
Bytes.toBytes(item[2]));
}
}
}
return put;
}
/**
*@Description: Row 转 Put 将行转换为put对象,进行封装数据
* @return:
**/
public static Put RowToPut(Row row){
//判断封装的行是否有数据
if (row==null){
return null;
}
Put put = new Put(Bytes.toBytes(row.key));
/*判断集合中是否有值*/
if (row.getColumns()!=null){
for (Column column : row.getColumns()){
//添加行的数据到put中
if (column.getTs()==null){
put.addColumn(
Bytes.toBytes(column.family),
Bytes.toBytes(column.getName()),
Bytes.toBytes(column.getValue())
);
}else {
put.addColumn(
Bytes.toBytes(column.family),
Bytes.toBytes(column.getName()),
column.getTs(),
Bytes.toBytes(column.getValue())
);
}
}
}
return put;
}
/**
*@Description: 将创建行的再次进行封装,创建一个createRow方法
* @return:
**/
public static Row createRow(String rowKey,List<String[]> columns){
if (rowKey == null || columns ==null) {
return null;
}
Row row = new Row(rowKey);
List<Column> columnList = new ArrayList<>();
row.setColumns(columnList);
for (String [] column : columns) {
//判断数组中的元素
if (column[0].contains(":")){
if (column.length==2){
String [] split = column[0].split(":");
Column column1 = new Column(split[0], split[1], column[1], null);
columnList.add(column1);
}else if (column.length==3){
String[] split = column[0].split(":");
Column column1 = new Column(split[0], split[1], column[1], Long.parseLong(new String(String.valueOf(System.currentTimeMillis()))));
columnList.add(column1);
}
}
}
return row;
}
/**
*@Description: 自己封装的列对象 封装好列的每个属性
* @return:
**/
public static class Column{
private String family;
private String name;
private String value;
private Long ts;
public Column(String family, String name, String value, Long ts) {
this.family = family;
this.name = name;
this.value = value;
this.ts = ts;
}
public Column() {
}
public String getFamily() {
return family;
}
public void setFamily(String family) {
this.family = family;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Long getTs() {
return ts;
}
public void setTs(Long ts) {
this.ts = ts;
}
}
/**
*@Description: 自己封装的行对象 行里面封装了RowKey以及列的集合
* @return:
**/
public static class Row{
private String key;
private List<Column> columns;
public Row() {
}
public Row(String key) {
this.key = key;
}
public Row(String key, List<Column> columns) {
this.key = key;
this.columns = columns;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
public void closeConnect(){
if (this.connection!=null){
try {
this.connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
-
测试Hbase的API方法,创建表、删除表、添加数据、查询数据
package com.ityouxin; import com.ityouxin.utils.HbaseHelper; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; 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.ArrayList; import java.util.Arrays; import java.util.List; /** * @program: TestHbase * @description: hbaseAPI * @author: lhx * @create: 2019-12-16 09:16 **/ public class TestHbase { // private Configuration conf; // private Connection connection; // private Admin admin; private HbaseHelper hbaseHelper; @Before public void getConfguration() throws IOException { hbaseHelper = HbaseHelper.getHelper(); // //获取配置对象 // conf = HBaseConfiguration.create(); // //获取连接对象 // connection = ConnectionFactory.createConnection(conf); // //通过admin操作命名空间 // admin = connection.getAdmin(); } //创建命名空间 @Test public void testNs() throws IOException { //命名空间的描述对象 NamespaceDescriptor ns1 = NamespaceDescriptor.create("ns12").build(); //创建命名空间 hbaseHelper.getAdmin().createNamespace(ns1); } //创建表 @Test public void createTable() throws IOException { List<String> list = new ArrayList<>(); String tableName="testtable"; if (!hbaseHelper.tableExists(tableName)){ list.add("info"); list.add("location"); hbaseHelper.createTable(tableName,list); }else { System.out.println("表已经纯在了!!!!!!"); } } //删除表 @Test public void deleteTable() throws IOException { hbaseHelper.deleteTable("emp_info"); } //put数据到表中 @Test public void putDataToTable() { //put 'table','rowkey','column','value' Table student = null; try { //获取表 student = hbaseHelper.getTable("student"); /* //操作 Put put = new Put(Bytes.toBytes("03")); byte[] family = Bytes.toBytes("info"); byte[] column = Bytes.toBytes("name"); byte[] value = Bytes.toBytes("lhx"); put.addColumn(family, column, value); */ // HbaseHelper.Row row = new HbaseHelper.Row(); row.setKey("04"); List<HbaseHelper.Column> columns = new ArrayList<>(); columns.add(new HbaseHelper.Column("info","name","lhx",System.currentTimeMillis())); columns.add(new HbaseHelper.Column("info","name","lhx",System.currentTimeMillis())); row.setColumns(columns); /* HbaseHelper.Row row = hbaseHelper.createRow("05", Arrays.asList( new String[]{"info:name","zhansgan",String.valueOf(System.currentTimeMillis())}, new String[]{"info:sex","45"} // new String[]{"location:city","shanghai"} )); Put put=hbaseHelper.RowToPut(row); */ Put put = HbaseHelper.createPut("1002",Arrays.asList( new String[]{"info:name","lisi",String.valueOf(System.currentTimeMillis())}, new String[]{"info:sex","male"} )); student.put(put); } catch (IOException e) { e.printStackTrace(); } finally { try { student.close(); } catch (IOException e) { e.printStackTrace(); } } } //查询表中数据 get scan @Test public void queryData() throws IOException { //得到table对象 Table student = hbaseHelper.getTable("student"); Get get = new Get(Bytes.toBytes("1001")); Result result = student.get(get); List<Cell> cells = result.listCells(); for (Cell cell : cells){ byte[] row = CellUtil.cloneRow(cell); byte[] family = CellUtil.cloneFamily(cell); byte[] column = CellUtil.cloneQualifier(cell); byte[] value = CellUtil.cloneValue(cell); String string = CellUtil.toString(cell, true); System.out.println(string); } } @Test public void deleteRow() throws IOException { Table student = hbaseHelper.getTable("student"); Delete delete = new Delete(Bytes.toBytes("01")); student.delete(delete); } @Test public void testTable() throws IOException { //family-1,family-2 // family-1,family-2 // family-1: col-1 col-2 col-3 col-4 col-5 // family-2: col-1 col-2 col-3 col-4 col-5 // value val-row.family.col // 1000 rows // List<String> families = new ArrayList<String>(); // families.add("family-1"); // families.add("family-2"); // hbaseHelper.createTable("testtable",families); Table table = hbaseHelper.getTable("testtable"); List<Put> puts = new ArrayList<>(); //0-999 --> 000-999 for (int i = 0; i <1000 ; i++) { //"family-1:col-1","val-row.family.col" List<String[]> columns = new ArrayList<>(); for (int j = 0; j <3 ; j++) {//family for (int k = 0; k <5 ; k++) {//col String [] item = new String[2]; item[0] = "family-" + j + ":" + "col-" + k; item[1] = "val-" + i + "." + j + "." + k ; System.out.println(Arrays.toString(item)); columns.add(item); } } HbaseHelper.Row row = hbaseHelper.createRow(hbaseHelper.lpad(i + "", 3), columns); Put put = hbaseHelper.RowToPut(row); puts.add(put); table.put(put); } table.put(puts); table.close(); } @After public void close() throws IOException { //关闭连接对象 // if (connection!=null){ // try { // connection.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } hbaseHelper.getConnection().close(); } }