Hbase javaapi curd笔记

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值