0


java连接hbase

现在有的新版本hbase在网上搜的java连接hbase代码用不了或者是idea上已经不推荐用(划横线的),我也搜了好多乱糟糟的,经过本人理解再经过整理,代码如下:

另外提示:连接配置中IP也可以写虚拟机的主机名,只需要改下映射即可。(C:\Windows\System32\drivers\etc)更改hosts文件(可能需要权限或更改不了,可以将此文件拉到桌面更改保存了再拉回去),在hosts文件下添加 IP hostname(中间有空格!!!)

另外我整理的以下代码hbase1-3版本应该都适用!!!(本人亲测过hbase-1.7.1,hbase-2.4.13,hbase-3.0.0-alpha-3)

pom.xml文件中添加

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.hbase</groupId>
  4. <artifactId>hbase-client</artifactId>
  5. <version>2.4.13</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.hbase</groupId>
  9. <artifactId>hbase</artifactId>
  10. <version>2.4.13</version>
  11. <type>pom</type>
  12. </dependency>
  13. </dependencies>
  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.hbase.*;
  3. import org.apache.hadoop.hbase.client.*;
  4. import org.apache.hadoop.hbase.filter.RowFilter;
  5. import org.apache.hadoop.hbase.filter.SubstringComparator;
  6. import org.apache.hadoop.hbase.util.Bytes;
  7. import java.io.IOException;
  8. import java.util.*;
  9. public class zsgcHbase {
  10. public static void main(String[] args) throws IOException {
  11. // tj();
  12. // cx();
  13. // versioncx();
  14. // sc();
  15. // jb();
  16. // fwcx();
  17. // strRow();
  18. // filterRow();
  19. valueCol();
  20. }
  1. //连接配置
  2. public static Connection pz() throws IOException {
  3. //获取配置
  4. Configuration conf = HBaseConfiguration.create();
  5. //指定HBase使用的zookeeper地址,多个用逗号隔开
  6. conf.set("hbase.zookeeper.quorum", "172.20.10.250:2181,172.20.10.251:2181,172.20.10.252:2181");
  7. //创建HBase连接,负责对HBase中数据的一些增删改查操作
  8. Connection conn = ConnectionFactory.createConnection(conf);
  9. return conn;
  10. }
  1. //添加数据和更新数据
  2. public static void tj() throws IOException {
  3. Connection conn = pz();
  4. //获取Table对象,指定要操作的表名(这里需要提前创建好)
  5. Table table = conn.getTable(TableName.valueOf("test"));
  6. //指定Rowkey,返回put对象 这里进行批量插入数据再声明一个Put对象再添加一次put对象(也可以声明一个List<Put>集合添加list对象)
  7. Put put = new Put(Bytes.toBytes("001"));
  8. //向put对象中指定列族、列、值
  9. put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("zhujuntao"));
  10. put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("man"));
  11. put.addColumn(Bytes.toBytes("level"),Bytes.toBytes("course"),Bytes.toBytes("qq"));
  12. //向表中添加数据
  13. table.put(put);
  14. //关闭table连接
  15. table.close();
  16. }
  1. //查询根据rowkey查询数据
  2. public static void cx() throws IOException {
  3. Connection conn = pz();
  4. Table table = conn.getTable(TableName.valueOf("test"));
  5. //指定Rowkey,返回Get对象
  6. Get get = new Get(Bytes.toBytes("001"));
  7. //可以在这里指定要查询指定Rowkey数据哪些列族中的列
  8. // 如果不指定,默认查询指定Rowkey所有列的内容
  9. //get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
  10. //get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"));
  11. Result result = table.get(get);
  12. //可以使用listCells()获取所有cell,cell对应的是某一个列的数据
  13. List<Cell> cells = result.listCells();
  14. for (Cell cell: cells) {
  15. //这里获取的信息都是字节类型的,可以通过new String(bytes)转为字符串
  16. //列族
  17. byte[] famaily_bytes = CellUtil.cloneFamily(cell);
  18. //列
  19. byte[] column_bytes = CellUtil.cloneQualifier(cell);
  20. //值
  21. byte[] value_bytes = CellUtil.cloneValue(cell);
  22. System.out.println("列族:"+new String(famaily_bytes)+",列:"+new String(column_bytes)+",值:"+new String(value_bytes));
  23. }
  24. //这里还可以直接使用getValue(family, qualifier)直接获取指定列族中指定列的数据
  25. //byte[] age_bytes = result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"));
  26. table.close();
  27. }
  1. //查询列值历史版本
  2. public static void versioncx() throws IOException {
  3. Connection conn = pz();
  4. Table table = conn.getTable(TableName.valueOf("test"));
  5. Get get = new Get(Bytes.toBytes("001"));
  6. //这里读取cell中的所有历史版本数据
  7. //可以通过get.readVersions(2)来指定获取多少个历史版本的数据
  8. get.readAllVersions();
  9. Result result = table.get(get);
  10. //获取指定列族中指定列的所有历史版本数据,必须设置get.readAllVersions()或者get.readVersions(2),否则只会获取最新数据
  11. List<Cell> columnCells = result.getColumnCells(Bytes.toBytes("info"), Bytes.toBytes("name"));
  12. for (Cell cell :columnCells) {
  13. byte[] value_bytes = CellUtil.cloneValue(cell);
  14. long timestamp = cell.getTimestamp();
  15. System.out.println("值为:"+new String(value_bytes)+",时间戳:"+timestamp);
  16. }
  17. table.close();
  18. }
  1. //根据rowkey删除数据
  2. public static void sc() throws IOException {
  3. Connection conn = pz();
  4. Table table = conn.getTable(TableName.valueOf("ideatest"));
  5. //指定Rowkey,返回Delete对象
  6. Delete delete = new Delete(Bytes.toBytes("laowang"));
  7. //这里还可以指定要删除指定Rowkey数据哪些列族中的列
  8. //delete.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
  9. table.delete(delete);
  10. table.close();
  11. }
  1. //创建表
  2. public static void jb() throws IOException {
  3. Connection conn = pz();
  4. //获取管理权限,负责对HBase中的表进行操作
  5. Admin admin = conn.getAdmin();
  6. //指定列族信息
  7. ColumnFamilyDescriptor familyDesc1 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"))
  8. //给列族设置属性
  9. .setMaxVersions(3)//这个是最多存储多少个历史版本数据
  10. .build();
  11. ColumnFamilyDescriptor familyDesc2 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("level"))
  12. .setMaxVersions(2)
  13. .build();
  14. ArrayList<ColumnFamilyDescriptor> familyList = new ArrayList<ColumnFamilyDescriptor>();
  15. familyList.add(familyDesc1);
  16. familyList.add(familyDesc2);
  17. //获取TableDescriptor对象
  18. TableDescriptor desc = TableDescriptorBuilder.newBuilder(TableName.valueOf("test"))//指定表名
  19. .setColumnFamilies(familyList)//指定列族
  20. .build();
  21. //创建表
  22. admin.createTable(desc);
  23. }
  24. //删除表,先禁用表
  25. //admin.disableTable(TableName.valueOf("test"));
  26. //admin.deleteTable(TableName.valueOf("test"));
  1. //Limit查询前5行
  2. public static void fwcx() throws IOException {
  3. Connection conn = pz();
  4. Table tableRead = conn.getTable(TableName.valueOf("ideatest"));
  5. //获取Scan对象
  6. Scan scan = new Scan();
  7. //同startrow
  8. //scan.withStartRow(Bytes.toBytes("000"));
  9. //limit
  10. scan.setLimit(5);
  11. //接收scan对象
  12. ResultScanner scanner = tableRead.getScanner(scan);
  13. //这里我试了很久,一开始使用上面查询的遍历查询方法结果只能出来第一条后来经过思考就写成以下的方法就行了
  14. for (Result rs : scanner) {
  15. System.out.println("行键:"+new String(rs.getRow()));
  16. Get get = new Get(rs.getRow());
  17. Result result = tableRead.get(get);
  18. List<Cell> cells = result.listCells();
  19. for (Cell cell: cells) {
  20. byte[] famaily_bytes = CellUtil.cloneFamily(cell);
  21. byte[] column_bytes = CellUtil.cloneQualifier(cell);
  22. byte[] value_bytes = CellUtil.cloneValue(cell);
  23. System.out.println("列族:"+new String(famaily_bytes)+",列:"+new String(column_bytes)+",值:"+new String(value_bytes));
  24. }
  25. }
  26. scanner.close();
  27. tableRead.close();
  28. }
  1. //分页查询,其实跟上边limit一样(上面有提示)
  2. public static void strRow() throws IOException {
  3. Connection conn = pz();
  4. Table tableRead = conn.getTable(TableName.valueOf("ideatest"));
  5. Scan scan = new Scan();
  6. scan.withStartRow(Bytes.toBytes("002"),false); //这里false表示不包含002,默认为true
  7. scan.withStopRow(Bytes.toBytes("003"),true);
  8. ResultScanner scanner = tableRead.getScanner(scan);
  9. for (Result rs : scanner) {
  10. System.out.println("行键:"+new String(rs.getRow()));
  11. Get get = new Get(rs.getRow());
  12. Result result = tableRead.get(get);
  13. List<Cell> cells = result.listCells();
  14. for (Cell cell: cells) {
  15. byte[] famaily_bytes = CellUtil.cloneFamily(cell);
  16. byte[] column_bytes = CellUtil.cloneQualifier(cell);
  17. byte[] value_bytes = CellUtil.cloneValue(cell);
  18. System.out.println("列族:"+new String(famaily_bytes)+",列:"+new String(column_bytes)+",值:"+new String(value_bytes));
  19. }
  20. }
  21. scanner.close();
  22. tableRead.close();
  23. }
  1. //查询rowkey中包含特定前缀的数据
  2. public static void filterRow() throws IOException {
  3. Connection conn = pz();
  4. Table tableRead = conn.getTable(TableName.valueOf("ideatest"));
  5. Scan scan = new Scan();
  6. //CompareOperator(EQUAL等于,CREATER大于,CREATE_OR_EQUAL大于或等于,LESS小于,LESS_OR_EQUAL大于或等于,NO_OP误操作,NOT_EQUAL不等于)
  7. RowFilter filter = new RowFilter(CompareOperator.EQUAL,new SubstringComparator("1")); //CompareFilter.CompareOp.EQUAL已被弃用,改用CompareOperator.EQUAL
  8. /*
  9. FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); 用于存储多个条件
  10. FilterList.Operator.MUST_PASS_ALL --> 取交集 相当一and操作
  11. FilterList.Operator.MUST_PASS_ONE --> 取并集 相当于or 操作
  12. */
  13. scan.setFilter(filter);
  14. ResultScanner scanner = tableRead.getScanner(scan);
  15. for (Result rs : scanner) {
  16. System.out.println("行键:"+new String(rs.getRow()));
  17. Get get = new Get(rs.getRow());
  18. Result result = tableRead.get(get);
  19. List<Cell> cells = result.listCells();
  20. for (Cell cell: cells) {
  21. byte[] famaily_bytes = CellUtil.cloneFamily(cell);
  22. byte[] column_bytes = CellUtil.cloneQualifier(cell);
  23. byte[] value_bytes = CellUtil.cloneValue(cell);
  24. System.out.println("列族:"+new String(famaily_bytes)+",列:"+new String(column_bytes)+",值:"+new String(value_bytes));
  25. }
  26. }
  27. scanner.close();
  28. tableRead.close();
  29. }
  1. //查询行键前缀为0的行 PrefixFilter
  2. public static void valueCol() throws IOException {
  3. Connection conn = pz();
  4. Table tableRead = conn.getTable(TableName.valueOf("ideatest"));
  5. Scan scan = new Scan();
  6. scan.setRowPrefixFilter(Bytes.toBytes("0"));
  7. ResultScanner scanner = tableRead.getScanner(scan);
  8. for (Result rs : scanner) {
  9. System.out.println("行键:"+new String(rs.getRow()));
  10. Get get = new Get(rs.getRow());
  11. Result result = tableRead.get(get);
  12. List<Cell> cells = result.listCells();
  13. for (Cell cell: cells) {
  14. byte[] famaily_bytes = CellUtil.cloneFamily(cell);
  15. byte[] column_bytes = CellUtil.cloneQualifier(cell);
  16. byte[] value_bytes = CellUtil.cloneValue(cell);
  17. System.out.println("列族:"+new String(famaily_bytes)+",列:"+new String(column_bytes)+",值:"+new String(value_bytes));
  18. }
  19. }
  20. scanner.close();
  21. tableRead.close();
  22. }

点赞关注,持续更新

标签: java 开发语言 hbase

本文转载自: https://blog.csdn.net/m0_73288917/article/details/126394525
版权归原作者 超* 所有, 如有侵权,请联系我们删除。

“java连接hbase”的评论:

还没有评论