我刚才问了一个类似的问题,但后来我不知道我在说什么。我将发布此问题并提供更多详细信息和重点查询。
所以我已经设置了带有名称节点和 2 个数据节点的 hadoop 集群。我正在使用 hadoop 2.9.0。我运行了命令 hdfs dfs -put "SomeRandomFile"它似乎工作正常。我在这里唯一的困惑是为什么它将我的文件存储到/user/hduser/路径?我没有在配置中的任何地方指定此路径,那么它如何在 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 = new Configuration();
我是不是漏掉了什么?是什么导致了这个问题?我是 HDFS 的新手,所以请原谅我这是一个明显的问题。
最佳答案
hadoop 2.9 依赖项与 hadoop 2.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>
关于java - Hadoop HDFS 以编程方式写入操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47823715/