采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
在/home/hadoop/data/jerry.txt是存在的,但是它总说不存在,是哪里出现问题了嘛?
请理解什么叫“本地”!
我搞了个单元测试这个目录文件是否存在,结果显示不存在,实际上是有的
package com.imooc.hadoop.hdfs;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.net.URI;/*Java API操作HDFS文件系统 */public class HDFSApp { public static final String HDFS_PATH="hdfs://192.168.234.129:8020"; FileSystem fileSystem=null; Configuration configuration=null; /** * 创建HDFS目录 */ @Test public void mkdir() throws Exception{ System.out.println("qqq"); fileSystem.mkdirs(new Path("/hdfsapi/test")); System.out.println("hhh"); } /* 创建文件 */ @Test public void create() throws Exception{ FSDataOutputStream output=fileSystem.create(new Path("/hdfsapi/test/a.txt")); output.write("hello hadoop".getBytes()); output.flush(); output.close(); } /* 查看文件 */ @Test public void cat() throws Exception{ FSDataInputStream in=fileSystem.open(new Path("/hdfsapi/test/a.txt")); IOUtils.copyBytes(in,System.out,1024); in.close(); } /* 重命名 */ @Test public void rename() throws Exception{ Path oldPath=new Path("/hdfsapi/test/a.txt"); Path newPath=new Path("/hdfsapi/test/b.txt"); fileSystem.rename(oldPath,newPath); } /* 上传文件到HDFS */ @Test public void copyFromLocalFile() throws Exception{ Path localPath=new Path("C:\\jerry.txt"); Path hdfsPath=new Path("hdfs://192.168.234.129:8020/hdfsapi/test"); //fileSystem.copyFromLocalFile(localPath,hdfsPath); try { // 将本地文件上传到HDFS fileSystem.copyFromLocalFile(localPath, hdfsPath); System.out.println("File " + localPath.toString() + " copied to " + hdfsPath.toString()); } catch (Exception e) { System.err.println("Exception caught! :" + e); System.exit(1); } finally { fileSystem.close(); } } @Before public void setUp() throws Exception{ System.out.println("HDFSApp.setUp"); configuration=new Configuration(); fileSystem=FileSystem.get(new URI(HDFS_PATH),configuration,"hadoop"); } @After public void tearDown() throws Exception{ configuration=null; fileSystem=null; System.out.println("HDFSApp.tearDown"); }}
结果:
HDFSApp.setUp
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception caught! :java.io.FileNotFoundException: File C:/jerry.txt does not exist
Process finished with exit code 1
里面没有空格哒,换成了个简单的路径,下面HDFS路径也补全了,可还是不行呢,哈哈
这个改成本地也不不行是怎么回事呢,找了半天找不出原因,谢谢
建议换个没空格的目录试试
HDFS的路径写全
还是不行哈,是不是我这个IDEA的某个IO之类的没有配置好,导致不能读取本地文件呢
登录后可查看更多问答,登录/注册
【毕设】大数据零基础入门必备,轻松掌握Hadoop开发核心技能
1.3k 30
2.4k 28
1.2k 17
1.3k 16
982 13