java api访问hbase_javaAPI操作HBase

平台:window+linux

工具:IDEA2016

public class HbaseTest {

public static void main(String[] args) throws Exception {

// createTab();

// HTable table = getTable();

// System.out.println(table.getName());

// putData();

getData();

}

//添加文本过滤器

public static void testFilter() {

HTable table = null;

try {

table = getTable();

Scan scan = new Scan();

// create filter

Filter filter = null;

filter = new PrefixFilter(Bytes.toBytes("rk"));

filter = new PageFilter(3);

// create hbase comparator

ByteArrayComparable comp = null;

comp = new SubstringComparator("lisi");

filter = new SingleColumnValueFilter(

Bytes.toBytes("info"),

Bytes.toBytes("name"),

CompareOp.EQUAL,

comp);

// set filter

scan.setFilter(filter);

ResultScanner rs = table.getScanner(scan);

for (Result result : rs) {

printResult(result);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if(table != null) {

IOUtils.closeStream(table);

}

}

}

//HBase扫描数据

public static void scanData() {

HTable table = null;

try {

table = getTable();

// create Scan instance

Scan scan = new Scan();

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

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

scan.addFamily(Bytes.toBytes("info"));

// set cache

scan.setCacheBlocks(false);

scan.setBatch(2);

scan.setCaching(2);

// set permmission

// scan.setACL(perms);

// get scanner results

ResultScanner rs = table.getScanner(scan);

for(Result result : rs) {

printResult(result);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if(table != null) {

IOUtils.closeStream(table);

}

}

}

//一个打印的方法

public static void printResult(Result rs) {

for (Cell cell : rs.rawCells()) {

System.out.println(

Bytes.toString(CellUtil.cloneFamily(cell)) + " : " +

Bytes.toString(CellUtil.cloneRow(cell)) + " : " +

Bytes.toString(CellUtil.cloneQualifier(cell)) + " : " +

Bytes.toString(CellUtil.cloneValue(cell))

);

}

}

//测试HBase的命名空间

public static void testNamespace() throws Exception {

Configuration c = HBaseConfiguration.create();

HBaseAdmin admin = new HBaseAdmin(c );

// create namespace

NamespaceDescriptor namespace = NamespaceDescriptor.create("ns1").build();

// admin.createNamespace(namespace );

admin.deleteNamespace("ns1");

// close resource

admin.close();

}

public static Configuration getConfiguration(){

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.rootdir","hdfs://10.23.12.183:8020/hbase");

conf.set("hbase.zookeeper.quorum","10.23.12.183");

return conf;

}

public static void getData() throws Exception {

HTable table =getTable();

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

Result result = table.get(get);

for (Cell cell: result.rawCells()

) {

System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));

System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));

System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));

System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));

}

}

//往一个表中插入数据

public static void putData() throws Exception {

HTable table = getTable();

//插入一个行健row

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

put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));

table.put(put);

table.close();

}

//从HBase中读出一个Tab所属的表

public static HTable getTable() throws Exception {

HTable table = new HTable(getConfiguration(),Bytes.toBytes("t1"));

return table;

}

//往HBase中写入一个Tab“t1”

public static void createTab() throws Exception {

HBaseAdmin admin = new HBaseAdmin(getConfiguration());

boolean b = admin.tableExists(Bytes.toBytes("t1"));

if(b){

admin.disableTable(Bytes.toBytes("t1"));

admin.deleteTable("t1");

}

HTableDescriptor table = new HTableDescriptor(TableName.valueOf("t1"));

table.addFamily(new HColumnDescriptor((Bytes.toBytes("info"))));

table.addFamily(new HColumnDescriptor((Bytes.toBytes("secret"))));

admin.createTable(table);

admin.close();

}

}

POM.XML为

4.0.0

com.jxky.bigdata

hbase

0.0.1-SNAPSHOT

1.1.12

2.6.0-cdh5.7.0

cloudera

https://repository.cloudera.com/artifactory/cloudera-repos/

org.apache.hbase

hbase-client

${hbase.version}

org.apache.hbase

hbase-server

${hbase.version}

org.apache.hadoop

hadoop-client

${hadoop.version}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值