Hadoop HDFS PutMerge不起作用

时间:2014-04-16 03:54:23

标签: java hadoop hdfs

我尝试在我的计算机上运行Hadoop in Action中的PutMerge。但它不起作用。我在本地机器上创建了几个文件。以下是代码

package org.apache.hadoop.examples;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class PutMerge {

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

    Configuration conf = new Configuration();
    FileSystem hdfs = FileSystem.get(conf);
    FileSystem local = FileSystem.getLocal(conf);

    Path inputDir = new Path("/home/yehang/Documents/example");
    Path hdfsFile = new Path("hdfs://localhost:54310:/user/yehang");

    //System.out.println(inputDir);
    //System.out.println(hdfsFile);

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

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

    }

}

}

当我在hadoop上运行时,出现以下错误:

  Exception in thread "main" java.lang.IllegalArgumentException: Wrong FS:        hdfs://localhost:54310:/user, expected: file:///
at org.apache.hadoop.fs.FileSystem.checkPath(FileSystem.java:381)
at org.apache.hadoop.fs.RawLocalFileSystem.pathToFile(RawLocalFileSystem.java:55)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:335)
at org.apache.hadoop.fs.ChecksumFileSystem.mkdirs(ChecksumFileSystem.java:492)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:377)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:364)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:555)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:536)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:443)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:435)
at org.apache.hadoop.examples.PutMerge.main(PutMerge.java:28)

谁能帮帮我?

1 个答案:

答案 0 :(得分:1)

Hadoop分布式文件系统(HDFS)相关的类文件在项目hadoop-hdfs中定义。 因此,您必须在Java类路径中导入jar hadoop-hdfs-*.jar以解决此问题。