java实现hbase表创建、数据插入、删除表

本文详细介绍了使用Java实现HBase的基本操作,包括创建表、添加数据、显示所有数据以及删除表的过程。通过配置相关jar包及代码实例,展示了如何在HBase中进行高效的数据管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


近日查看了相关资料后,梳理了一下用java实现hbase的表创建、数据插入、删除表,代码如下:

1、需要的jar包:

commons-codec-1.4.jar

commons-logging-1.0.4.jar

hadoop-0.20.2-core.jar

hbase-0.20.6.jar

log4j-1.2.15.jar

zookeeper-3.2.2.jar

2、代码:

[java:nogutter] view plain copy
  1. packageorg.myhbase;
  2. importjava.io.IOException;
  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.KeyValue;
  8. importorg.apache.hadoop.hbase.client.HBaseAdmin;
  9. importorg.apache.hadoop.hbase.client.HTable;
  10. importorg.apache.hadoop.hbase.client.Result;
  11. importorg.apache.hadoop.hbase.client.ResultScanner;
  12. importorg.apache.hadoop.hbase.client.Scan;
  13. importorg.apache.hadoop.hbase.io.BatchUpdate;
  14. publicclassHBaseBasic02{
  15. staticHBaseConfigurationhbaseConfig=null;
  16. static{
  17. Configurationconfig=newConfiguration();
  18. config.set("hbase.zookeeper.quorum","192.168.10.149,192.168.10.44,192.168.10.49");
  19. config.set("hbase.zookeeper.property.clientPort","2181");
  20. hbaseConfig=newHBaseConfiguration(config);
  21. }
  22. /**
  23. *创建一张表
  24. *@throwsIOException
  25. */
  26. publicstaticvoidcreateTable(Stringtablename)throwsIOException{
  27. HBaseAdminadmin=newHBaseAdmin(hbaseConfig);
  28. if(admin.tableExists(tablename)){
  29. System.out.println("tableExists!!!");
  30. }else{
  31. HTableDescriptortableDesc=newHTableDescriptor(tablename);
  32. tableDesc.addFamily(newHColumnDescriptor("name:"));
  33. admin.createTable(tableDesc);
  34. System.out.println("createtableok.");
  35. }
  36. }
  37. /**
  38. *删除一张表
  39. *@throwsIOException
  40. */
  41. publicstaticvoiddropTable(Stringtablename)throwsIOException{
  42. HBaseAdminadmin=newHBaseAdmin(hbaseConfig);
  43. admin.disableTable(tablename);
  44. admin.deleteTable(tablename);
  45. System.out.println("droptableok.");
  46. }
  47. /**
  48. *添加一条数据
  49. *@throwsIOException
  50. */
  51. publicstaticvoidaddData(Stringtablename)throwsIOException{
  52. HTabletable=newHTable(hbaseConfig,tablename);
  53. BatchUpdateupdate=newBatchUpdate("Huangyi");
  54. update.put("name:java","http://www.sun.com".getBytes());
  55. table.commit(update);
  56. System.out.println("adddataok.");
  57. }
  58. /**
  59. *显示所有数据
  60. *@throwsIOException
  61. */
  62. publicstaticvoidgetAllData(Stringtablename)throwsIOException{
  63. HTabletable=newHTable(hbaseConfig,tablename);
  64. Scans=newScan();
  65. ResultScannerrs=table.getScanner(s);
  66. for(Resultr:rs){
  67. for(KeyValuekv:r.raw()){
  68. System.out.println("rowkey:"+newString(kv.getRow()));
  69. System.out.println(newString(kv.getColumn())+"="+newString(kv.getValue()));
  70. }
  71. }
  72. }
  73. publicstaticvoidmain(String[]args)throwsIOException{
  74. Stringtablename="table_1";
  75. HBaseBasic02.createTable(tablename);
  76. HBaseBasic02.addData(tablename);
  77. HBaseBasic02.getAllData(tablename);
  78. HBaseBasic02.dropTable(tablename);
  79. }
  80. }

HBase 是一个基于列族(Column Family)的分布式NoSQL数据库,主要用于大规模数据存储。在 Java 中使用 HBase 插入数据通常涉及以下几个步骤: 1. **初始化 HBase 客户端**:首先需要通过 `HBaseConfiguration` 初始化配置,并创建一个 `HBaseAdmin` 对象以进行格管理,以及一个 `Table` 对象用于实际的数据操作。 ```java HBaseConfiguration config = HBaseConfiguration.create(); config.addResource(new Path("path_to_your_hbase_config")); // 配置文件路径 HBaseAdmin admin = new HBaseAdmin(config); Table table = admin.open("your_table_name"); ``` 2. **构建 Row Key**:Row Key 是 HBase 格中的主键,需要保证唯一性。你可以根据业务需求生成唯一的字符串作为行键。 3. **准备 Put 对象**:使用 `Put` 对象来封装将要插入数据,包括 Row Key 和 Column Family/Qualifier/Value 信息。 ```java Put put = new Put(Bytes.toBytes("row_key")); put.addColumn(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), Bytes.toBytes("value")); ``` 4. **执行插入**:调用 `table.put(put)` 来向指定插入数据。 5. **提交事务**(可选):如果你在一个批量操作中插入多条数据,可以使用 `table.batch()` 来批量提交。最后记得关闭 `table` 和 `admin`。 ```java try (AutoCloseable ignored = table.batch()) { table.put(puts); // puts 是一个 List<Put> } ``` 6. **关闭连接**:完成操作后别忘了关闭资源。 ```java admin.close(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值