【头歌】HBase扫描与过滤答案 排除复制粘贴限制

瑞星  金牌会员 | 2024-11-3 06:47:04 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 862|帖子 862|积分 2586

 排除复制粘贴限制

当作者遇到这个限制的时间火气起来了三分,然后去网上搜索答案,然后发现了一位【碳烤小肥肠】居然不贴代码,XX链接,贴截图,瞬时火气冲顶,怒写此文


起首启动全能的控制台,然后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. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

瑞星

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表