Hadoop hdfs DistributedFileSystem无法实例化

时间:2018-02-13 13:18:16

标签: java hdfs

我已经设置了一个hadoop hdfs集群,因为我是hadoop的新手,所以我一直在尝试按照一个简单的例子从我在本地机器上编写的java驱动程序读取/写入hdfs。我试图测试的例子如下:

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

    args = new String[3];
    args[0] = "add";
    args[1] = "./files/jaildata.csv";
    args[2] = "hdfs://<Namenode-Host>:<Port>/dir1/dir2/";        
    if (args.length < 1) {
        System.out.println("Usage: hdfsclient add/read/delete/mkdir [<local_path> <hdfs_path>]");
        System.exit(1);
    }

    FileSystemOperations client = new FileSystemOperations();
    String hdfsPath = "hdfs://<Namenode-Host>:<Port>";

    Configuration conf = new Configuration();
    conf.addResource(new Path("file:///user/local/hadoop/etc/hadoop/core-site.xml"));
    conf.addResource(new Path("file:///user/local/hadoop/etc/hadoop/hdfs-site.xml"));

    if (args[0].equals("add")) {
        if (args.length < 3) {
            System.out.println("Usage: hdfsclient add <local_path> <hdfs_path>");
            System.exit(1);
        }
        client.addFile(args[1], args[2], conf);

    } else {
        System.out.println("Usage: hdfsclient add/read/delete/mkdir [<local_path> <hdfs_path>]");
        System.exit(1);
    }
    System.out.println("Done!");
}

addFile函数如下:

public void addFile(String source, String dest, Configuration conf) throws IOException {

    FileSystem fileSystem = FileSystem.get(conf);

    // Get the filename out of the file path
    String filename = source.substring(source.lastIndexOf('/') + 1, source.length());

    // Create the destination path including the filename.
    if (dest.charAt(dest.length() - 1) != '/') {
        dest = dest + "/" + filename;
    } else {
        dest = dest + filename;
    }
    Path path = new Path(dest);
    if (fileSystem.exists(path)) {
        System.out.println("File " + dest + " already exists");
        return;
    }

    // Create a new file and write data to it.
    FSDataOutputStream out = fileSystem.create(path);
    InputStream in = new BufferedInputStream(new FileInputStream(new File(source)));

    byte[] b = new byte[1024];
    int numBytes = 0;
    while ((numBytes = in.read(b)) > 0) {
        out.write(b, 0, numBytes);
    }

    // Close all the file descriptors
    in.close();
    out.close();
    fileSystem.close();
}

该项目是一个maven项目,hadoop-common-2.6.5hadoop-hdfs-2.9.0hadoop=hdfs-client 2.9.0添加到依赖项,并配置为构建包含所有依赖项的jar。

我的问题,无论我尝试过不同的演示示例,我都会在FileSystem创建FileSystem fileSystem = FileSystem.get(conf);时收到以下异常:

Exception in thread "main" java.util.ServiceConfigurationError: org.apache.hadoop.fs.FileSystem: Provider org.apache.hadoop.hdfs.DistributedFileSystem could not be instantiated
at java.util.ServiceLoader.fail(ServiceLoader.java:232)
at java.util.ServiceLoader.access$100(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at org.apache.hadoop.fs.FileSystem.loadFileSystems(FileSystem.java:2565)
at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:2576)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2593)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:91)
at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2632)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2614)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:370)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:169)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:354)
at org.apache.hadoop.fs.Path.getFileSystem(Path.java:296)
Caused by: java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FSDataOutputStreamBuilder

我不清楚如何通过,我已经尝试了我在网上看到的几个解决方案,所以我很感激任何建议......

感谢。

1 个答案:

答案 0 :(得分:1)

org.apache.hadoop.fs.FSDataOutputStreamBuilder课程不在hadoop-common-2.6.5但在hadoop-common-2.9.0。{/ p>

正如我注意到你已经在为hdfs-client使用2.9.0版本了。 将其他hadoop包与2.9.0对齐以避免类似问题。

请在您的版本中引用hadoop-common的2.9.0版本以解决此问题。