[英]Hadoop HDFS Write Operation Programmatically
不久前我問了一個類似的問題,但后來我不知道我在說什么。 我正在發布這個問題,並附有更多詳細信息和重點查詢。
所以我用namenode和2個datanodes建立了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/... 當我運行此代碼時,它會引發異常。 IE
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
具體來說,異常是在謊言中引發的:
配置 conf = new Configuration();
我在這里錯過了什么嗎? 是什么導致了這個問題? 我對 HDFS 完全陌生,所以請原諒我這是一個明顯的問題。
hadoop 2.9 依賴項與 hadoop 2.6 不同。
我遇到了同樣的情況,並嘗試找到依賴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>
對我來說缺少 jar 是這樣的:
hadoop-client-runtime.jar<\/code> 。
從 Maven 添加額外的依賴項解決了問題。
作為一個附注,下面是我的依賴項列表(我使用的是 SBT\/Scala):
libraryDependencies ++= Seq(
"org.apache.hadoop" % "hadoop-client-api" % "3.3.1",
"commons-logging" % "commons-logging" % "1.2",
"org.slf4j" % "slf4j-api" % "1.7.35",
"org.apache.hadoop" % "hadoop-client-runtime" % "3.3.1"
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.