hadoophdfs编程写操作

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

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

  1. public static boolean fileWriteHDFS(InputStream input, String fileName) {
  2. try {
  3. System.setProperty("HADOOP_USER_NAME", "hduser");
  4. //Get Configuration of Hadoop system
  5. Configuration conf = new Configuration();
  6. conf.set("fs.defaultFS", "hdfs://localhost:9000");
  7. //conf.get("fs.defaultFS");
  8. //Extract destination path
  9. URI uri = URI.create(DESTINATION_PATH+fileName);
  10. Path path = new Path(uri);
  11. //Destination file in HDFS
  12. FileSystem fs = FileSystem.get(uri, conf); //.get(conf);
  13. //Check if the file already exists
  14. if (fs.exists(path))
  15. {
  16. //Write appropriate error to log file and return.
  17. return false;
  18. }
  19. //Create an Output stream to the destination path
  20. FSDataOutputStream out = fs.create(path);
  21. //Copy file from input steam to HDFSs
  22. IOUtils.copyBytes(input, out, 4096, true);
  23. //Close all the file descriptors
  24. out.close();
  25. fs.close();
  26. //All went perfectly as planned
  27. return true;
  28. } catch (Exception e) {
  29. //Something went wrong
  30. System.out.println(e.toString());
  31. return false;
  32. }
  33. }

我添加了以下三个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/。。。当我运行这个代码时,它抛出一个异常。即

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

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

e4yzc0pl

e4yzc0pl1#

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

  1. <dependency>
  2. <groupId>org.apache.hadoop</groupId>
  3. <artifactId>hadoop-common</artifactId>
  4. <version>2.9.0</version>
  5. <!--<scope>provided</scope>-->
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.hadoop</groupId>
  9. <artifactId>hadoop-hdfs</artifactId>
  10. <version>2.9.0</version>
  11. </dependency>

相关问题