hbase java api 查询_HBase的JavaAPI操作

该博客介绍了如何使用HBase的Java API进行操作,包括创建表、插入记录、查询单条记录以及遍历所有记录的步骤。通过配置HBase连接参数,创建HBaseAdmin和HTable实例,实现对HBase表的增删查改操作。
摘要由CSDN通过智能技术生成

1 packagehbase;2

3 importorg.apache.hadoop.conf.Configuration;4 importorg.apache.hadoop.hbase.HBaseConfiguration;5 importorg.apache.hadoop.hbase.HColumnDescriptor;6 importorg.apache.hadoop.hbase.HTableDescriptor;7 importorg.apache.hadoop.hbase.client.Get;8 importorg.apache.hadoop.hbase.client.HBaseAdmin;9 importorg.apache.hadoop.hbase.client.HTable;10 importorg.apache.hadoop.hbase.client.Put;11 importorg.apache.hadoop.hbase.client.Result;12 importorg.apache.hadoop.hbase.client.ResultScanner;13 importorg.apache.hadoop.hbase.client.Scan;14

15 /**

16 *要先将HBase相关jar包添加进去!!!17 *18 * 创建表、删除表 (使用HBaseAdmin)19 *20 * 插入记录、查询一条记录、遍历所有记录 (使用HTable)21 *22 *@authorahu_lichang23 *24 */

25 public classHBaseApp {26

27 private static final String TABLE_NAME = "table1";28 private static final String FAMILY_NAME = "family1";29 private static final String ROW_KEY = "rowkey1";30

31 public static void main(String[] args) throwsException {32 Configuration conf =HBaseConfiguration.create();33 /*

34 * hbase操作必备35 */

36 conf.set("hbase.rootdir", "hdfs://hadoop0:9000/hbase");37 //使用eclipse时必须添加这个,否则无法定位

38 conf.set("hbase.zookeeper.quorum", "hadoop0");39 /*

40 *创建表41 */

42 HBaseAdmin hBaseAdmin = newHBaseAdmin(conf);43 /*

44 * if (!hBaseAdmin.tableExists(TABLE_NAME)) { HTableDescriptor45 * hTableDescriptor = new HTableDescriptor(TABLE_NAME);46 * HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(47 * FAMILY_NAME); hTableDescriptor.addFamily(hColumnDescriptor);48 * hBaseAdmin.createTable(hTableDescriptor); }49 */

50

51 /*

52 * 添加一条记录53 */

54 HTable hTable = newHTable(conf, TABLE_NAME);55 /*

56 * Put put = new Put(ROW_KEY.getBytes());57 * put.add(FAMILY_NAME.getBytes(), "age".getBytes(), "25".getBytes());58 * hTable.put(put);59 */

60

61 /*

62 *查询一条记录63 */

64

65 /*

66 * Get get = new Get(ROW_KEY.getBytes()); Result result =67 * hTable.get(get); byte[] value = result68 * .getValue(FAMILY_NAME.getBytes(), "age".getBytes()); //69 * keyvalues={rowkey1/family1:age/1491571143625/Put/vlen=2/ts=0} 2570 * System.out.println(result + "\t" + new String(value));71 */

72

73 /*

74 *遍历所有记录75 */

76 Scan scan = newScan();77 ResultScanner resultScanner =hTable.getScanner(scan);78 for(Result result : resultScanner) {79 byte[] value =result.getValue(FAMILY_NAME.getBytes(),80 "age".getBytes());81 System.out.println(result + "\t" + newString(value));82 }83

84 hTable.close();85 /*

86 * 删除表87 */

88 /*

89 * hBaseAdmin.disableTable(TABLE_NAME);90 * hBaseAdmin.deleteTable(TABLE_NAME);91 */

92 }93

94 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值