当前位置:   article > 正文

hbase java_JAVA操作Hbase

[main] client.hbaseadmin (hbaseadmin.java:postoperationresult(3730)) - opera

实际开发中可以利用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");<

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/754105
推荐阅读
相关标签
  

闽ICP备14008679号