客户端HbaseUtil
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.log4j.Logger;
import java.io.IOException;
public class HbaseUtil {
private static final Logger logger = Logger.getLogger(HbaseUtil.class);
private static Connection conn =null;
private static Admin admin=null;
private static Configuration conf=null;
private static final String CONNECT_STR = "hbase.zookeeper.quorum";
private static final String CONNECT_VALUE = "192.168.157.132:2181";
static {
conf= HBaseConfiguration.create();
conf.set(CONNECT_STR,CONNECT_VALUE);
System.out.println("conf:"+conf);
try {
conn = ConnectionFactory.createConnection(conf);
System.out.println("conn:"+conn);
} catch (IOException e) {
logger.error("获取客户操作失败");
}
}
public static Admin getAdmin() {
try {
admin = conn.getAdmin();
System.out.println("admin:"+admin);
return admin;
} catch (IOException e) {
logger.error("获取客户操作失败");
}
return admin;
}
public static void close(Admin admin){
if(admin==null){return;}
try {
admin.close();
} catch (IOException e) {
logger.error("获取客户操作失败");
}
}
public static void printRS(ResultScanner rsc) {
for(Result rs:rsc){
printRS(rs);
}
}
public static void printRS(Result rs) {
System.out.print("rowkey:"+new String(rs.getRow()));
CellScanner cellScanner=rs.cellScanner();
try {
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
System.out.print("\t" + new String(CellUtil.cloneFamily(cell)));
System.out.print(":" + new String(CellUtil.cloneQualifier(cell)));
System.out.println(": " + new String(CellUtil.cloneValue(cell)));
}
}catch (Exception e){
logger.error("获取客户操作失败");
}
}
public static Table getTable(){
return getTable("jxlg:test1");
}
public static Table getTable(String table){
TableName tableName=TableName.valueOf(table);
Table table1=null;
try {
table1=conn.getTable(tableName);
}catch (IOException e) {
logger.error("获取客户操作失败");
}
return table1;
}
public static void closeTable(Table table){
if(table==null)return;
try {
table.close();
} catch (IOException e) {
}
}
}