org.apache.hadoop.security.AccessControlException: Permission denied: user=Dell, access=WRITE

Win10 Dell 主机 Java 访问 Linux HDFS 时报错:

日志

Exception in thread "main" org.apache.hadoop.security.AccessControlException: Permission denied: user=Dell, access=WRITE, inode="/":hadoop:supergroup:drwxr-xr-x
	at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:399)
	at ......

代码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.*;

public class MainApp {
	public static void main(String[] args) throws Exception{
		// 配置连接
		Configuration conf = new Configuration();
		// 在 core-site.xml 文件中
		conf.set("fs.defaultFS","hdfs://node102:8020");
		FileSystem fs = FileSystem.get(conf);

		// 1.新建目录
		Path path = new Path("/myfile2/hello");
		if (!fs.exists(path)){
			fs.mkdirs(path);
		}else{
			System.out.println("路径已存在");
		}

		// 2.读取文件
		InputStream in = fs.open(new Path("/myfile2/word.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String str;
		while ((str=br.readLine())!=null){
			System.out.println(str);
		}

		// 3.上传文件
		fs.copyFromLocalFile(new Path("D:\\Program Files (x86)\\WorkspaceIDEA\\HadoopStu\\pom.xml"),
				new Path("/myfile2/hello/pom.xml"));

		fs.close();
		System.out.println("Finished.");
	}
}

解决办法

  • (首选)在代码中规定访问用户:
    • 前几行代码修改如下,指定用户为 “hadoop”(我的 Linux 用户名,因人而异):
		// 配置连接
		Configuration conf = new Configuration();
		// 在 core-site.xml 文件中
		//conf.set("fs.defaultFS","hdfs://node102:8020");
		//FileSystem fs = FileSystem.get(conf);
		FileSystem fs = FileSystem.get(new URI("hdfs://node102:8020"), conf, "hadoop");

  • 修改HDFS文件权限(这里递归修改了整个目录的权限):
    hdfs dfs -chmod -R 777 /myfile
    • 原权限:drwxr-xr-x
    • 修改后:drwxrwxrwx

  • 也可以修改文件的用户为Dell(报错显示我的用户名为Dell):
    hdfs dfs -chown -R Dell /myfile
    • 原用户:hadoop
    • 修改后:Dell
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬金砖的小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值