hbase java api 查询_Java API 实现HBase的数据添加与过滤查询

包依赖比较麻烦,找了好久,我用的CDH5.0 现将所依赖的包的列表清单如下:

07b3fd03c8fb22862dc89c69c5f410ec.png

public class EmployeeDao {

/**

* @param args

*/

public static Configuration configuration;

static {

configuration = new Configuration();

String filePath = "hbase-site.xml";

Path path = new Path(filePath);

configuration.addResource(path);

configuration = HBaseConfiguration.create(configuration);

}

//使用过滤器实现查询

public List QueryEmployeeByName(String name) throws Exception {

String rowKey = name;

HTable table = new HTable(configuration, "employee".getBytes());

Scan scan = new Scan();

// select ..... from .. where filter

scan.addColumn("info".getBytes(), "age".getBytes());

scan.addColumn("info".getBytes(), "sex".getBytes());

RowFilter filter = new RowFilter(CompareOp.EQUAL,

new RegexStringComparator(rowKey));

scan.setFilter(filter);

List list = new ArrayList();

ResultScanner rScanner = table.getScanner(scan);

for (Result rs : rScanner) {

Employee e = new Employee();

for (KeyValue kValue : rs.list()) {

if ("sex".equalsIgnoreCase(new String(kValue.getQualifier()))) {

e.setSex(new String(kValue.getValue()));

}

if ("age".equalsIgnoreCase(new String(kValue.getQualifier()))) {

e.setAge(Integer.parseInt((new String(kValue.getValue()))));

}

}

list.add(e);

}

return list;

}

// 插入一个单元格数据

public static void insertOneRow(String tableName, String rowkey,

String columnFamily, String column, String value) throws Exception {

HTable table = new HTable(configuration, tableName);

Put put = new Put(Bytes.toBytes(rowkey));

put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),

Bytes.toBytes(value));

table.put(put);// 放入表

table.close();// 释放资源

}

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

// TODO Auto-generated method stub

EmployeeDao dao = new EmployeeDao();

List l = dao.QueryEmployeeByName("oftenlin");

for (Employee e : l) {

System.out.println("Name:oftenlin," + "Sex:" + e.getSex() + ",Age:"+ e.getAge());

}

//insertOneRow("employee","allen","info","scholl","shi yan mid school");

insertOneRow("employee","gold fly","info","scholl","shi yan mid school");

System.out.println("写入成功!");

}

}

hbase-site.xml 的清单

hbase.rootdir

hdfs://CentOS-cm:8020/hbase

hbase.client.write.buffer

2097152

hbase.client.pause

100

hbase.client.retries.number

35

hbase.client.scanner.caching

100

hbase.client.keyvalue.maxsize

10485760

hbase.rpc.timeout

60000

hbase.snapshot.enabled

true

hbase.security.authentication

simple

zookeeper.session.timeout

60000

zookeeper.znode.parent

/hbase

zookeeper.znode.rootserver

root-region-server

hbase.zookeeper.quorum

CentOS-server3,CentOS-server2,CentOS-server1

hbase.zookeeper.property.clientPort

2181

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值