当前位置: 首页 > wzjs >正文

重庆平台网站推广全国疫情最新

重庆平台网站推广,全国疫情最新,六安公共招聘网,自学摄影教程的网站有哪些MapReduce入门案例-分词统计 文章目录 MapReduce入门案例-分词统计1.xml依赖2.编写MapReduce处理逻辑3.上传统计文件到HDFS3.配置MapReduce作业并测试4.执行结果 1.xml依赖 <dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-commo…

MapReduce入门案例-分词统计

文章目录

    • MapReduce入门案例-分词统计
        • 1.xml依赖
        • 2.编写MapReduce处理逻辑
        • 3.上传统计文件到HDFS
        • 3.配置MapReduce作业并测试
        • 4.执行结果

1.xml依赖
     <dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.1.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>3.1.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.1.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-mapreduce-client-core</artifactId><version>3.1.3</version></dependency>
2.编写MapReduce处理逻辑
/*** MapReduce示例-单词统计*/
public class WordCount {/*** Mapper:将输入数据拆分为键值对(Key-Value)。*/public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable> {private final static IntWritable one = new IntWritable(1); // 定义一个常量表示单词计数为1private final Text word = new Text(); // 定义用于存储单词的Text对象public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {// 将每行文本拆分成单词StringTokenizer itr = new StringTokenizer(value.toString(), "\n");while (itr.hasMoreTokens()) {// 按空格分割字符串String[] str = itr.nextToken().split(" ");if(ObjectUtils.isNotEmpty(str)){Map<String, Long> listMap = Lists.newArrayList(str).stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));listMap.forEach((k,v)->{//单词word.set(k);// 第二个元素是单词出现的次数one.set(v.intValue());try {context.write(word, one); // 输出键值对(单词,1)} catch (IOException | InterruptedException e) {throw new RuntimeException(e);}});}}}}/*** Reducer:对相同Key的Value进行聚合处理。* 对相同键(即相同的单词)的值进行汇总,计算出每个单词出现的总次数。*/public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {private final IntWritable result = new IntWritable(); // 定义用于存储汇总结果的IntWritable对象public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int sum = 0; // 初始化计数器for (IntWritable val : values) { // 遍历所有值sum += val.get(); // 累加单词出现的次数}result.set(sum); // 将汇总结果设置为输出值context.write(key, result); // 输出最终的键值对(单词,总次数)}}}
3.上传统计文件到HDFS

文件内容

在这里插入图片描述

文件上传

    /*** 文件上传*/@Testpublic void test04() throws Exception {File file = new File("D:\\IdeaProjects\\springboot\\springboot-hadoop\\src\\main\\resources\\templates\\demo.txt");MultipartFile cMultiFile = new MockMultipartFile("file", file.getName(), null, Files.newInputStream(file.toPath()));hdfsService.uploadFile(cMultiFile,"/demo.txt");}
3.配置MapReduce作业并测试
   /*** 分词统计测试*/@Testpublic void test06() throws Exception {Configuration conf = new Configuration(); // 创建配置对象conf.set("fs.defaultFS", "hdfs://hadoop001:9000");conf.set("dfs.permissions.enabled", "false"); // 可选:关闭权限检查FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop001:9000"),conf, "moshangshang");Job job = Job.getInstance(conf, "word count"); // 创建MapReduce作业实例job.setJarByClass(WordCount.class); // 设置作业的主类job.setMapperClass(WordCount.TokenizerMapper.class); // 设置Mapper类job.setReducerClass(WordCount.IntSumReducer.class); // 设置Reducer类job.setOutputKeyClass(Text.class); // 设置输出键的类型job.setOutputValueClass(IntWritable.class); // 设置输出值的类型FileInputFormat.addInputPath(job, new Path(hdfsProperties.getUploadPath()+"/demo.txt")); // 设置输入路径(hdfs路径)String resultPath = hdfsProperties.getUploadPath()+System.currentTimeMillis();FileOutputFormat.setOutputPath(job, new Path(resultPath)); // 设置输出路径boolean flag = job.waitForCompletion(true);// 等待作业完成并返回结果状态if(flag){System.out.println("执行成功");//查询hdfs结果listFileContent2(fileSystem, new Path(resultPath));//删除执行的统计文件//hdfsService.deleteFile(resultPath);}}/*** 查看hdfs数据(本地执行,就类似本地cmd命令行并没有直接连接远程hadoop)* hdfs dfs -cat /test/1749717998628/**/private static void listFileContent(FileSystem fs, Path filePath) throws IOException {System.out.println("hdfs://hadoop001:9000"+filePath.toString()+"/*");// 使用 hdfs dfs -cat 命令来查看文件内容ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", "hadoop", "fs", "-cat", filePath +"/*");processBuilder.redirectErrorStream(true);Process process = processBuilder.start();InputStream inputStream = process.getInputStream();Scanner scanner = new Scanner(inputStream);while (scanner.hasNextLine()) {System.out.println("执行后的结果:"+scanner.nextLine());}scanner.close();}/*** 使用hadoop集群执行查看路径下所有文件内容(类似cat)*/private static void listFileContent2(FileSystem fs, Path filePath) throws IOException {try {FileStatus[] files = fs.listStatus(filePath);for (FileStatus file : files) {if (file.isDirectory()){continue;}try (FSDataInputStream inputStream = fs.open(file.getPath())) {BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String line;if(reader.read()== -1){continue;}System.out.println("=== 文件: " + filePath.getName() + " ===");while ((line = reader.readLine()) != null) {System.out.println(line);}reader.close();}}} finally {if (fs != null) fs.close();}}
4.执行结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

http://www.dtcms.com/wzjs/169438.html

相关文章:

  • 做户外灯批发什么b2b网站好免费个人网站注册
  • 用软件做的网站权限网站维护推广的方案
  • 哪里做外贸网站网站管理和维护的主要工作有哪些
  • 西宁哪家公司做网站百度精准引流推广
  • 公司建设网站费用重庆seo网站推广优化
  • 网站公司的未来网站seo外包
  • 企业公司网站制作代做网页设计平台
  • 重庆南川网站制作公司电话制作网站平台
  • 网站建设怎么弄怎么做一个自己的网页
  • 网页开发和网站开发站长统计幸福宝2022年排行榜
  • 无锡网站制作哪家便宜手机网站优化排名
  • ps做图游戏下载网站有哪些西安网站seo技术厂家
  • 自己做的网站找不到了搜索引擎官网
  • 网站建设服务器租用守游网络推广平台
  • b2c电子商务网站建设费用p2p万能搜索引擎
  • wordpress 36氪主题手机一键优化
  • 做项目搭建网站 构建数据库品牌策划书案例
  • 北京建设网华樾领尚规划图东莞外贸优化公司
  • 跨境电商erp软件排名百度系优化
  • 钉钉网站建设服务协议汨罗网站seo
  • 网站建设 海口百中搜优化软件
  • 东莞大朗网站设计seo关键词优化方法
  • 织梦响应式网站模板seo搜索引擎优化题库
  • 做特产网站的原因全网推广平台推荐
  • 最新免费网站源码资源网站seo入门黑帽培训教程
  • 自己做网站怎么做网络推广公司有多少家
  • 多图片网站优化东莞seo技术
  • 成都建设网站的公司百度竞价排名又叫
  • 优秀flash网站设计太原seo网站排名
  • 怎么做网站的内链外链网站排名掉了怎么恢复