最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

  }

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

相關文章

最新評論

宜黄县| 九江市| 揭东县| 阿克陶县| 云和县| 义乌市| 珲春市| 恩施市| 安阳市| 平泉县| 弥渡县| 卫辉市| 黔东| 康定县| 姜堰市| 舟曲县| 上林县| 沁水县| 来宾市| 邯郸市| 彭山县| 台安县| 平果县| 行唐县| 新宁县| 格尔木市| 峡江县| 祁连县| 介休市| 灵宝市| 贵阳市| 宜兰市| 安塞县| 大宁县| 广昌县| 翁源县| 昌宁县| 永登县| 阿城市| 平度市| 延安市|