HBase快速入门系列(6) | Hbase简单的API操作
大家好,我是不温卜火,是一名计算机学院大数据专业大二的学生,昵称来源于成语—
不温不火
,本意是希望自己性情温和
。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己所犯的错误希望能够帮助到很多和自己一样处于起步阶段的萌新。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只有csdn这一个平台,博客主页:https://buwenbuhuo.blog.csdn.net/
此篇为大家带来的是Hbase简单的API操作。
标注:
此处为反爬虫标记:读者可自行忽略
- 1
- 2
原文地址:https://buwenbuhuo.blog.csdn.net/
1. 添加依赖
<dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.8</version> <scope>system</scope> <systemPath>D:/java/jdk-1.8.0/lib/tools.jar</systemPath> </dependency> </dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
2. HBaseAPI
- 1. 获取Configuration对象
/**
* @author 卜温不火
* @create 2020-05-12 10:48
*/ private static Connection connection = null; static{ try { Configuration conf = HBaseConfiguration.create(); //使用HBaseConfiguration的单例方法实例化 conf.set("hbase.zookeeper.quorum", "hadoop002,hadoop003,hadoop004"); conf.set("hbase.zookeeper.property.clientPort", "2181"); connection= ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 2. 判断表是否存在
// 判断表是否存在 public static boolean tableExists(String tableName) throws IOException{ Admin admin = connection.getAdmin(); try { return admin.tableExists(TableName.valueOf(tableName)); }finally { admin.close(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 3. 创建表
// 创建ddl public static void creatTable(String tableName,String... families)throws IOException { Admin admin = connection.getAdmin(); try { if (admin.tableExists(TableName.valueOf(tableName))){ System.out.println("表:"+tableName+" 已经存在"); return; } HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName)); for (String family : families){ HColumnDescriptor familyDesc = new HColumnDescriptor(family); desc.addFamily(familyDesc); } admin.createTable(desc); }finally { admin.close(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 4. 删除表
// 删除表 public static void dropTable(String tableName) throws IOException{ Admin admin = connection.getAdmin(); try{ if(!admin.tableExists(TableName.valueOf(tableName))){ System.out.println("表" + tableName + "删除成功!"); }else{ System.out.println("表" + tableName + "不存在!"); } admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); }finally { admin.close(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 5. 向表中插入数据
// 往表中插入数据 public static void putCell(String tableName,String rowKey,String family,String column,String value) throws IOException { if(!tableExists(tableName)){ return; } Table table = connection.getTable(TableName.valueOf(tableName)); try{ Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value)); table.put(put); }finally { table.close(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 6. 删除数据
①删除某一行
// 1. 删除某一行 public static void deleteRow(String tableName,String rowKey) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); table.close(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
②删除整个列组
// 2. 删除整个列组 public static void deleteFamily(String tableName,String rowKey,String family) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addFamily(Bytes.toBytes(family)); table.delete(delete); table.close(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
③删除某一列
// 删除所有版本
public static void deleteCell(String tableName,String rowKey,String family,String column) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(column)); table.delete(delete); table.close(); } // 测试 public static void main(String[] args)throws IOException{ putCell("student","1003","info", "name","buwen"); putCell("student","1003","info", "name","bubuhuo"); deleteCell("student","1003","info", "name"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
我们可以看到删除了最新的一条
// 删除最新版本
public static void deleteCells(String tableName,String rowKey,String family,String column) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumns(Bytes.toBytes(family),Bytes.toBytes(column)); table.delete(delete); table.close(); } // 测试 public static void main(String[] args)throws IOException{ putCell("student","1003","info", "name","buwen"); putCell("student","1003","info", "name","bubuhuo"); deleteCells("student","1003","info", "name"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 7. 查看一行数据
// 查找一行数据 public static void getRow(String tableName,String rowKey) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); Cell[] cells = result.rawCells(); for (Cell cell : cells){ byte[] columnBytes = CellUtil.cloneQualifier(cell); String columnStr = Bytes.toString(columnBytes); byte[] valueBytes = CellUtil.cloneValue(cell); String valueStr = Bytes.toString(valueBytes); System.out.println(columnStr + ":" + valueStr); } table.close(); } public static void main(String[] args)throws IOException{ putCell("student","1003","info", "age","18"); putCell("student","1003","info", "gender","boy");
// deleteCells("student","1003","info", "name"); getRow("student","1003"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 8. 查看指定范围内数据
public static void getRows(String tableName,String startRow,String stopRow) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { for (Cell cell : result.rawCells()) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); String column = Bytes.toString(CellUtil.cloneQualifier(cell)); String value = Bytes.toString(CellUtil.cloneValue(cell)); System.out.println(rowKey + "-" + column + ":" + value); } } scanner.close(); table.close(); } public static void main(String[] args)throws IOException{ getRows("student","1002","1003!"); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 9. 过滤(单个)
public static void getRowsByColumn(String tableName,String family,String column, String value) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter(Bytes.toBytes(family), Bytes.toBytes(column), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(value));
filter.setFilterIfMissing(true); // 过滤器 scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { for (Cell cell : result.rawCells()) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); String columnStr = Bytes.toString(CellUtil.cloneQualifier(cell)); String valueStr = Bytes.toString(CellUtil.cloneValue(cell)); System.out.println(rowKey + "-" + columnStr + ":" + valueStr); } } scanner.close(); table.close(); } public static void main(String[] args) throws IOException { getRowsByColumn("student","info","age","18"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 10. 过滤(多个)
//过滤(多个) public static void getRowsByColumns(String tableName, String family, Map<String,String> map) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); SingleColumnValueExcludeFilter filter1 = new SingleColumnValueExcludeFilter(Bytes.toBytes(family), Bytes.toBytes(map.keySet().toArray()[0].toString()), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(map.get(map.keySet().toArray()[0].toString()))); filter1.setFilterIfMissing(true); SingleColumnValueExcludeFilter filter2 = new SingleColumnValueExcludeFilter(Bytes.toBytes(family), Bytes.toBytes(map.keySet().toArray()[1].toString()), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(map.get(map.keySet().toArray()[1].toString()))); filter2.setFilterIfMissing(true); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); filterList.addFilter(filter1); filterList.addFilter(filter2); scan.setFilter(filterList); // da yin ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { for (Cell cell : result.rawCells()) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); String columnStr = Bytes.toString(CellUtil.cloneQualifier(cell)); String valueStr = Bytes.toString(CellUtil.cloneValue(cell)); System.out.println(rowKey + "-" + columnStr + ":" + valueStr); } } scanner.close(); table.close(); } public static void main(String[] args) throws IOException {
// putCell("student","1005","info", "name","lixuefang");
// putCell("student","1005","info", "gender","girl");
// putCell("student","1005","info", "age","18"); HashMap<String,String>map = new HashMap<String, String>(); map.put("age","18"); map.put("gender","girl"); getRowsByColumns("student","info",map); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
本次的分享就到这里了,
好书不厌读百回,熟读课思子自知。而我想要成为全场最靓的仔,就必须坚持通过学习来获取更多知识,用知识改变命运,用博客见证成长,用行动证明我在努力。
如果我的博客对你有帮助、如果你喜欢我的博客内容,请“点赞” “评论”“收藏”
一键三连哦!听说点赞的人运气不会太差,每一天都会元气满满呦!如果实在要白嫖的话,那祝你开心每一天,欢迎常来我博客看看。
码字不易,大家的支持就是我坚持下去的动力。点赞后不要忘了关注
我哦!
文章来源: buwenbuhuo.blog.csdn.net,作者:不温卜火,版权归原作者所有,如需转载,请联系作者。
原文链接:buwenbuhuo.blog.csdn.net/article/details/106069000
- 点赞
- 收藏
- 关注作者
评论(0)