赞
踩
实际开发中可以利用javaAPI去操作控制Hbase
【准备】
1:开启集群,一次开启(zookeeper,hdfs,hbase)
2:创建项目,导入jar
3:代码实现
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
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.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
public class HbaseDemo {
private Configuration conf = null;
@Before
public void init(){
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop2004:2181,hadoop2005:2181,hadoop2006:2181");
}
@Test
public void testPut() throws Exception{
HTable table = new HTable(conf, "tab_dog");
Put put = new Put(Bytes.toBytes("rk0001"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("旺财"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("3"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("boy"));
table.put(put);
table.close();
}
@Test
public void testPutAll() throws Exception{
HTable table = new HTable(conf, "tab_dog");
List puts = new ArrayList(10000);
for(int i=0 ; i<1000000; i++){
Put put = new Put(Bytes.toBytes("rk"+i));
put.add(Bytes.toBytes("info"), Bytes.toBytes("sal"), Bytes.toBytes(""+i));
puts.add(put);
//这里防止数据量过大,内存溢出,所以10000条就插入一次,分批插入
if(i % 10000 == 0){
table.put(puts);
puts = new ArrayList(10000);
}
}
table.put(puts);
table.close();
}
@Test
public void testGet() throws Exception{
//HTablePool pool = new HTablePool(conf, 10);
//HTable table = (HTable) pool.getTable("user");<
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。