当前位置:   article > 正文

头歌 HBase扫描与过滤答案

头歌 HBase扫描与过滤答案

解除复制粘贴限制

各位,都遇到过这样的问题吧,做实验的时候不能复制粘贴就很烦


首先启动万能的控制台,然后Ctrl+Shift+F全局搜索这个弹窗

双击进入源码,随便搞个NB的文件夹,然后点击允许


然后右键把这个事儿多的js文件里面禁止复制粘贴的代码给注释了,然后保存

最后刷新网页,奇迹出现,可以复制粘贴了【切勿关闭控制台】

第1关:使用 Shell 命令创建表

  1. start-hbase.sh
  2. hbase shell
  3. create 'exam_tb1','student_info','course_info'
  4. put 'exam_tb1', 'row-1', 'student_info:name', 'zhangsan'
  5. put 'exam_tb1', 'row-1', 'student_info:s_no', '2020001'
  6. put 'exam_tb1', 'row-2', 'student_info:name', 'lisi'
  7. put 'exam_tb1', 'row-2', 'student_info:s_no', '2020002'
  8. put 'exam_tb1', 'row-1', 'course_info:c_no', '123001'
  9. put 'exam_tb1', 'row-1', 'course_info:c_name', 'HBase'
  10. put 'exam_tb1', 'row-2', 'course_info:c_no', '123002'
  11. put 'exam_tb1', 'row-2', 'course_info:c_name', 'Hadoop'
  12. exit
  13. echo "scan 'exam_tb1'" | hbase shell >/root/student.txt

第2关:使用 Java API 实现增删操作

  1. package com.yy;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.TableName;
  5. import org.apache.hadoop.hbase.client.*;
  6. import org.apache.hadoop.hbase.util.Bytes;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.io.IOException;
  10. public class HbaseUtil {
  11. private static Admin admin = null;
  12. private static Connection connection = null;
  13. private static Configuration conf = null;
  14. static {
  15. // HBase配置文件
  16. conf = HBaseConfiguration.create();
  17. // 获取连接对象
  18. try {
  19. connection = ConnectionFactory.createConnection(conf);
  20. // 获取HBase管理员对象
  21. admin = connection.getAdmin();
  22. } catch (IOException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }
  26. }
  27. private static void close(Connection conn, Admin admin) throws IOException {
  28. if (conn != null) {
  29. conn.close();
  30. }
  31. if (admin != null) {
  32. admin.close();
  33. }
  34. }
  35. //删除exam_tb2
  36. public void deleteTable() throws IOException {
  37. /**********Begin**********/
  38. admin.disableTable(TableName.valueOf("exam_tb2"));
  39. admin.deleteTable(TableName.valueOf("exam_tb2"));
  40. /**********End**********/
  41. }
  42. //创建exam_tb3
  43. public void createTab() throws IOException {
  44. /**********Begin**********/
  45. TableName tb3 = TableName.valueOf("exam_tb3");
  46. TableDescriptorBuilder tbd = TableDescriptorBuilder.newBuilder(tb3);
  47. tbd.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("user_info")).build());
  48. tbd.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("address_info")).build());
  49. admin.createTable(tbd.build());
  50. /**********End**********/
  51. }
  52. //往exam_tb3表中添加数据
  53. public void putBatch() throws IOException {
  54. /**********Begin**********/
  55. Table table = connection.getTable(TableName.valueOf("exam_tb3"));
  56. Put p1 = new Put(Bytes.toBytes("1"));
  57. Put p2 = new Put(Bytes.toBytes("2"));
  58. Put p3 = new Put(Bytes.toBytes("3"));
  59. p1.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("Avatar"));
  60. p1.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("100"));
  61. p1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("pandora"));
  62. p1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("avatar@163.com"));
  63. p1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("123456"));
  64. p2.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("change"));
  65. p2.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("50"));
  66. p2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("moon"));
  67. p2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("change@163.com"));
  68. p2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("234567"));
  69. p3.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("nezha"));
  70. p3.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("6"));
  71. p3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("earth"));
  72. p3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("nezha@163.com"));
  73. p3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("345678"));
  74. ArrayList < Put > puts = new ArrayList < > ();
  75. puts.add(p1);
  76. puts.add(p2);
  77. puts.add(p3);
  78. table.put(puts);
  79. /**********End**********/
  80. }
  81. }

第3关:HBase扫描

  1. package step3;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.*;
  4. import org.apache.hadoop.hbase.*;
  5. import org.apache.hadoop.hbase.client.*;
  6. import org.apache.hadoop.hbase.util.*;
  7. public class Task {
  8. public void scanTable(String tableName) throws Exception {
  9. /********* Begin *********/
  10. Configuration config = HBaseConfiguration.create();
  11. Connection conn = ConnectionFactory.createConnection(config);
  12. Admin admin = conn.getAdmin();
  13. TableName name = TableName.valueOf(tableName);
  14. Table table = conn.getTable(name);
  15. Scan scan = new Scan();
  16. scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
  17. scan.setStartRow(Bytes.toBytes("row-10"));
  18. scan.setStopRow(Bytes.toBytes("row-30"));
  19. ResultScanner scanner = table.getScanner(scan);
  20. for (Result result: scanner) {
  21. for (Cell cell: result.listCells()) {
  22. String family = Bytes.toString(CellUtil.cloneFamily(cell));
  23. String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
  24. String value = Bytes.toString(CellUtil.cloneValue(cell));
  25. System.out.println("Rowkey:" + Bytes.toString(result.getRow()) + ",ColumuFamily:" + family + ",Column:" + qualifier + ",Value:" + value);
  26. /********* End *********/
  27. }
  28. }
  29. }
  30. }

第4关:HBase过滤器

  1. package step4;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.hbase.HBaseConfiguration;
  7. import org.apache.hadoop.hbase.TableName;
  8. import org.apache.hadoop.hbase.client.Connection;
  9. import org.apache.hadoop.hbase.client.ConnectionFactory;
  10. import org.apache.hadoop.hbase.client.Result;
  11. import org.apache.hadoop.hbase.client.ResultScanner;
  12. import org.apache.hadoop.hbase.client.Scan;
  13. import org.apache.hadoop.hbase.client.Table;
  14. import org.apache.hadoop.hbase.util.Bytes;
  15. public class Task {
  16. public void query(String tName) throws Exception {
  17. /********* Begin *********/
  18. Configuration conf = HBaseConfiguration.create();
  19. Connection connection = ConnectionFactory.createConnection(conf);
  20. try {
  21. Table table = connection.getTable(TableName.valueOf(tName));
  22. Scan scan = new Scan();
  23. ResultScanner scanner = table.getScanner(scan);
  24. for (Result result : scanner) {
  25. byte[] row = result.getRow();
  26. String rowKey = Bytes.toString(row);
  27. result.listCells().forEach(cell -> {
  28. String family = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
  29. String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
  30. String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
  31. System.out.println(String.format("Rowkey:%s,ColumuFamily:%s,Column:%s,Value:%s", rowKey, family, qualifier, value));
  32. });
  33. }
  34. scanner.close();
  35. table.close();
  36. } finally {
  37. connection.close();
  38. }
  39. /********* End *********/
  40. }
  41. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/817891
推荐阅读
相关标签
  

闽ICP备14008679号