package com.test.hbase;
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.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.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;
/**
* 初始化配置信息,zookeeper集群地址
*/
@Before
public void init(){
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "cluster01:2181,cluster02:2181,cluster03:2181");
}
/**
* 创建表
*/
public void testCreate() throws Exception{
HBaseAdmin admin = new HBaseAdmin(conf);
//表描述信息
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("user"));
//指定列族
HColumnDescriptor hcd = new HColumnDescriptor("info");
//版本
hcd.setMaxVersions(3);
htd.addFamily(hcd);
admin.createTable(htd);
admin.close();
}
/**
* 删除表
* @throws Exception
*/
@Test
public void testDrop() throws Exception{
HBaseAdmin admin = new HBaseAdmin(conf);
//先停用,再删除
admin.disableTable("user");
admin.deleteTable("user");
admin.close();
}
/**
* 插入数据
* @throws Exception
*/
@Test
public void testPut() throws Exception{
//得到表对象
HTable table = new HTable(conf, "user");
//row key
Put put = new Put(Bytes.toBytes("rk001"));
//列族---列----值
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(12));
put.add(Bytes.toBytes("info"), Bytes.toBytes("addr"), Bytes.toBytes("beijing"));
table.put(put);
table.close();
}
/**
* 插入多条数据
* @throws Exception
*/
@Test
public void testPutAll() throws Exception{
//得到表对象
HTable table = new HTable(conf, "user");
List<Put> puts = new ArrayList<Put>();
for (int i = 1; i<= 1000; i++){
Put put = new Put(Bytes.toBytes("rk00"+i));
//列族---列----值
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"+i));
puts.add(put);
if(i % 100 == 0){
table.put(puts);
puts = new ArrayList<Put>();
}
}
table.close();
}
/**
* 查询数据
* @throws Exception
*/
@Test
public void testGet() throws Exception{
HTable table = new HTable(conf, "user");
//指定row key
Get get = new Get(Bytes.toBytes("rk0010"));
Result relust = table.get(get);
String r = Bytes.toString(relust.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
System.out.println(r);
table.close();
}
/**
* 范围查询
* @throws Exception
*/
@Test
public void testScan() throws Exception{
HTable table = new HTable(conf, "user");
Scan scan = new Scan(Bytes.toBytes("rk0080"), Bytes.toBytes("rk090"));
ResultScanner resule = table.getScanner(scan);
for (Result result : resule) {
String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
System.out.println(r);
}
table.close();
}
/**
* 删除数据
* @throws Exception
*/
@Test
public void testDel() throws Exception{
HTable table = new HTable(conf, "user");
Delete delete = new Delete(Bytes.toBytes("rk0010"));
table.delete(delete);
table.close();
}
}
4.Hbase基于java接口的简单示例
最新推荐文章于 2021-02-12 09:09:31 发布