【6-6】HBASE的Java接口(2)

本文介绍了一个使用 Java 实现 HBase 数据库的增删改查操作的示例。通过具体代码展示了如何进行数据的插入、查询、批量插入及删除等基本操作。

使用Java接口实现增删改查。

在这里面我们要实现多个方法,因此我们使用jUnit test比较好

 

package cn.itcast.hbase;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
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.RetriesExhaustedWithDetailsException;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

public class HBaseDemo {
	
	
	private Configuration conf = null;
	
	//这个方法在我所有的junittest方法之前执行,应该加before标签【查1:junittest】
	@Before
	public void init(){
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "itcast04:21");
	}
	
	//配置已经装好了,现在先来一个什么方法呢 插入数据
	@Test
	public void testPut() throws Exception{
		//我得先得到一个表对象table,才能往里插入数据
		HTable table = new HTable(conf, "peoples");//我现在得到了想操作的这张表,接下来插入操作,
		Put put = new Put(Bytes.toBytes("kr0001"));
		put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhagnsf"));
		//以前只能插入一个属性,现在,我想插多少插多少,复制粘贴【查2:bytes.toBytes】
		put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("35"));
		put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(60000));
		table.put(put);//这里面put的是一个put对象,因此我们给new一个
		table.close();//好借好还
		//接下来插入100w条,500w条看看时间。【如果在oracle或者MySQL会很费时间】
	}
	
	@Test
	public void testPutAll() throws Exception, IOException{
		//HTablePool HTablePool = new HTablePool(config, maxSize)这个不支持了,还是用HTable吧
		HTable table = new HTable(conf, "peoples");
		List<Put> puts = new ArrayList<Put>();
		for(int i =1;i<=10000000;i++){
			Put put = new Put(Bytes.toBytes("kr"+i));
			put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes("zhagnsf"));
			puts.add(put);//
		}
				table.put(puts);
				table.close();
				//用这个一个一个的并不好,现在
	}
	
	
	@Test
	public void testGet() throws Exception{
		HTable table = new HTable(conf, "peoples");//得到一个table对象
		Get get = new Get(Bytes.toBytes("kr99999"));
		table.get(get);//调用table对象的一个方法,它返回的是一个结果集
		Result result = table.get(get);
		String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
		//getValue返回的是一个byte数组,如何转换成int或string类型?工具类,bytes.toString
		System.out.println(r);//打印一下
		table.close();
	}
	
	
	//查多个
	@Test
	public void testScan(){
		HTable table = new HTable(conf,"peoples");
		Scan scan = new Scan(Bytes.toBytes("kr88888"), Bytes.toBytes("kr300000));
		table.getScanner(scan);//它要什么我new什么,要scan我就在上面new一个scan
		//它返回的是一个结果集,resultscanner
		//它是怎么一下子弄出来resultscanner的?
		ResultScanner scanner = table.getScanner(scan);
		for(Result result:scanner){
			String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
			System.out.println(r);
		}
		
		
	}
	
	//删除
	@Test
	public void testDel() throws Exception{
		HTable table = new HTable(conf,"people");
		Delete delete = new Delete(Bytes.toBytes("kr9999"));
		table.delete(delete);
		table.close();
	}
	
	
	
	
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值