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

2023-05-16

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

org.apache.hadoop.security.AccessControlException: Permission denied: user=Dell, access=WRITE 的相关文章

随机推荐