在idea中上传文件到HDFS失败,找不到文件,文件不存在
来源:3-11 Java API操作HDFS文件系统
慕粉2033125118
2018-08-14
在/home/hadoop/data/jerry.txt是存在的,但是它总说不存在,是哪里出现问题了嘛?
5回答
-
Michael_PK
2018-08-14
请理解什么叫“本地”!
10 -
慕粉2033125118
提问者
2018-08-14
我搞了个单元测试这个目录文件是否存在,结果显示不存在,实际上是有的
00 -
慕粉2033125118
提问者
2018-08-14
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
00 -
慕粉2033125118
提问者
2018-08-14
里面没有空格哒,换成了个简单的路径,下面HDFS路径也补全了,可还是不行呢,哈哈
00 -
慕粉2033125118
提问者
2018-08-14
这个改成本地也不不行是怎么回事呢,找了半天找不出原因,谢谢
0122018-08-14
相似问题