如何通过JavaAPI在google云平台的hdfs中创建目录

de90aj5v  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(328)

我正在google云平台上运行hadoop集群,使用google云存储作为持久数据的后端。我能够从远程机器ssh到主节点并运行hadoop fs命令。不管怎样,当我尝试执行下面的代码时,我得到了一个超时错误。
代码

FileSystem hdfs =FileSystem.get(new URI("hdfs://mymasternodeip:8020"),new Configuration());
Path homeDir=hdfs.getHomeDirectory();
//Print the home directory
System.out.println("Home folder: " +homeDir); 

// Create a directory
Path workingDir=hdfs.getWorkingDirectory();
Path newFolderPath= new Path("/DemoFolder");

newFolderPath=Path.mergePaths(workingDir, newFolderPath);
if(hdfs.exists(newFolderPath))
    {
        hdfs.delete(newFolderPath, true); //Delete existing Directory
    }
//Create new Directory
hdfs.mkdirs(newFolderPath);

当执行hdfs.exists()命令时,我得到一个超时错误。
错误
org.apache.hadoop.net.connecttimeoutexception:从gl051-win7/192..1.调用111.222.333.444.bc.googleusercontent。com:8020 failed 套接字超时异常:org.apache.hadoop.net.connecttimeoutexception:等待通道准备好连接时超时20000毫秒。ch:java.nio.channels.socketchannel[connection pending remote=111.222.333.444.bc.googleusercontent.com/111.222.333.444:8020]
你知道在google云平台上使用javahadoopapi来对付hadoop有什么限制吗?
谢谢!

aemubtdh

aemubtdh1#

看起来您正在本地计算机上运行该代码,并试图连接到google计算引擎vm;默认情况下,gce有严格的防火墙设置,以避免将外部ip地址暴露给任意入站连接。如果您使用的是默认值,那么您的hadoop集群应该位于“默认”gce网络上。您需要按照添加防火墙的说明,允许在端口8020和其他hadoop端口(可能)上以及从您的本地ip地址进行传入tcp连接。它看起来像这样:

gcloud compute firewall-rules create allow-http \
    --description "Inbound HDFS." \
    --allow tcp:8020 \
    --format json \
    --source-ranges your.ip.address.here/32

请注意,您确实希望避免打开 0.0.0.0/0 因为hadoop没有对这些传入请求进行身份验证或授权。您需要尽可能地将其限制为只使用您计划拨入的入站ip地址。根据连接hadoop所使用的功能,您可能还需要打开几个其他端口。
更一般的建议是,只要有可能,您应该尝试在hadoop集群本身上运行代码;在这种情况下,您将使用主主机名本身作为hdfs授权,而不是外部ip:

hdfs://<master hostname>/foo/bar

这样,您就可以将端口暴露限制在ssh端口22上,在ssh端口22上,传入的流量由ssh守护进程正确地控制,这样您的代码就不必担心哪些端口是打开的,甚至不必担心处理ip地址。

相关问题