hadoophdfs编程写操作

nwsw7zdq  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(309)

不久前我问了一个类似的问题,但那时我不知道我在说什么。我张贴这个问题与进一步的细节和问题的要点。
所以我用namenode和2个datanode建立了hadoop集群。我正在使用hadoop2.9.0。我运行了命令hdfs-dfs-put“somerandomfile”,它似乎工作正常。我在这里唯一的困惑是为什么它要将我的文件存储到/user/hduser/path?我没有在配置中指定这个路径,那么它是如何在hdfs上构建这个路径的呢?
此外,我还创建了一个小java程序来做同样的事情。我创建了一个简单的eclipse项目,并编写了以下行:

public static boolean fileWriteHDFS(InputStream input, String fileName) {   
    try {
        System.setProperty("HADOOP_USER_NAME", "hduser");

        //Get Configuration of Hadoop system
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://localhost:9000");
        //conf.get("fs.defaultFS");     

        //Extract destination path
        URI uri = URI.create(DESTINATION_PATH+fileName);
        Path path = new Path(uri);

        //Destination file in HDFS
        FileSystem fs = FileSystem.get(uri, conf); //.get(conf);

        //Check if the file already exists
        if (fs.exists(path))
        {
            //Write appropriate error to log file and return.
            return false;
        }

        //Create an Output stream to the destination path
        FSDataOutputStream out = fs.create(path);

        //Copy file from input steam to HDFSs
        IOUtils.copyBytes(input, out, 4096, true);

        //Close all the file descriptors
        out.close();
        fs.close();
        //All went perfectly as planned
        return true;    
    } catch (Exception e) {
        //Something went wrong
        System.out.println(e.toString());
        return false;
    }
}

我添加了以下三个hadoop库:
/home/hduser/bin/hadoop-2.9.0/share/hadoop/common/hadoop-common-2.9.0.jar/home/hduser/bin/hadoop-2.9.0/share/hadoop/common/hadoop-common-2.9.0-tests.jar/home/hduser/bin/hadoop-2.9.0/share/hadoop/common/hadoop-nfs-2.9.0.jar
您可以看到我的hadoop安装位置是/home/hduser/bin/hadoop-2.9.0/。。。当我运行这个代码时,它抛出一个异常。即

Exception in thread "main" java.lang.NoClassDefFoundError: com/ctc/wstx/io/InputBootstrapper
at com.ws.filewrite.fileWrite.fileWriteHDFS(fileWrite.java:21)
at com.ws.main.listenerService.main(listenerService.java:21)
Caused by: java.lang.ClassNotFoundException: com.ctc.wstx.io.InputBootstrapper
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

特别是在谎言中抛出了异常:
configuration conf=新配置();
我是不是漏了什么?是什么导致了这个问题?我是完全新的hdfs,所以请原谅我这是明显的问题。

e4yzc0pl

e4yzc0pl1#

Hadoop2.9依赖项与Hadoop2.6不相似。
我遇到了同样的情况,并试图找到依赖jar。这很难,下次可能会错过另一个jar。。。
所以,我使用maven来管理依赖关系。
只要附加这两个依赖项,问题就迎刃而解了。

<dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.9.0</version>
        <!--<scope>provided</scope>-->
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>2.9.0</version>
    </dependency>

相关问题