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

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

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
主站蜘蛛池模板: 呜呜别塞了啊抽插 | 日韩网新片免费 | 99小视频| 地址二地址三2021变更 | 小寡妇水真多好紧 | b站免费网站入口 | 激情婷婷成人亚洲综合 | 日韩欧美国产一区 | 欧美一级鲁丝片免费看 | 好舒服好爽再快点视频 | 日本综合在线观看 | 村上里沙40分钟在线观看 | 欧美另类老女人 | 无码中文字幕热热久久 | 国产性片在线观看 | 极端 成熟 性别 视频 | v视界影院成片 | 黄色大片免费网站 | 911精品国产亚洲日本美国韩国 | 日本女人www | 午夜精品久久久久久久99蜜桃 | 久久内在线视频精品mp4 | narutomanga玖辛奈之乳 | 国产精品亚洲片在线va | 新版孕妇bbwbbwbbw| www四虎| 成人欧美一区二区三区黑人 | 国产亚洲精品福利在线 | 波多野结衣不卡 | 99ri精品| 小小水蜜桃视频高清在线播放 | 国产99久久九九精品免费 | 调教禽兽| 精品高潮呻吟99AV无码视频 | 亚洲欧美精品一区天堂久久 | 和直男装修工在工地啪 | 四虎永久在线精品国产馆v视影院 | 欧美x×x | 色综合网天天综合色中文男男 | 成3d漫二区三区四区 | 高清国产在线观看 |