pom.xml
4.0.0
com.yc
HBaseDome
0.0.1-SNAPSHOT
jar
HBaseDome
http://maven.apache.org
UTF-8
junit
junit
4.12
org.apache.hbase
hbase-client
2.2.0
org.apache.hbase
hbase-server
2.2.0
org.apache.hbase
hbase-common
2.2.0
jdk.tools
jdk.tools
1.8
system
${JAVA_HOME}/lib/tools.jar
HBaseUtil
package com.yc.HBaseDome;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.client.coprocessor.Batch.Call;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hbase.thirdparty.io.netty.util.ResourceLeakTracker;
public class HBaseUtil {
private static Connection conn=null;
/**
* 建立连接
* */
static{
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "master,slave2,slave1");
conf.set("hbase.master", "master:60000");
try {
conn=ConnectionFactory.createConnection(conf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建表
* @throws IOException
* */
public void createTable(String name,String ...ColumnFamilys) throws IOException{
Admin admin=conn.getAdmin();
TableName tname=TableName.valueOf(name);
if(admin.tableExists(tname)){
System.out.println(name+"表已经存在");
}else{
TableDescriptorBuilder tdesc=TableDescriptorBuilder.newBuilder(tname);
for(String s: ColumnFamilys){
ColumnFamilyDescriptor cfd=ColumnFamilyDescriptorBuilder.of(s);
tdesc.setColumnFamily(cfd);
}
TableDescriptor desc=tdesc.build();
admin.createTable(desc);
}
}
/**
* 删除表
* @throws IOException
* */
public void deleteTable(String name) throws IOException{
Admin admin=conn.getAdmin();
TableName tableName=TableName.valueOf(name);
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
/**
* 批量创建表
* @throws IOException
* */
public void createTables(String[] names,List> ColumnFamilys) throws IOException{
Admin admin=conn.getAdmin();
if(names.length==ColumnFamilys.size()){
for(int i =0;i params) throws TableNotFoundException, IOException{
TableName tableName=TableName.valueOf(tname);
Table table=conn.getTable(tableName);
Put put=new Put(params.get("row").toString().getBytes());
for(Map.Entry m:params.entrySet()){
if(m.getKey().equals("row")){
continue;
}
put.addColumn(family.getBytes(), m.getKey().getBytes(), m.getValue().toString().getBytes());
}
table.put(put);
}
/**
* 批量添加数据
* @throws IOException
* params 数据结构为: {columnName:{family,Value},columnName:{family,Value}....}
* */
public void addrows(String tname,Map> params) throws IOException{
TableName tableName=TableName.valueOf(tname);
Table table=conn.getTable(tableName);
List listput=new ArrayList();
for(Map.Entry> map:params.entrySet()){
Put put=new Put(map.getKey().getBytes());
String family=map.getValue().get("family").toString();
for(Map.Entry m:map.getValue().entrySet()){
if(m.getKey().equals("row")){
continue;
}
put.addColumn(family.getBytes(), m.getKey().getBytes(), m.getValue().toString().getBytes());
}
listput.add(put);
}
table.put(listput);
}
/**
* 删除单row数据
* @throws IOException
* */
public void deleteRow(String tname,String row,Map params) throws IOException{
TableName tableName=TableName.valueOf(tname);
Table table=conn.getTable(tableName);
Delete delete=new Delete(row.getBytes());
if(params!=null){
for(Map.Entry m:params.entrySet()){
delete.addColumn(m.getKey().getBytes(), m.getValue().toString().getBytes());
}
}
table.delete(delete);
}
/**
* 批量删除数据
* */
public void deleteRows(String tname,Map params,String ...rows) throws IOException{
TableName tableName=TableName.valueOf(tname);
Table table=conn.getTable(tableName);
List deletes=new ArrayList();
for(String row:rows){
Delete delete=new Delete(row.getBytes());
if(params!=null){
for(Map.Entry m:params.entrySet()){
delete.addColumn(m.getKey().getBytes(), m.getValue().toString().getBytes());
}
}
deletes.add(delete);
}
table.delete(deletes);
}
/**
* 得到所有数据
* @throws IOException
* */
public String getAllDate(String tname) throws IOException{
TableName tableName=TableName.valueOf(tname);
Table table=conn.getTable(tableName);
Set familyNames=table.getDescriptor().getColumnFamilyNames();
String result="";
for(byte[] familuName:familyNames){
ResultScanner rs= table.getScanner(familuName);
Iterator iterator=rs.iterator();
while(iterator.hasNext()){
Result r=iterator.next();
for (Cell cell:r.rawCells()){
String family=Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String qualifier=Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String row=Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String value=Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
result+=row+","+family+","+qualifier+","+value+"\n";
}
}
}
return result;
}
/**
* 得到指定列数据
* @throws IOException
* */
public List get(String tname,String family,String qualifier) throws IOException{
TableName tableName=TableName.valueOf(tname);
Table table=conn.getTable(tableName);
ResultScanner rs= table.getScanner(family.getBytes(), qualifier.getBytes());
Iterator iterator=rs.iterator();
List list=new ArrayList();
while(iterator.hasNext()){
Result r=iterator.next();
for (Cell cell:r.rawCells()){
String value=Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
list.add(value);
}
}
return list;
}
}