异常:org.apache.hadoop.security.AccessControlException: Permission denied: user=zhangsan, access=WRITE, inode="/":hdfs:supergroup:drwxr-xr-x
通常的解决方法是1.系统用户使用hadoop组中的用户操作;2.hdfs dfs -chmod -R 777 /dir设置操作权限
今天想在代码中设置一下,尝试了两种方法:
1.没有成功的尝试
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.fs.Path
/**
* @author WANGPINGLE767
*/
object HdfsApp {
def main(args: Array[String]): Unit = {
val conf = new Configuration()
// conf.set("hadoop.job.ugi","zhangsan,hdfs") //设置未成功
val fs = FileSystem.get(conf)
// println(System.getProperty("user.name"))
// System.setProperty("hadoop.user.name", "zhangsan") //设置未成功
// println(System.getProperty("hadoop.user.name"))
val path = new Path("/zhangsan/20170610")
fs.create(path)
fs.close()
}
}
尝试代理操作
import java.io.IOException;
import java.security.PrivilegedExceptionAction;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;
public class JhdfsApp {
public static void main(String[] args) {
UserGroupInformation ugi =
// UserGroupInformation.createProxyUser(user, UserGroupInformation.getLoginUser());
UserGroupInformation.createRemoteUser("zhangsan"); //设置成功
try {
ugi.doAs( new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
System.out.println(System.getProperty("user.name"));
System.out.println(System.getProperty("hadoop.user.name"));
Path path = new Path("/zhangsan/20170610");
fs.create(path);
fs.close();
return null;
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}