-无输出

5m1hhzi4  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(161)

这是我的密码

public class solution1 {

public static void main(String[] args) throws IOException {

    String localStr = args[0];
    String hdfsStr = args[1];

    Configuration conf = new Configuration();
    FileSystem hdfs  = FileSystem.get(URI.create(hdfsStr), conf);
    FileSystem local = FileSystem.getLocal(conf);

    Path inputDir = new Path(localStr);
    String folderName = inputDir.getName(); 
    Path hdfsFile = new Path(hdfsStr, folderName);

    try {
        FileStatus[] inputFiles = local.listStatus(inputDir);
        FSDataOutputStream out = hdfs.create(hdfsFile);

        for (int i=0; i<inputFiles.length; i++) {
            System.out.println(inputFiles[i].getPath().getName());
            FSDataInputStream in = local.open(inputFiles[i].getPath());
            byte buffer[] = new byte[256];
            int bytesRead = 0;
            while( (bytesRead = in.read(buffer)) > 0) {
                out.write(buffer, 0, bytesRead);
            }
            in.close();
        }
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

我试图用这段代码来循环通过数百个.txt文件这个.txt文件的内容,这个文件有500个
更新
这是我在虚拟框中键入的内容,这是我得到的结果,输出不是预期的输出,没有成功的结果没有成功的结果
它不是读取单个txt文件中的内容
但是如果我使用相同的命令来运行另一个java文件,结果是,它将成功运行。看起来像是在跑
wordcountjava的代码在这里

public class WordCount {

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(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(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }

  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 {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        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);
    }
  }
}

我被困住了,真的不知道该怎么办。我应该在virtualbox中键入什么来读取所有500个txt文件?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题