您的位置:首页 > 编程语言 > Java开发

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

2016-02-25 21:17 477 查看
使用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 自动生成的方法存根

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: