|
问题描述:
hadoop在虚拟机中的linux系统下
在本地通过eclipse编写代码把本地文件上传至hadoop平台的指定目录
代码如下:
@Testpublic void upload() throws IOException {
Configuration conf
= new Configuration(); conf.set(
"fs.defaultFS", "hdfs://lujie01:9000/"); FileSystem fs
= FileSystem.get(conf); Path path
= new Path("hdfs://lujie01:9000/aa/lulu.txt"); FSDataOutputStream outputStream
= fs.create(path); FileInputStream inputStream
= new FileInputStream("D:/lulu.txt"); IOUtils.copy(inputStream, outputStream);
}
文件上传有更加简单的方法:
直接使用FileSystem的copyFromLocalFile方法
fs.copyFromLocalFile(new Path("D:/lulu.txt"), new Path("hdfs://lujie01:9000/aa/lulu.txt"));
在没有设定用户或者更改hadoop下/aa文件夹的权限之前,本机的用户身份无法通过hadoop的认证,导致无法上传
错误如下:
解决方法一:
把/aa文件夹的权限设为任何用户都可读可写可操作
再次执行测试程序,正常运行,通过客户端可以查看aa中的文件lulu.txt。表示已上传成功,注意这里的owner是lujie,电脑本地的用户名
解决方法二:
在Run Configuration中设置Arguments,把用户名改为Linux系统的用户名hadoop
解决方法三:
直接在代码中指定用户为hadoop
FileSystem fs = FileSystem.get(new URI("hdfs://lujie01:9000/"),conf,"hadoop");
@Testpublic void upload() throws IOException, InterruptedException, URISyntaxException {
Configuration conf
= new Configuration(); conf.set(
"fs.defaultFS", "hdfs://lujie01:9000/"); FileSystem fs
= FileSystem.get(new URI("hdfs://lujie01:9000/"),conf,"hadoop"); Path path = new Path("hdfs://lujie01:9000/aa/lulu1.txt"); FSDataOutputStream outputStream
= fs.create(path); FileInputStream inputStream
= new FileInputStream("D:/lulu.txt"); IOUtils.copy(inputStream, outputStream);
}
上传成功后,在客户端查看
|
|
|