java怎么操作hbase_java对HBase的基本操作

package

com.hbase.myhbase;

import

java.io.IOException;

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.HColumnDescriptor;

import

org.apache.hadoop.hbase.HTableDescriptor;

import

org.apache.hadoop.hbase.TableName;

import

org.apache.hadoop.hbase.client.Admin;

import

org.apache.hadoop.hbase.client.Connection;

import

org.apache.hadoop.hbase.client.ConnectionFactory;

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;

import

org.junit.Before;

import

org.junit.Test;

public

class

HBaseDemo {

private

Configuration conf = null;

private

Connection con = null;

@Before

public

void

init()

throws

IOException {

conf = HBaseConfiguration.create();

conf.set(

"hbase.zookeeper.quorum"

,

"worker05:2181,worker06:2181,worker07:2181"

);

con = ConnectionFactory.createConnection(conf);

}

@Test

public

void

testCreateTable()

throws

IOException {

Admin admin = ConnectionFactory.createConnection(conf).getAdmin();

HTableDescriptor htd =

new

HTableDescriptor(TableName.valueOf(

"people"

));

HColumnDescriptor hcdInfo =

new

HColumnDescriptor(

"info"

);

HColumnDescriptor hcdData =

new

HColumnDescriptor(

"data"

);

hcdInfo.setMaxVersions(

3

);

htd.addFamily(hcdInfo);

htd.addFamily(hcdData);

admin.createTable(htd);

//用完关闭

admin.close();

}

/**插入数据

* @throws IOException **/

@Test

public

void

testPut()

throws

IOException {

Table table = con.getTable(TableName.valueOf(

"people"

));

Put put =

new

Put(Bytes.toBytes(

"row001"

));

put.addColumn(Bytes.toBytes(

"info"

), Bytes.toBytes(

"name"

), Bytes.toBytes(

"lingxin"

));

put.addColumn(Bytes.toBytes(

"info"

), Bytes.toBytes(

"age"

), Bytes.toBytes(

"57"

));

put.addColumn(Bytes.toBytes(

"info"

), Bytes.toBytes(

"money"

), Bytes.toBytes(

30000

));

table.put(put);

table.close();

}

/**测试插入100万条数据**/

@Test

public

void

testPutAll()

throws

IOException {

Table table = con.getTable(TableName.valueOf(

"people"

));

List puts =

new

ArrayList(

10000

);

for

(

int

i =

1

; i <

1000000

; i++) {

Put put =

new

Put(Bytes.toBytes(

"row"

+ i));

put.addColumn(Bytes.toBytes(

"info"

), Bytes.toBytes(

"name"

), Bytes.toBytes(

"lingxin"

+ i));

put.addColumn(Bytes.toBytes(

"info"

), Bytes.toBytes(

"age"

), Bytes.toBytes(

"57"

));

put.addColumn(Bytes.toBytes(

"info"

), Bytes.toBytes(

"money"

), Bytes.toBytes(

30000

));

puts.add(put);

//每隔10000,写一次

if

(i %

10000

==

0

) {

table.put(puts);

puts =

new

ArrayList(

10000

);

}

}

table.put(puts);

table.close();

//以下方式不可取

/*      for(int i=1; i<1000000; i++) {

Put put = new Put(Bytes.toBytes("row"+i));

put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lingxin"+i));

put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("57"));

put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(30000));

puts.add(put);

}

table.put(puts);

table.close();*/

}

/**查看某个cell的值**/

@Test

public

void

testGet()

throws

IOException {

Table table = con.getTable(TableName.valueOf(

"people"

));

Get get =

new

Get(Bytes.toBytes(

"row9999"

));

Result resut = table.get(get);

String

r = Bytes.toString(resut.getValue(Bytes.toBytes(

"info"

), Bytes.toBytes(

"name"

)));

System.out.println(r);

table.close();

}

/**查看某个rowkey范围的数据,按字典顺序排序**/

@Test

public

void

testScan()

throws

IOException {

Table table = con.getTable(TableName.valueOf(

"people"

));

Scan scan =

new

Scan(Bytes.toBytes(

"row010000"

), Bytes.toBytes(

"row110"

));

ResultScanner scanner = table.getScanner(scan);

for

(Result result : scanner) {

String

r = Bytes.toString(result.getValue(Bytes.toBytes(

"info"

), Bytes.toBytes(

"name"

)));

System.out.println(r);

}

table.close();

}

@Test

public

void

testDel()

throws

IOException {

Table table = con.getTable(TableName.valueOf(

"people"

));

Delete delete =

new

Delete(Bytes.toBytes(

"row9999"

));         table.delete(delete);         table.close();     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值