package hbase;
import java.io.IOException;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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;
/**
* @author Administrator
*
*/
public class HBaseDML extends HBaseAb {
/**
* 表连接池
*/
private static HTablePool _tPool = null;
/**
* 表连接池中最大初始化连接数量
*/
private static int maxSize = 1000;
static{
_tPool = new HTablePool(conf, maxSize);
}
/**
* 根据表名和列标识查找一列的数据
* @param tableName
* @param key
* @throws IOException
*/
public void getOneRow(String tableName,String key) throws IOException{
HTableInterface table = _tPool.getTable(tableName);
Get get = new Get(key.getBytes());
Result res = table.get(get);
for (KeyValue entry : res.raw()) {
System.out.println("*******************************************************************");
System.out.println("Family:"+new String(entry.getFamily(), "UTF-8"));
System.out.println("Row:"+new String(entry.getRow(),"UTF-8"));
System.out.println("Qualifier:"+new String(entry.getQualifier(), "UTF-8"));
System.out.println("Timestamp:"+entry.getTimestamp());
System.out.println("Value:"+new String(entry.getValue(),"UTF-8"));
System.out.println("*******************************************************************");
}
}
/**
* 得到一张表中的所有数据
* @param tableName
* @throws IOException
*/
public void getAllRows(String tableName) throws IOException{
HTableInterface table = _tPool.getTable(tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
for (Result result : rs) {
System.out.println("_________________________________________________________________");
for (KeyValue entry : result.raw()) {
System.out.println("*******************************************************************");
System.out.println("Family:"+new String(entry.getFamily(), "UTF-8"));
System.out.println("Row:"+new String(entry.getRow(),"UTF-8"));
System.out.println("Qualifier:"+new String(entry.getQualifier(), "UTF-8"));
System.out.println("Timestamp:"+entry.getTimestamp());
System.out.println("Value:"+new String(entry.getValue(),"UTF-8"));
System.out.println("*******************************************************************");
}
System.out.println("_________________________________________________________________");
}
}
/**
* 删除一行数据
* @param tableName
* @param key
* @throws IOException
*/
public void deleteOneRow(String tableName,String key) throws IOException{
HTableInterface table = _tPool.getTable(tableName);
Delete delete = new Delete(key.getBytes());
table.delete(delete);
}
/**
* 添加一行数据
* @param tableName
* @param key
* @param family
* @param qualifier
* @param value
* @throws IOException
*/
public void addOneRow(String tableName,String key,String family,String qualifier,String value) throws IOException{
HTableInterface table = _tPool.getTable(tableName);
Put put = new Put(key.getBytes());
put.add(family.getBytes(), qualifier.getBytes(), value.getBytes());
table.put(put);
}
public static void main(String[] args) {
HBaseDML hbase_dml = new HBaseDML();
try {
// hbase_dml.getOneRow("user", "zhuss");
hbase_dml.getAllRows("user");
// hbase_dml.addOneRow("user", "zhushunshan", "address", "Local", "河南省淮阳县朱庄");
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}