package test.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.checkerframework.checker.units.qual.C;
import org.checkerframework.checker.units.qual.K;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TestHbase {
// 获取HBase 链接
Connection connection = null;
//获取管理权限
Admin admin = null;
TableName tableName = TableName.valueOf("student");
/**
* 建立链接
*/
@Before
public void getHBaseConnection() {
Configuration conf = HBaseConfiguration.create();
//连接zk的信息,如果windows中没有ip与主机名的映射,那么在此写ip地址。
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
try {
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建命名空间
*
* @throws IOException
*/
@Test
public void createNamespace() throws IOException {
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("user").build();
try {
admin.getNamespaceDescriptor("user");
} catch (NamespaceNotFoundException e) {
admin.createNamespace(namespaceDescriptor);
System.out.println("创建命名空间成功");
}
}
/**
* 删除命名空间
*
* @throws IOException
*/
@Test
public void deleteNamespace() throws IOException {
try {
admin.deleteNamespace("user");
} catch (IOException e) {
System.out.println("删除命名空间失败");
}
}
/**
* 判断表是否存在 不存在创建表
*
* @throws IOException
*/
@Test
public void tableExists() throws IOException {
TableName tableName = TableName.valueOf("user");
boolean b = admin.tableExists(tableName);
if (b) {
System.out.println(" User TableExists");
}
}
/**
* 创建表
*
* @throws IOException
*/
@Test
public void createTable() throws IOException {
TableName tableName = TableName.valueOf("user");
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("user")).build();
TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(family).build();
admin.createTable(desc);
}
/**
* 删除表 删除前必须禁用表,不然抛出异常
* org.apache.hadoop.hbase.TableNotDisabledException:
* org.apache.hadoop.hbase.TableNotDisabledException:
* Not DISABLED; tableName=user, state=ENABLED
*
* @throws IOException
*/
@Test
public void deleteTable() throws IOException {
TableName tableName = TableName.valueOf("user");
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
@Test
public void putDataToTable() throws IOException {
TableName tableName = TableName.valueOf("user");
Table table = connection.getTable(tableName);
String rowkey = "10008"; //行键
String family = "test"; //列族
String qualifier = "age"; //列
Integer value = 20; //值
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
}
/**
* 插入百万数据
*
* @throws IOException
*/
@Test
public void putDatasToTable() throws IOException {
Table table = connection.getTable(TableName.valueOf("user"));
// 用来存储数据的列表
List<Put> puts = new ArrayList<>();
// 开始时间
long start = System.currentTimeMillis();
System.out.println("开始时间:"+start);
for (int i = 0; i <= 10000; i++) {
Put put = new Put((""+i).getBytes());
put.addColumn("user".getBytes(), "value".getBytes(), ("" + i).getBytes());
// 把需要提交数据放入集合中
puts.add(put);
if (i % 10 == 0) {
table.put(puts);
puts = new ArrayList<>();
}
}
// 获取结束时间
long end = System.currentTimeMillis();
System.out.println("结束时间:"+end);
// 共用时长
System.out.println("总耗时:" + (end - start));
}
/**
* 获取单行数据
* @throws IOException
*/
@Test
public void getRow() throws IOException {
Table table = connection.getTable(tableName);
//行键
String rowkey = "10001";
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.print("行键:"+Bytes.toString(CellUtil.cloneRow(cell))+"\t");
System.out.print("列族:"+Bytes.toString(CellUtil.cloneFamily(cell))+"\t");
System.out.print("列:"+Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");
System.out.println("值:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
/**
* Scan 扫描全表
* @throws IOException
*/
@Test
public void ScanTable() throws IOException {
Table table = connection.getTable(TableName.valueOf("user"));
Scan scan = new Scan();
scan.setCaching(1000); //设置缓存 可以不加
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()){
Result next = iterator.next();
for (Cell cell : next.rawCells()) {
System.out.print("行键:"+Bytes.toString(CellUtil.cloneRow(cell))+"\t");
System.out.print("列族:"+Bytes.toString(CellUtil.cloneFamily(cell))+"\t");
System.out.print("列:"+Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");
System.out.println("值:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
/**
* Scan 扫描全表 控制startrow,stopRow 注意包括startrow 不包括stoprow,
* @throws IOException
*/
@Test
public void ScanStartrowToStopRowTable() throws IOException {
Table table = connection.getTable(tableName);
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes("10002"));
scan.withStopRow(Bytes.toBytes("10006"));
scan.setCaching(1000); //设置缓存 可以不加
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()){
Result next = iterator.next();
for (Cell cell : next.rawCells()) {
System.out.print("行键:"+Bytes.toString(CellUtil.cloneRow(cell))+"\t");
System.out.print("列族:"+Bytes.toString(CellUtil.cloneFamily(cell))+"\t");
System.out.print("列:"+Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");
System.out.println("值:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
/**
* Scan 扫描全表 filterList对查询过滤
* @throws IOException
*/
@Test
public void ScanFilterTable() throws IOException {
Table table = connection.getTable(tableName);
Scan scan = new Scan();
Filter filter=new PageFilter(5);
scan.setFilter(filter);
scan.setCaching(1000); //设置缓存 可以不加
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()){
Result next = iterator.next();
for (Cell cell : next.rawCells()) {
System.out.print("行键:"+Bytes.toString(CellUtil.cloneRow(cell))+"\t");
System.out.print("列族:"+Bytes.toString(CellUtil.cloneFamily(cell))+"\t");
System.out.print("列:"+Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");
System.out.println("值:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
/**
* 删除某行的数据
* @throws IOException
*/
@Test
public void deleteRow() throws IOException {
Table user = connection.getTable(TableName.valueOf("user"));
String rowkey="9";
Delete delete = new Delete(Bytes.toBytes(rowkey));
user.delete(delete);
}
@After
public void close() throws IOException {
connection.close();
admin.close();
}
}
Hbase JavaAPI操作 Hbase
最新推荐文章于 2022-06-10 21:28:00 发布