亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java執(zhí)行hadoop的基本操作實例代碼

 更新時間:2017年04月25日 09:28:37   投稿:lqh  
這篇文章主要介紹了Java執(zhí)行hadoop的基本操作實例代碼的相關(guān)資料,需要的朋友可以參考下

Java執(zhí)行hadoop的基本操作實例代碼

向HDFS上傳本地文件

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("已經(jīng)上傳文件到input文件夾啦");
  }

將output文件下載到本地

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("已經(jīng)將輸出文件保留到本地文件");
    fs.close();
  }

刪除hdfs中的文件

 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文件已經(jīng)刪除");
  }

執(zhí)行mapReduce程序

創(chuàng)建Mapper類和Reducer類

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);
    }
  }

執(zhí)行mapReduce程序

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 執(zhí)行完畢!");
    System.exit(job.waitForCompletion(true)?0:1);

  }

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

相關(guān)文章

  • Java編程中的檢查型異常與非檢查型異常分析

    Java編程中的檢查型異常與非檢查型異常分析

    這篇文章主要介紹了Java編程中的未檢查型異常與非檢查型異常,以及異常的處理方式,需要的朋友可以參考下
    2017-09-09
  • 關(guān)于java關(guān)鍵字this和super的區(qū)別和理解

    關(guān)于java關(guān)鍵字this和super的區(qū)別和理解

    這篇文章主要給大家介紹了關(guān)于java關(guān)鍵字this和super的區(qū)別和理解的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • 深層剖析java應(yīng)用開發(fā)中MyBayis緩存

    深層剖析java應(yīng)用開發(fā)中MyBayis緩存

    這篇文章主要為大家深層剖析java開發(fā)中MyBayis緩存,文中講解了Mybatis緩存的分類以及使用的方式,有需要的朋友可以借鑒參考下,希望可以有所幫助
    2021-09-09
  • Mybatis中的@Param及動態(tài)SQL詳解

    Mybatis中的@Param及動態(tài)SQL詳解

    這篇文章主要介紹了Mybatis中的@Param及動態(tài)SQL詳解,@Param是MyBatis所提供的作為Dao層的注解,作用是用于傳遞參數(shù),從而可以與SQL中的的字段名相對應(yīng),需要的朋友可以參考下
    2023-10-10
  • Java深入淺出掌握SpringBoot之MVC自動配置原理篇

    Java深入淺出掌握SpringBoot之MVC自動配置原理篇

    在進行項目編寫前,我們還需要知道一個東西,就是SpringBoot對我們的SpringMVC還做了哪些配置,包括如何擴展,如何定制,只有把這些都搞清楚了,我們在之后使用才會更加得心應(yīng)手
    2021-10-10
  • Java正則表達式處理特殊字符轉(zhuǎn)義的方法

    Java正則表達式處理特殊字符轉(zhuǎn)義的方法

    由于正則表達式定了一些特殊字符,而有時候需要對這些特殊字符進行匹配的話就需要進行轉(zhuǎn)義了,下面這篇文章主要給大家介紹了Java正則表達式處理特殊字符轉(zhuǎn)義的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • Java OOM原因以及解決方案

    Java OOM原因以及解決方案

    這篇文章主要介紹了Java OOM原因以及解決方案,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Java獲取控制臺輸入的兩種方法小結(jié)

    Java獲取控制臺輸入的兩種方法小結(jié)

    這篇文章主要介紹了Java獲取控制臺輸入的兩種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringMVC+ZTree實現(xiàn)樹形菜單權(quán)限配置的方法

    SpringMVC+ZTree實現(xiàn)樹形菜單權(quán)限配置的方法

    本篇文章主要介紹了SpringMVC+ZTree實現(xiàn)樹形菜單權(quán)限配置的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • springBoot 過濾器去除請求參數(shù)前后空格實例詳解

    springBoot 過濾器去除請求參數(shù)前后空格實例詳解

    這篇文章主要為大家介紹了springBoot 過濾器去除請求參數(shù)前后空格實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論