别再死记硬背了!我用Hadoop HDFS和HBase Shell命令搞定期末大作业(附完整代码)
大数据期末实战从零构建HadoopHBase学生成绩分析系统1. 环境搭建10分钟搞定伪分布式集群每次打开虚拟机都要重新配置环境用Docker容器化方案可以彻底解决这个问题。以下是基于Docker Compose的一键式部署方案version: 3 services: hadoop: image: sequenceiq/hadoop-docker:2.7.1 ports: - 50070:50070 # HDFS Web UI - 8088:8088 # YARN Web UI volumes: - ./data:/data environment: - HADOOP_HEAPSIZE512 hbase: image: harisekhon/hbase:1.4 depends_on: - hadoop ports: - 16010:16010 # HBase Web UI environment: - HBASE_HEAPSIZE512启动命令docker-compose up -d常见问题排查表问题现象可能原因解决方案50070端口无法访问防火墙未开放sudo ufw allow 50070HBase无法连接HDFS网络配置错误检查docker网络是否互通内存不足导致崩溃默认配置过低调整HADOOP_HEAPSIZE参数提示Windows用户建议使用WSL2Docker Desktop方案避免传统虚拟机性能损耗2. HDFS实战高效处理作业数据文件假设我们需要合并三个班级的成绩单文件class1.txt, class2.txt, class3.txt并过滤出及格记录# 创建专用目录 hdfs dfs -mkdir -p /user/student/scores # 上传本地文件到HDFS hdfs dfs -put class*.txt /user/student/scores # 合并并过滤文件Java API实现 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; public class ScoreFilter { public static void main(String[] args) throws Exception { Configuration conf new Configuration(); FileSystem fs FileSystem.get(conf); // 获取目录下所有.txt文件 FileStatus[] files fs.globStatus(new Path(/user/student/scores/*.txt)); // 创建输出流 FSDataOutputStream output fs.create(new Path(/user/student/merged_scores.txt)); for (FileStatus file : files) { FSDataInputStream input fs.open(file.getPath()); BufferedReader reader new BufferedReader(new InputStreamReader(input)); String line; while ((line reader.readLine()) ! null) { String[] parts line.split(\t); if (Integer.parseInt(parts[1]) 60) { // 成绩≥60 output.writeBytes(line \n); } } reader.close(); } output.close(); } }HDFS常用命令效率对比操作单机处理HDFS处理优势1GB文件上传45秒12秒并行分块10个文件合并手动操作单命令完成原子性操作数据过滤全量加载逐块处理内存友好3. HBase实战构建学生成绩数据库用Shell命令创建学生成绩表并插入样例数据# 创建包含两个列族的表 create student_scores, {NAME basic_info, VERSIONS 1}, {NAME course, VERSIONS 3} # 插入数据多版本示例 put student_scores, 2023001, basic_info:name, 张三 put student_scores, 2023001, course:math, 89 put student_scores, 2023001, course:math, 92 # 成绩修改 put student_scores, 2023001, course:english, 85Java API查询示例 - 获取数学成绩历史版本public class ScoreQuery { public static void main(String[] args) throws IOException { Configuration config HBaseConfiguration.create(); Connection connection ConnectionFactory.createConnection(config); Table table connection.getTable(TableName.valueOf(student_scores)); Get get new Get(Bytes.toBytes(2023001)); get.addColumn(Bytes.toBytes(course), Bytes.toBytes(math)); get.setMaxVersions(3); // 获取所有版本 Result result table.get(get); NavigableMapbyte[], NavigableMapbyte[], NavigableMapLong, byte[] map result.getMap(); for (Cell cell : result.columnCells(course.getBytes(), math.getBytes())) { System.out.println(版本: cell.getTimestamp() 值: Bytes.toString(CellUtil.cloneValue(cell))); } } }输出结果版本:1645587200000 值:92 版本:1645587100000 值:894. 数据分析MapReduce统计课程平均分实现各门课程的平均分计算public class AverageScore { public static class Map extends MapperObject, Text, Text, IntWritable { private Text course new Text(); private IntWritable score new IntWritable(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String[] fields value.toString().split(\t); course.set(fields[0]); // 课程名 score.set(Integer.parseInt(fields[1])); // 分数 context.write(course, score); } } public static class Reduce extends ReducerText, IntWritable, Text, DoubleWritable { public void reduce(Text key, IterableIntWritable values, Context context) throws IOException, InterruptedException { double sum 0; int count 0; for (IntWritable val : values) { sum val.get(); count; } context.write(key, new DoubleWritable(sum/count)); } } }优化技巧使用Combiner减少网络传输调整mapreduce.task.io.sort.mb提升排序效率对于小文件场景启用CombineTextInputFormat5. 避坑指南我踩过的那些坑时间戳问题HBase默认使用系统时间戳批量导入时建议显式指定时间戳避免版本混乱Region分裂预分区可以避免热点问题创建表时指定create large_table, {NAME cf}, {SPLITS [a, m, z]}HDFS权限开发环境可以临时关闭权限检查property namedfs.permissions.enabled/name valuefalse/value /property内存配置伪分布式环境注意调整各服务内存上限避免OOM数据本地性计算节点尽量与数据节点保持一致可通过以下命令检查hdfs fsck /path/to/file -files -blocks -locations这套方案在我校大数据课程设计中获得满分所有代码均通过测试验证。建议先在小数据集跑通流程再扩展到完整数据集。遇到问题时多看日志文件/var/log/hadoop/能快速定位原因。