import java.io.IOException;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
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.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class Test extends HBaseDao{
static{
HBaseDao.init();
}
//添加一条数据
static public void addRaw(){
Table table = null;
try {
table = conn.getTable(TableName.valueOf("FULL_PHONE_BY_SIMPLEMD5"));
Put put = new Put(Bytes.toBytes("测试"));//指定行
put.addColumn(Bytes.toBytes("d"), Bytes.toBytes("phone"), Bytes.toBytes("测试"));
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (table != null) {
try {
table.close();
System.out.println("成功添加");
} catch (IOException e) {
}
}
}
}
//删除一条数据
static public void delRaw(){
Table table = null;
try {
table = conn.getTable(TableName.valueOf("FULL_PHONE_BY_SIMPLEMD5"));
Delete delete = new Delete(Bytes.toBytes("测试"));
//delete.addColumn(family, qualifier) //指定具体列
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (table != null) {
try {
table.close();
System.out.println("成功删除");
} catch (IOException e) {
}
}
}
}
// 获取一条数据
static public void getRaw(){
Table table = null;
try {
table = conn.getTable(TableName.valueOf("FULL_PHONE_BY_SIMPLEMD5"));
Get get = new Get(Bytes.toBytes("测试")); //table.exists(new Get(Bytes.toBytes(rowKey))) 判断rowkey是否存在
Result result = table.get(get);
for(Cell cell :result.rawCells()){
System.out.print("行健: "+Bytes.toString(CellUtil.cloneRow(cell)));
System.out.print("列簇: "+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print(" 列: "+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.print(" 值: "+Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("时间戳: "+cell.getTimestamp());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (table != null) {
try {
table.close();
System.out.println("查询完成");
} catch (IOException e) {
}
}
}
}
//
// 获取一条数据
static public void getAllRaws(){
Table table = null;
try {
table = conn.getTable(TableName.valueOf("FULL_PHONE_BY_SIMPLEMD5"));
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for(Result result:resultScanner){
for(Cell cell :result.rawCells()){
System.out.print("行健: "+Bytes.toString(CellUtil.cloneRow(cell))+" ");
System.out.print("列簇: "+Bytes.toString(CellUtil.cloneFamily(cell))+" ");
System.out.print(" 列: "+Bytes.toString(CellUtil.cloneQualifier(cell))+" ");
System.out.print(" 值: "+Bytes.toString(CellUtil.cloneValue(cell))+" ");
System.out.println("时间戳: "+cell.getTimestamp());
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (table != null) {
try {
table.close();
System.out.println("查询完成");
} catch (IOException e) {
}
}
}
}
public static void main(String[] args) {
getAllRaws();
}
}
记录一下," 值: "+Bytes.toString(CellUtil.cloneValue(cell)) 改为new String(CellUtil.cloneValue(cell)) 如果值为中文,乱码了...
批量插入操作和使用缓冲区提高效率http://blog.csdn.net/u011518678/article/details/50790193