java cdh hbase_Hbase的基本操作(CDH组件可用)

importorg.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.util.Bytes;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importjava.io.IOException;importjava.text.DecimalFormat;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;/*** @Author:Xavier

* @Data:2019-02-20 15:48

**/

public classOptionTest {private Admin admin = null;private Connection connection = null;private Configuration conf = null;private TableName tname = null;

@Beforepublic void init() throwsIOException {

conf=HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "nn01,nn02,dn01,dn02,dn03,dn04");

conf.set("hbase.zookeeper.property.clientPort", "2181");

conf.set("hbase.master", "dn01:60000");

connection=ConnectionFactory.createConnection(conf);

admin=connection.getAdmin();

}//创建一张表,指定表名,列族

@Testpublic void createTabl() throwsIOException {try{

admin.createNamespace(NamespaceDescriptor.create("MyNamespace").build());

}catch(NamespaceExistException e) {

System.out.println("该命名空间已经存在");

}//创建tablename对象,描述表的名称信息

tname = TableName.valueOf("MyNamespace:students");//创建HTableDescriptor对象,描述表信息

HTableDescriptor tDescriptor = newHTableDescriptor(tname);if(admin.tableExists(tname)) {

System.out.println("students" + "存在!");

System.exit(0);

}else{

HColumnDescriptor famliy= new HColumnDescriptor("core");

tDescriptor.addFamily(famliy);

admin.createTable(tDescriptor);

System.out.println("创建表成功!");

}

}//获取所有的表

@Testpublic voidgetAllTable() {if (admin != null) {try{

HTableDescriptor[] alltable=admin.listTables();for(HTableDescriptor hTableDesc : alltable) {

System.out.println(hTableDesc.getNameAsString());

}

}catch(IOException e) {

e.printStackTrace();

}

}

}//删除表

@Testpublic void deleteTab() throwsIOException {

tname= TableName.valueOf("MyNamespace:students");if(admin.tableExists(tname)) {

admin.disableTable(tname);//先禁用表才能删除

admin.deleteTable(tname);

System.out.println("删除表成功!");

}else{

System.out.println("表不存在");

}

}//插入一条数据

@Testpublic void addOneRecord() throwsIOException {//通过连接查询tableName对象

tname = TableName.valueOf("MyNamespace:students");if(admin.tableExists(tname)) {

Table table=connection.getTable(tname);

Put put= new Put(Bytes.toBytes("lisi"));

put.add(Bytes.toBytes("core"), Bytes.toBytes("math"), Bytes.toBytes("98"));

table.put(put);

System.out.println("插入数据成功!");

}else{

System.out.println("插入数据失败");

}

}//插入多条数据

@Testpublic void moreInsert() throwsIOException {//测试在数据前补零

DecimalFormat format = newDecimalFormat();

format.applyPattern("0000");

tname= TableName.valueOf("MyNamespace:students");

HTable htable=(HTable) connection.getTable(tname);//不要自动清理缓冲区

htable.setAutoFlush(false);//一个put代表一行数据,再new一个put表示第二行数据,每行一个唯一的RowKey

for (int i = 1; i < 10000; i++) {

Put put= new Put(Bytes.toBytes("leilei" +format.format(i)));//关闭写前日志

put.setWriteToWAL(false);

put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("math"), Bytes.toBytes(format.format(i)));

put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("english"), Bytes.toBytes(format.format(Math.random() *i)));

put.addColumn(Bytes.toBytes("core"), Bytes.toBytes("chinese"), Bytes.toBytes(format.format(Math.random() *i)));

htable.put(put);if (i % 2000 == 0) {

htable.flushCommits();

}

}

htable.flushCommits();

htable.close();

}//通过RowKey,faimly,colum获取cell数据

@Testpublic void getData() throwsIOException {

tname= TableName.valueOf("MyNamespace:students");

Table table=connection.getTable(tname);//通过RowKey

Get get = new Get(Bytes.toBytes("lisi"));

Result result=table.get(get);

System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("core"), Bytes.toBytes("math"))));

}//扫描在rowkey范围内的cell数据

@Testpublic void deleteRangeRK() throwsIOException {

tname= TableName.valueOf("MyNamespace:students");

Table table=connection.getTable(tname);

Scan scan= newScan();

scan.setStartRow(Bytes.toBytes("leilei1000"));

scan.setStopRow(Bytes.toBytes("leilei9999"));

ResultScanner resultScanner=table.getScanner(scan);

Iterator iterator =resultScanner.iterator();while(iterator.hasNext()) {

System.out.println(Bytes.toString((iterator.next()).getValue(Bytes.toBytes("core"), Bytes.toBytes("english"))));//System.out.println((iterator.next()).toString());

}

}//KeyValue形式查询一行的数据

@Testpublic void getValueFromKey() throwsIOException {

tname= TableName.valueOf("MyNamespace:students");

Table table=connection.getTable(tname);

Get get= new Get(Bytes.toBytes("lisi"));

Result result=table.get(get);if (result.raw().length == 0) {

System.out.println("不存在该关键字的行!!");

}else{for(Cell kv : result.rawCells()) {

System.out.println("列:"+Bytes.toString(CellUtil.cloneFamily(kv))+":"+Bytes.toString(CellUtil.cloneQualifier(kv))+"\t 值:"+Bytes.toString(CellUtil.cloneValue(kv)));

}

}

}//KeyValue形式查询所有的数据

@Testpublic void getAllData() throwsException {

tname= TableName.valueOf("MyNamespace:students");

Table table=connection.getTable(tname);

Scan scan= newScan();

ResultScanner rs=table.getScanner(scan);for(Result r : rs) {for(KeyValue kv : r.raw()) {

System.out.println(Bytes.toString(kv.getKey())+Bytes.toString(kv.getValue()));

}

}

}//删除一行Hbase表中记录信息的

@Testpublic void deleteRecord() throwsIOException {

tname= TableName.valueOf("MyNamespace:students");

Table table=connection.getTable(tname);

Delete de= new Delete(Bytes.toBytes("leilei9997"));try{

table.delete(de);

System.out.println("删除记录成功!!!");

}catch(IOException e) {

System.out.println("删除记录异常!!!");

}

}

@Afterpublic void destory() throwsIOException {

admin.close();

connection.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值