要看hadoop和hbase搭建过程的通过传送门过去看:
hadoop:https://blog.csdn.net/qq1049545450/article/details/90019159
hbase:https://blog.csdn.net/qq1049545450/article/details/90023386
在使用java操作Hbase数据库之前,先进行以下配置:
对本地的hosts(C:\Windows\System32\drivers\etc\hosts)进行配置,在hosts文件后面加上:
192.168.5.133 hbasehost-server hbasehost
项目中引入hbase客户端的maven依赖,版本最好以hbase的版本保持一致:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.5</version>
</dependency>
将服务器上的hadoop和hbase的几个配置文件拷贝至resources目录下:
开始使用api操作hbase:
api参考:Apache HBase 2.1.9 API ,HBase的Java API操作-腾讯云开发者社区-腾讯云
直接上代码:
public class HBaseUtil {
/**
<br>功能描述: 初始化配置
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 11:45
<br>修改记录: {修改人 修改原因 修改时间}
* @param
* @throws
* @return org.apache.hadoop.conf.Configuration
* @see #
*/
public static Configuration initConfig(){
Configuration config = new Configuration();
//记住 写服务名
config.set("hbase.zookeeper.quorum","hbasehost");
return config;
}
/**
<br>功能描述: 获取数据库管理员对象
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 11:46
<br>修改记录: {修改人 修改原因 修改时间}
* @throws
* @return org.apache.hadoop.hbase.client.Admin
* @see #
*/
public static Admin getAdmin() throws IOException {
Configuration conf = initConfig();
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
return admin;
}
/**
<br>功能描述: 检查hbase是否可用
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 12:30
<br>修改记录: {修改人 修改原因 修改时间}
* @param
* @throws
* @return void
* @see #
*/
public static void isAvailable() throws IOException {
Configuration conf = initConfig();
HBaseAdmin.available(conf);
}
/**
<br>功能描述: 获取表名集合
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 12:34
<br>修改记录: {修改人 修改原因 修改时间}
* @param
* @throws
* @return java.util.List<java.lang.String>
* @see #
*/
public static List<String> getTables() throws IOException {
Admin admin = getAdmin();
TableName[] tableNames = admin.listTableNames();
List<String> tables = new ArrayList<String>();
for(TableName tableName:tableNames){
tables.add(tableName.getNameAsString());
}
return tables;
}
/**
<br>功能描述: 创建表
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:09
<br>修改记录: {修改人 修改原因 修改时间}
* @param tableName
* @param cols
* @throws
* @return void
* @see #
*/
public static void createTable(String tableName,List<String> cols) throws IOException {
TableName tn = TableName.valueOf(tableName);
HBaseAdmin hBaseAdmin = (HBaseAdmin) getAdmin();
if (hBaseAdmin.tableExists(tn)) {
System.out.println("talbe is exists!");
} else {
TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(tn);
for (String col : cols) {
ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.of(col);
tdb.setColumnFamily(cfd);
}
hBaseAdmin.createTable(tdb.build());
}
hBaseAdmin.close();
}
/**
<br>功能描述: 往表里面插值
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:33
<br>修改记录: {修改人 修改原因 修改时间}
* @param tableName
* @param rowKey
* @param columnFamilys
* @param colValue
* @throws
* @return void
* @see #
*/
public static void put(String tableName,String rowKey,String columnFamilys,Map<String,Object> colValue) throws IOException {
Connection conn = getAdmin().getConnection();
Table table = conn.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
for(String col:colValue.keySet()){
put.addColumn(Bytes.toBytes(columnFamilys), Bytes.toBytes(col), ByteArrayUtils.objectToBytes(colValue.get(col)).get());
}
table.put(put);
table.close();
}
/**
<br>功能描述: 删除某行数据
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:35
<br>修改记录: {修改人 修改原因 修改时间}
* @param tableName
* @param rowKey
* @throws
* @return void
* @see #
*/
public static void delete(String tableName,String rowKey) throws IOException {
Connection conn = getAdmin().getConnection();
Table table = conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
table.close();
}
/**
<br>功能描述: 获取行
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:07
<br>修改记录: {修改人 修改原因 修改时间}
* @param tableName
* @param rowKey
* @throws
* @return java.util.List<com.alibaba.fastjson.JSONObject>
* @see #
*/
public static List<JSONObject> get(String tableName, String rowKey) throws IOException {
return get(tableName,rowKey,null, (List<String>) null);
}
/**
<br>功能描述: 获取行的具体单元格数据
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:07
<br>修改记录: {修改人 修改原因 修改时间}
* @param tableName
* @param rowKey
* @param columnFamilys
* @param col
* @throws
* @return com.alibaba.fastjson.JSONObject
* @see #
*/
public static JSONObject get(String tableName,String rowKey,String columnFamilys,String col) throws IOException {
List<String> cols = new ArrayList<>();
cols.add(col);
List<JSONObject> objs = get(tableName,rowKey,columnFamilys,cols);
if(objs!=null&&objs.size()>0){
return objs.get(0);
}
return null;
}
/**
<br>功能描述: 获取行指定列簇的多个单元格数据
<br>处理逻辑:
<br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:08
<br>修改记录: {修改人 修改原因 修改时间}
* @param tableName
* @param rowKey
* @param columnFamilys
* @param cols
* @throws
* @return java.util.List<com.alibaba.fastjson.JSONObject>
* @see #
*/
public static List<JSONObject> get(String tableName,String rowKey,String columnFamilys,List<String> cols) throws IOException {
Connection conn = getAdmin().getConnection();
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
if(cols!=null){
for(String col:cols){
get.addColumn(Bytes.toBytes(columnFamilys),Bytes.toBytes(col));
}
}
Result result = table.get(get);
Cell[] cells = result.rawCells();
List<JSONObject> objs = new ArrayList<>();
for(Cell cell:cells){
JSONObject obj = new JSONObject();
obj.put("columnFamilys",Bytes.toString(CellUtil.cloneFamily(cell)));
obj.put("qualifie",Bytes.toString(CellUtil.cloneQualifier(cell)));
obj.put("value",ByteArrayUtils.bytesToObject(CellUtil.cloneValue(cell)).get());
objs.add(obj);
}
return objs;
}
}