记录常用的一些Hbase API
package datacool.hadoop.hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
public class HBaseDao {
private static Connection connection;
private static Admin admin;
private static Configuration hbaseConf;
static {
hbaseConf = HBaseConfiguration.create();
hbaseConf.set("hbase.zookeeper.quorum", "Ubuntu01:2182");
//hbaseConf.addResource(new Path("$HBASE_HOME/conf", "hbase-site.xml"));
//hbaseConf.addResource(new Path("$HADOOP_HOME/etc/hadoop", "core-site.xml"));
try {
connection = ConnectionFactory.createConnection(hbaseConf);
admin = connection.getAdmin();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void createTable(String tableName,List<String> list) {
HTableDescriptor tabdesc = new HTableDescriptor(TableName.valueOf(tableName));
for(String family:list) {
tabdesc.addFamily(new HColumnDescriptor(family));
}
try {
if(admin.tableExists(TableName.valueOf(tableName))) {
System.out.println("exit this table");
return ;
}
admin.createTable(tabdesc);
System.out.println("建表成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void dropTable(String tableName) {
try {
if(admin.tableExists(TableName.valueOf(tableName))) {
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("删除"+tableName+"成功");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void addFamilyColumn(String tableName,List<String> list) {
try {
if(admin.tableExists(TableName.valueOf(tableName))) {
for(String family:list) {
admin.addColumn(TableName.valueOf(tableName),new HColumnDescriptor(family));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void enableTable(String tableName) {
try {
admin.enableTable(TableName.valueOf(tableName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void disableTable(String tableName) {
try {
admin.disableTable(TableName.valueOf(tableName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void listTable() {
try {
HTableDescriptor[] listTables = admin.listTables();
for(HTableDescriptor htable:listTables) {
System.out.println(htable);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void insertData(String tablename,String row,String cf,String cn,long ts,String value) {
try {
Table table = connection.getTable(TableName.valueOf(tablename));
Put put = new Put(Bytes.toBytes(row));
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
table.put(put);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void selectData(String tablename,String row,String cf,String cn) {
try {
Table table = connection.getTable(TableName.valueOf(tablename));
Get get = new Get(Bytes.toBytes(row));
get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
List<Cell> listCells = table.get(get).listCells();
for(Cell cell:listCells) {
System.out.println(new String(CellUtil.cloneValue(cell)));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void selectDataWithCondition(String tablename,String cf,String cn,CompareOp op,String opnum) {
try {
Table table = connection.getTable(TableName.valueOf(tablename));
SingleColumnValueFilter singleColumnValueFilter =
new SingleColumnValueFilter(Bytes.toBytes(cf),Bytes.toBytes(cn),
op,
Bytes.toBytes(opnum));
/*
* SingleColumnValueFilter singleColumnValueFilter =
new SingleColumnValueFilter(Bytes.toBytes(cf),Bytes.toBytes(cn),
CompareOp.EQUAL,
"happy".getBytes());
* */
Scan scan = new Scan();
scan.setFilter(singleColumnValueFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
List<Cell> cells= result.listCells();
for (Cell cell : cells) {
String row1 = Bytes.toString(result.getRow());
String family1 = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("[row:"+row1+"],[family:"+family1+"],[qualifier:"+qualifier+"]"
+ ",[value:"+value+"],[time:"+cell.getTimestamp()+"]");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
//insertData("test","1","base_info","cn2",5,"happx");
selectDataWithCondition("test","base_info","cn",CompareOp.EQUAL,"happy");
}
}