首先 根據 hadoop 搭建 + hbase 搭建把 環境弄好

由於 hbase 依賴於 hdfs ,所以 需要 進入 hadoop --》sbin  下 啟動 start-dfs.sh , start-yarn.sh
然後 進 hbase --> 下 啟動 start-hbase.sh 如果 後面 執行失敗 報 找不到 zookeeper 八成 你需要進 hbase-bin-> stop-hbase 然後 重run start-hbase.sh

這裡列舉下 hbase shell 的常用操作

#最有用命令 help
help 'status'
#建表
create 'FileTable','fileInfo','saveInfo'
#列出有哪些表
list
#描述表資訊
desc 'FileTable'
#統計 表
count 'FileTable'
#新增列簇
alter 'FileTable','cf'
# 刪除列簇 ,一定要注意大小寫
alter 'FileTable',{NAME=>'cf',METHOD=>'delete'} #插入資料
put 'FileTable','rowkey1','fileInfo:name','file1.txt'
0 row(s) in 3.9840 seconds
put 'FileTable','rowkey1','fileInfo:type','txt'
0 row(s) in 0.0410 seconds
put 'FileTable','rowkey1','fileInfo:size','1024'
0 row(s) in 0.1100 seconds
put 'FileTable','rowkey1','saveInfo:path','/home/pics'
0 row(s) in 0.1320 seconds
put 'FileTable' , 'rowkey1','saveInfo:creator','tom'
0 row(s) in 0.1430 seconds
#查詢表總行數
hbase(main):016:0> count 'FileTable'
1 row(s) in 0.8500 seconds
# get 查詢
get 'FileTable','rowkey1'
get 'FileTable','rowkey1','fileInfo' #delete #deleteall #scan #刪除表之前必須先禁用表Drop the named table. Table must first be disabled:
disable 'FileTable' is_enabled
is_disabled drop 'FileTable'

查詢所有列簇

查詢指定列簇

HBase 連線類

package com.ghc.hbase.api;

import org.apache.hadoop.conf.Configuration;
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.Table; import java.io.IOException; public class HBaseConn {
private static final HBaseConn INSTANCE = new HBaseConn();
private static Configuration configuration;
private static Connection connection; private HBaseConn(){
try{
if(configuration == null){
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","192.168.32.129:2181");
}
}catch(Exception e){
e.printStackTrace();
}
}
private Connection getConnection(){
if(connection == null || connection.isClosed()){
try{
connection = ConnectionFactory.createConnection(configuration);
}catch(IOException e){
e.printStackTrace();
}
}
return connection;
} public static Connection getHBaseConnection(){
return INSTANCE.getConnection();
} public static Table getTable(String tableName) throws IOException{
return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
} public static void closeConn(){
if(connection != null){
try{
connection.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}

junit 測試一波連線類

import com.ghc.hbase.api.HBaseConn;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.junit.Test; import java.io.IOException; public class HBaseConnTest {
@Test
public void getConnTest(){
Connection conn = HBaseConn.getHBaseConnection();
System.out.println(conn.isClosed());
HBaseConn.closeConn();
System.out.println(conn.isClosed());
}
@Test
public void getTableTest(){
Table table = null;
try{table = HBaseConn.getTable("FileTable");
System.out.println(table.getName());
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}

hbase 增刪操作類

package com.ghc.hbase.api;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner; public class HBaseUtil { public static Boolean createTable(String tableName,String[] cfs){
try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
if(admin.tableExists(tableName)) {
return false;
}
// 不存在 則建立
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
Arrays.stream(cfs).forEach(cf->{
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hColumnDescriptor.setMaxVersions(1);
hTableDescriptor.addFamily(hColumnDescriptor);
});
admin.createTable(hTableDescriptor);
}catch(Exception e){
e.printStackTrace();
}
return true;
}
public static Boolean putRow(String tableName, String rowKey, String cfName,String qualifier,String data){
try(Table table = HBaseConn.getTable(tableName)){
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));
table.put(put);
}catch (IOException ioe){
ioe.printStackTrace();
}
return true;
} public static Boolean putRows(String tableName,List<Put> puts){
try(Table table = HBaseConn.getTable(tableName)){
table.put(puts);
}catch(IOException ioe){
ioe.printStackTrace();
}
return true;
} public static Result getRow(String tableName, String rowKey){
try(Table table = HBaseConn.getTable(tableName)){
Get get = new Get(Bytes.toBytes(rowKey));
return table.get(get);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
} public static Result getRow(String tableName, String rowKey, FilterList filterList){
try(Table table = HBaseConn.getTable(tableName)){
Get get = new Get(Bytes.toBytes(rowKey));
get.setFilter(filterList);
return table.get(get);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
} public static ResultScanner getScanner(String tableName){
try(Table table = HBaseConn.getTable(tableName)){
Scan scan = new Scan();
scan.setCaching(1000);
return table.getScanner(scan);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
} public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey){
try(Table table = HBaseConn.getTable(tableName)){
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scan.setCaching(1000);
return table.getScanner(scan);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
} // 使用過濾器
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey, FilterList filterList){
try(Table table = HBaseConn.getTable(tableName)){
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scan.setCaching(1000);
scan.setFilter(filterList);
return table.getScanner(scan);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
} // 刪除
public static Boolean deleteRow(String tableName,String rowKey){
try(Table table = HBaseConn.getTable(tableName)){
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
return true;
}catch(IOException ioe){ioe.printStackTrace();}
return null;
} // 刪除 列簇 用 admin
public static Boolean deleteColumnFamily(String tableName,String cfName){
try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
admin.deleteColumn(tableName,cfName);
}catch(IOException ioe){
ioe.printStackTrace();
}
return true;
}
//刪除 某列 用 table
public static Boolean deleteQualifier(String tableName, String rowKey, String cfName,String qualifier){
try(Table table = HBaseConn.getTable(tableName)){
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(qualifier));
table.delete(delete);
return true;
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
}
}