一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - Java執行hadoop的基本操作實例代碼

Java執行hadoop的基本操作實例代碼

2020-09-17 15:46Java之家 JAVA教程

這篇文章主要介紹了Java執行hadoop的基本操作實例代碼的相關資料,需要的朋友可以參考下

Java執行hadoop的基本操作實例代碼

向HDFS上傳本地文件

 
?
1
 
2
3
4
5
6
7
8
9
public static void uploadInputFile(String localFile) throws IOException{
    Configuration conf = new Configuration();
    String hdfsPath = "hdfs://localhost:9000/";
    String hdfsInput = "hdfs://localhost:9000/user/hadoop/input";
    FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
    fs.copyFromLocalFile(new Path(localFile), new Path(hdfsInput));
    fs.close();
    System.out.println("已經上傳文件到input文件夾啦");
  }

將output文件下載到本地

 
?
1
 
2
3
4
5
6
7
8
9
10
public static void getOutput(String outputfile) throws IOException{
    String remoteFile = "hdfs://localhost:9000/user/hadoop/output/part-r-00000";
    Path path = new Path(remoteFile);
    Configuration conf = new Configuration();
    String hdfsPath = "hdfs://localhost:9000/";
    FileSystem fs = FileSystem.get(URI.create(hdfsPath),conf);
    fs.copyToLocalFile(path, new Path(outputfile));
    System.out.println("已經將輸出文件保留到本地文件");
    fs.close();
  }

刪除hdfs中的文件

 
?
1
 
2
3
4
5
6
7
8
9
10
public static void deleteOutput() throws IOException{
   Configuration conf = new Configuration();
   String hdfsOutput = "hdfs://localhost:9000/user/hadoop/output";
   String hdfsPath = "hdfs://localhost:9000/";
   Path path = new Path(hdfsOutput);
   FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
   fs.deleteOnExit(path);
   fs.close();
   System.out.println("output文件已經刪除");
 }

執行mapReduce程序

創建Mapper類和Reducer類

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{
 
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
 
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
      String line = value.toString();
      line = line.replace("\\", "");
      String regex = "性別:</span><span class=\"pt_detail\">(.*?)</span>";
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(line);
      while(matcher.find()){
        String term = matcher.group(1);
        word.set(term);
        context.write(word, one);
      }
    }
  }
 
  public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
 
    private IntWritable result = new 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);
    }
  }

執行mapReduce程序

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void runMapReduce(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if(otherArgs.length != 2){
      System.err.println("Usage: wordcount<in> <out>");
      System.exit(2);
    }
    Job job = new Job(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.out.println("mapReduce 執行完畢!");
    System.exit(job.waitForCompletion(true)?0:1);
 
  }

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/qq_30843221/article/details/54429792

延伸 · 閱讀

精彩推薦
  • JAVA教程Jdbc的步驟以及簡單實現代碼

    Jdbc的步驟以及簡單實現代碼

    下面小編就為大家帶來一篇Jdbc的步驟以及簡單實現代碼。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧 ...

    java教程網1922020-05-31
  • JAVA教程spring mvc實現登錄賬號單瀏覽器登錄

    spring mvc實現登錄賬號單瀏覽器登錄

    這篇文章主要為大家詳細介紹了spring mvc實現登錄賬號單瀏覽器登錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Mr_Smile20142812020-09-16
  • JAVA教程SpringMVC對日期類型的轉換示例

    SpringMVC對日期類型的轉換示例

    本篇文章主要介紹了SpringMVC對日期類型的轉換示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...

    ngulc3642020-08-11
  • JAVA教程理解java多線程中ExecutorService使用

    理解java多線程中ExecutorService使用

    這篇文章主要幫助大家理解java多線程中ExcetorServiced的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    stefan.king4852020-07-09
  • JAVA教程Java單例模式實例簡述

    Java單例模式實例簡述

    這篇文章主要介紹了Java單例模式,在Java應用程序設計中有著非常重要的作用,本文以實例形式對此加以簡單分析,需要的朋友可以參考下 ...

    shichen20145262019-11-29
  • JAVA教程Java編程中的條件判斷之if語句的用法詳解

    Java編程中的條件判斷之if語句的用法詳解

    這篇文章主要介紹了Java編程中的條件判斷之if語句的用法詳解,是Java入門學習中的基礎知識,需要的朋友可以參考下 ...

    goldensun1922020-01-16
  • JAVA教程Java并發控制機制詳解

    Java并發控制機制詳解

    這篇文章主要為大家詳細介紹了Java并發控制機制,什么是Java并發控制機制,Java并發控制機制的作用,感興趣的小伙伴們可以參考一下 ...

    ririlu4112020-06-08
  • JAVA教程JAVA基礎之基本數據類型全面解析

    JAVA基礎之基本數據類型全面解析

    下面小編就為大家帶來一篇JAVA基礎之基本數據類型全面解析。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧 ...

    java教程網1302020-05-23
主站蜘蛛池模板: bb18lv黑料正能量 | 门卫老张和女警花小说 | 三体动漫在线观看免费完整版2022 | 成人在线视频在线观看 | 欧美白人猛性xxxxx69交 | 国产一级黄毛片 | a级毛片毛片免费很很综合 a级黄色视屏 | 免费一级日本c片完整版 | 双子母性本能在线观看 | 国产亚洲精品久久yy5099 | 男人与禽交的方法 | 日本一区二区三区久久精品 | 我与么公激情性完整视频 | 精品国偷自产在线 | 国产91区 | 丝瓜视频成人在线观看 | 99青青青精品视频在线 | 亚洲欧美专区精品久久 | 欧美午夜精品久久久久久黑人 | 性xx色3d动画xx无尽 | 好大好硬好深好爽想要小雪 | 啊皇上你好大要知画 | xnxx动漫| 边摸边吃奶玩乳尖视频 | 免费看国产一级片 | 91制片厂制作传媒破解版免费 | 日本一区二区三区视频在线观看 | 精品国产91久久久久久久 | www.爱操 | 欧美三级一区二区 | 麻豆网站在线看 | aaa一级最新毛片 | 欧美日韩色图 | xxxxxx性受 | 欧美国产精品 | 免费网站看v片在线香蕉 | 九草视频在线 | 国产乱码在线精品可播放 | 大乳奶水bbw | 国产自拍啪啪 | 和日本免费不卡在线v |