国科大大数据系统与大规模数据分析第一次作业(hash distinct)

一、作业内容

  1. 从HDFS中读出数据

  2. 对读出的数据进行hash去重

  3. 将处理好的数据存入Hbase

二、作业代码

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;import org.apache.log4j.*;
/*** @ClassName Hw1Grp4* @Description: hw1 code* @Author: cgg* @CreateDate: 2022/3/21 11:22* @UpdateUser: cgg* @UpdateDate: 2022/3/25 10:22* @UpdateRemark: info* @Version: 1.1*/
@SuppressWarnings("all")
public class Hw1Grp4 {public static ArrayList<String> TestData_List = null;//raw datapublic static ArrayList<String> DataAfterSelect = null;//Data After Selectpublic static String FilePath = null;  //File pathpublic static int SelectRow = 0;//Columns to selectpublic static String SelectCondition = null; //Select Conditionpublic static String SelectValue = null;//Condition valuepublic static ArrayList<Integer> DistinctRow = null;//Columns to be redonepublic static LinkedHashMap<Integer, String> linkedHashMap = null; //Columns to be redone/*** @auther: cgg* @Description //TODO     Read the data according to the parameters passed in the main function* @param: [args]  parameters passed in the main function* @return: void* @date: 2022/4/1 14:09*/public static void Read_Data(String[] args) throws IOException {//Read DataTestData_List = new ArrayList<String>();if (args.length <= 0) {System.out.println("Usage: HDFSTest <hdfs-file-path>");System.exit(1);}String file = FilePath;Configuration conf = new Configuration();FileSystem fs = FileSystem.get(URI.create(file), conf);Path path = new Path(file);FSDataInputStream in_stream = fs.open(path);BufferedReader in = new BufferedReader(new InputStreamReader(in_stream));String s;while ((s=in.readLine())!=null) {TestData_List.add(s);}in.close();fs.close();}/*** @auther: cgg* @Description //TODO  Initialize to get the file name, selected column,selection condition and column to be de duplicated* @param: [args]   parameters passed in the main function* @return: void* @date: 2022/4/1 14:17*/public static void init(String[] args) {String arg0 = args[0];String arg1 = args[1];String arg2 = args[2];//regular expression  arg0String regStr = "^R=(.*)$";Pattern pattern = Pattern.compile(regStr);Matcher matcher = pattern.matcher(arg0);while (matcher.find()) {FilePath = matcher.group(1);break;}//regular expression  arg1regStr = "^select:(.*)$";pattern = Pattern.compile(regStr);matcher = pattern.matcher(arg1);while (matcher.find()) {arg1 = matcher.group(1);break;}String[] temp = arg1.split(",");SelectRow = Integer.parseInt(temp[0].substring(temp[0].length() - 1));SelectCondition = temp[1];SelectValue = temp[2];//regular expression  arg2regStr = "^distinct:(.*)$";pattern = Pattern.compile(regStr);matcher = pattern.matcher(arg2);while (matcher.find()) {arg2 = matcher.group(1);break;}temp = arg2.split(",");DistinctRow = new ArrayList<Integer>();for (int i = 0; i < temp.length; i++) {DistinctRow.add(Integer.parseInt(temp[i].substring(temp[i].length() - 1)));}}/*** @auther: cgg* @Description //TODO   Robustness test, if the input parameters are irregular, exit the program* @param: []* @return: void* @date: 2022/4/1 14:21*/public static void Robustness() {if (FilePath == null || FilePath == "") System.exit(1);if (SelectRow < 0 || SelectRow >= TestData_List.get(0).split("\\|").length) System.exit(1);if (!SelectCondition.equals("gt") && !SelectCondition.equals("ge") && !SelectCondition.equals("eq")&& !SelectCondition.equals("ne") && !SelectCondition.equals("le") && !SelectCondition.equals("lt")) {System.exit(1);}for (int i = 0; i < DistinctRow.size(); i++) {if (DistinctRow.get(i) < 0 || DistinctRow.get(i) >= TestData_List.get(0).split("\\|").length) {System.exit(1);}}}/*** @auther: cgg* @Description //TODO     Judge whether the data in this column meets the filtering conditions* @param: [content, condition, value]  content:Content value to compare; condition:Conditions to compare; value: Comparison value* @return: boolean* @date: 2022/4/1 14:22*/public static boolean IsSelectData(String content, String condition, String value) {switch (condition) {case "gt"://>return Double.parseDouble(content) > Double.parseDouble(value);case "ge"://>=return Double.parseDouble(content) >= Double.parseDouble(value);case "eq"://==return content.equals(value);case "ne"://!=return !content.equals(value);case "le"://<=return Double.parseDouble(content) <= Double.parseDouble(value);case "lt"://<return Double.parseDouble(content) < Double.parseDouble(value);default:System.out.println("Illegal instruction!!");break;}return false;}/*** @auther: cgg* @Description //TODO    //Select:The filtered data is put into a list* @param: []* @return: void* @date: 2022/4/1 14:25*/public static void Select() {DataAfterSelect = new ArrayList<String>();for (Object data : TestData_List) {String[] temp = ((String) data).split("\\|");if (IsSelectData(temp[SelectRow], SelectCondition, SelectValue)) {String TempDataStr = "";for (int i = 0; i < DistinctRow.size(); i++) {if (i == 0) TempDataStr += temp[(int) DistinctRow.get(i)];else TempDataStr += "|" + temp[(int) DistinctRow.get(i)];}DataAfterSelect.add(TempDataStr);}}}/*** @auther: cgg* @Description //TODO     //De duplication of filtered data* @param: []* @return: void* @date: 2022/4/1 14:26*/public static void Distinct() {linkedHashMap = new LinkedHashMap<Integer, String>();for (Object o : DataAfterSelect) {String data = (String) o;linkedHashMap.put(data.hashCode(), data);}}/*** @auther: cgg* @Description //TODO  //get anser print* @param: []* @return: void* @date: 2022/4/1 14:27*/public static void GetAnser() {int cnt = 0;Iterator it = linkedHashMap.entrySet().iterator();while (it.hasNext()) {Map.Entry entry = (Map.Entry) it.next();String[] temp = ((String) entry.getValue()).split("\\|");for (int i = 0; i < temp.length; i++) {System.out.print("(row key=" + cnt + ",res:R" + DistinctRow.get(i) + "=" + temp[i] + ")");}System.out.println();cnt++;}}/*** @auther: cgg* @Description //TODO   //write in Hbase* @param: []* @return: void* @date: 2022/4/1 14:29*/public static void WritertoHbase() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{Logger.getRootLogger().setLevel(Level.WARN);// create table descriptorString tableName= "Result";HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));// create column descriptorHColumnDescriptor cf = new HColumnDescriptor("res");htd.addFamily(cf);// configure HBaseConfiguration configuration = HBaseConfiguration.create();HBaseAdmin hAdmin = new HBaseAdmin(configuration);if (hAdmin.tableExists(tableName)) {hAdmin.deleteTable(tableName);hAdmin.createTable(htd);System.out.println("Table already exists");}else {hAdmin.createTable(htd);System.out.println("table "+tableName+ " created successfully");}hAdmin.close();HTable table = new HTable(configuration,tableName);//Traversal result writingint cnt=0;Iterator it = linkedHashMap.entrySet().iterator();while (it.hasNext()) {Map.Entry entry = (Map.Entry) it.next();Put put;String[] temp = ((String) entry.getValue()).split("\\|");for (int i = 0; i < temp.length; i++) {put  = new Put(("row key="+cnt).getBytes());put.add("res".getBytes(),("R" + DistinctRow.get(i)).getBytes(),temp[i].getBytes());table.put(put);}cnt++;}table.close();System.out.println("put successfully");}public static void main(String[] args) throws IOException {init(args);Read_Data(args);Robustness();Select();Distinct();GetAnser();System.out.println("start write");WritertoHbase();System.out.println("end write");}
}

三、总结

1. 使用LinkedHashMap能够去重且保证顺序,大大减少去重编程的难度
2. 要熟练掌握在HDFS上存取数据操作,要注意Hbase的存储格式
3. 要学会使用正则表达式进行文本字符的处理

国科大大数据系统与大规模数据分析课程第一次作业(hash distinct)相关推荐

  1. 国科大学习资料--人工智能原理与算法-第一次作业解析(学长整理)

    国科大学习资料–人工智能原理与算法-第一次作业解析(张文生老师主讲)(1.3.1.7.1.9.1.14.1.15) 1.3 反射行动(比如从热炉子上缩回你的手)是理性的吗?它们是智能的吗? 答:反射行 ...

  2. 大数据系统与大规模数据分析--第一次作业操作,HDFS、HBase编程

    第一次作业操作 大家好,我是[豆干花生],这次我带来了大数据的第一次实践作业~ 可以说十分具体了,包含了具体操作.代码指令.各个步骤截图. 文章目录 第一次作业操作 一.作业内容 二.准备工作 三.编 ...

  3. 北航操作系统课程-第一次作业-操作系统引论1

    北航操作系统课程-第一次作业-操作系统引论1 北京航空航天大学计算机学院-2020春操作系统课程 题目作者为北航计算机学院操作系统课程组,答案为博主原创.水平有限,无法保证作答正确性,如有错误敬请批评 ...

  4. 软件工程课程第一次作业

    软件工程第一次作业 作业属于哪个课程 软件工程 作业要求在哪里 作业要求链接 作业的目标 1. 熟悉markdown语法 2. 阅读<构建之法> 3. 撰写csdn博客 4. 评估当前,展 ...

  5. 软件工程课程第一次作业-3120005403

    目录 作业要求 一.评估当前的自己 1.1自我简介 1.2当前值 二.展望未来 2.1阅读<构建之法>,并阅读[构建之法社区](https://bbs.csdn.net/forums/So ...

  6. 国科大算法分析陈玉福老师——第三章作业

    Exercise1 and 2 归并排序及时间复杂性: #include<iostream> #include<stdlib.h> #include<time.h> ...

  7. [开源软件开发导论课程——第一次作业]有关开源软件开发的5个问题

    这个作业的要求是: https://bbs.csdn.net/topics/607938212 1. 一个软件越贴近于底层就越有开源的潜力,因为越贴近底层基础设施往往就越通用 文章链接:https:/ ...

  8. 高级软件工程课程第一次作业的小结

    成果: 1)各位同学按要求建立了自己博客,完善并反馈的相关信息 2)对各自本科设计进行了分析,有的同学也写得比较详细,如曾秀.李文华.吴文兵等 存在的问题: 1)对软件工程的认识较基础.本次课程的学生 ...

  9. CS224N 2019年课程第一次作业复现

    本次作业主要介绍 余弦相似性 两种求词向量的方法 基于计数(词共现矩阵 + SVD) 基于预测(word2vec) 完整代码:CS 224N | Home 一.环境及数据问题 1.gensim安装 p ...

最新文章

  1. 这个男人让你的爬虫开发效率提升8倍
  2. html怎么让表格竖排,表格里的文字怎么竖排
  3. iptables配置详解
  4. STL学习笔记(仿函数)
  5. 基于spring自动注入及AOP的表单二次提交验证
  6. graphics | 基础绘图系统(五)——plot函数功能再探和低级绘图函数
  7. JSON应用场景与实战
  8. 转载:用图片搜索图片的几个好网站
  9. 用数字计算机公式表白,数学计算题表白公式
  10. 黑马JAVA P136 斗地主游戏
  11. 《Metasploit魔鬼训练营》环境搭建与前两章经历体会
  12. 联拓生物任命钱江担任中国区总经理
  13. 转载:选一个适合自己的加密芯片,加密IC,如何才能真正的做到不被破解。
  14. Java基础入门必须知道的英语词汇
  15. 计算属性与监听器、Vue 实例的生命周期
  16. ExcelVBA 之可选参数
  17. 根据WebService地址展示方法列表、入参和返回值(解析wsdl)
  18. 激活Windows 7旗舰版
  19. android imageview.setVisibility(View.VISIBLE)设置无效,代码已执行但是图片不显示
  20. 齐鲁师范学院的计算机专业在哪个校区,齐鲁师范学院有几个校区及校区地址 哪个校区最好_大学生创业计划书...

热门文章

  1. 基于Materials Studio的异质结构建模技巧
  2. Hadoop三大组件之分布式文件操作系统HDFS实现原理及编程
  3. 关于Jupyter Notebook No module named ‘xxx’ 问题解决
  4. 一些网络上搜集的网页特效
  5. 那些年我们电脑上的软件 都用过的绝对是大神
  6. 使用mongoDB初体验
  7. 关于数组的对象获取及排序问题/小程序的多层页面返回问题
  8. 副本技能-记录一次OOM的异常处理
  9. java Lambda表达式List快速转Map
  10. Slicer学习笔记(三十六)slicer坐标系