Java 中的 HDFS - 指定用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16768115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:57:47  来源:igfitidea点击:

HDFS from Java - Specifying the User

javasecurityauthenticationhadoophdfs

提问by Kong

I'm happily connecting to HDFS and listing my home directory:

我很高兴连接到 HDFS 并列出我的主目录:

Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://hadoop:8020");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fs = FileSystem.get(conf);
RemoteIterator<LocatedFileStatus> ri = fs.listFiles(fs.getHomeDirectory(), false);
while (ri.hasNext()) {
    LocatedFileStatus lfs = ri.next();
    log.debug(lfs.getPath().toString());
}
fs.close();

What I'm wanting to do now though is connect as a specific user (not the whois user). Does anyone know how you specify which user you connect as?

我现在想做的是作为特定用户(而不是 whois 用户)进行连接。有谁知道你如何指定你连接的用户?

回答by Roman Nikitchenko

As soon as I see this is done through UserGroupInformationclass and PrivilegedActionor PrivilegedExceptionAction. Here is sample code to connect to remote HDFS 'like' different user ('hbase' in this case). Hope this will solve your task. In case you need full scheme with authentication you need to improve user handling. But for SIMPLE authentication scheme (actually no authentication) it works just fine.

一旦我看到这是通过UserGroupInformationclass 和PrivilegedActionor 完成的PrivilegedExceptionAction。这是连接到远程 HDFS 的示例代码,“像”不同的用户(在本例中为“hbase”)。希望这将解决您的任务。如果您需要带有身份验证的完整方案,您需要改进用户处理。但是对于 SIMPLE 身份验证方案(实际上没有身份验证),它工作得很好。

package org.myorg;

import java.security.PrivilegedExceptionAction;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileStatus;

public class HdfsTest {

    public static void main(String args[]) {

        try {
            UserGroupInformation ugi
                = UserGroupInformation.createRemoteUser("hbase");

            ugi.doAs(new PrivilegedExceptionAction<Void>() {

                public Void run() throws Exception {

                    Configuration conf = new Configuration();
                    conf.set("fs.defaultFS", "hdfs://1.2.3.4:8020/user/hbase");
                    conf.set("hadoop.job.ugi", "hbase");

                    FileSystem fs = FileSystem.get(conf);

                    fs.createNewFile(new Path("/user/hbase/test"));

                    FileStatus[] status = fs.listStatus(new Path("/user/hbase"));
                    for(int i=0;i<status.length;i++){
                        System.out.println(status[i].getPath());
                    }
                    return null;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

回答by sadhu

If I got you correct, all you want is to get home directory of the user if specify and not the whois user.

如果我猜对了,您想要的只是获取用户的主目录(如果指定而不是 whois 用户)。

In you configuration file, set your homedir property to user/${user.name}. Make sure you have a system property named user.name

在您的配置文件中,将您的 homedir 属性设置为 user/${user.name}。确保您有一个名为 user.name 的系统属性

This worked in my case.

这在我的情况下有效。

I hope this is what you want to do, If not add a comment.

我希望这是你想要做的,如果没有添加评论。