如何从java通过hdfs协议访问hadoop?

发布于 2024-12-11 06:45:08 字数 1528 浏览 0 评论 0原文

我找到了一种通过 hftp 连接到 hadoop 的方法,它工作正常,(只读):

uri = "hftp://172.16.xxx.xxx:50070/";

System.out.println( "uri: " + uri );           
Configuration conf = new Configuration();

FileSystem fs = FileSystem.get( URI.create( uri ), conf );
fs.printStatistics();

但是,我想读/写以及复制文件,也就是说,我想通过以下方式连接HDFS。如何启用 hdfs 连接以便可以编辑实际的远程文件系统?

我尝试从 hftp 更改上面的协议 -> hdfs,但我得到了以下异常...

(请原谅我对 url 协议和 hadoop 的了解不足,我认为这是我问的一个有点奇怪的问题,但任何帮助将不胜感激!)

线程“main”中的异常 java.io.IOException:本地异常调用 /172.16.112.131:50070 失败:java.io.EOFException at org.apache.hadoop.ipc.Client.wrapException(Client.java:第1139章 1107 org.apache.hadoop.ipc.RPC$Invoker.invoke(RPC.java:226) 位于 org.apache.hadoop.ipc.RPC.getProxy(RPC.java:398) 处 $Proxy0.getProtocolVersion(未知来源) .apache.hadoop.ipc.RPC.getProxy(RPC.java:384) 在org.apache.hadoop.hdfs.DFSClient.createRPCNamenode(DFSClient.java:111) 在 org.apache.hadoop.hdfs.DFSClient.(DFSClient.java:213) 在 org.apache.hadoop.hdfs.DFSClient.(DFSClient.爪哇:180)在org.apache.hadoop.hdfs.DistributedFileSystem.initialize(DistributedFileSystem.java:89) 在 org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:1514) 在 org.apache.hadoop.fs.FileSystem.access$200 (文件系统.java:67)位于org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:1548) 在 org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:1530) 在 org.apache.hadoop.fs。 FileSystem.get(FileSystem.java:228) at sb.HadoopRemote.main(HadoopRemote.java:24)

I found a way to connect to hadoop via hftp, and it works fine, (read only) :

uri = "hftp://172.16.xxx.xxx:50070/";

System.out.println( "uri: " + uri );           
Configuration conf = new Configuration();

FileSystem fs = FileSystem.get( URI.create( uri ), conf );
fs.printStatistics();

However, I want to read/write as well as copy files, that is, I want to connect over hdfs . How can I enable hdfs connections so that i can edit the actual , remote filesystem ?

I tried to change the protocol above from hftp -> hdfs, but I got the following exception ...

(forgive my poor knowledge of url protocols and hadoop , I assume this is a somewhat strange question im asking, but any help would really be appreciated !)

Exception in thread "main" java.io.IOException: Call to /172.16.112.131:50070 failed on local exception: java.io.EOFException at org.apache.hadoop.ipc.Client.wrapException(Client.java:1139) at org.apache.hadoop.ipc.Client.call(Client.java:1107) at org.apache.hadoop.ipc.RPC$Invoker.invoke(RPC.java:226) at $Proxy0.getProtocolVersion(Unknown Source) at org.apache.hadoop.ipc.RPC.getProxy(RPC.java:398) at org.apache.hadoop.ipc.RPC.getProxy(RPC.java:384) at org.apache.hadoop.hdfs.DFSClient.createRPCNamenode(DFSClient.java:111) at org.apache.hadoop.hdfs.DFSClient.(DFSClient.java:213) at org.apache.hadoop.hdfs.DFSClient.(DFSClient.java:180) at org.apache.hadoop.hdfs.DistributedFileSystem.initialize(DistributedFileSystem.java:89) at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:1514) at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:67) at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:1548) at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:1530) at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:228) at sb.HadoopRemote.main(HadoopRemote.java:24)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2024-12-18 06:45:08

只需将您想要访问的 hadoop 的 core-site.xmlhdfs-site.xml 添加到 conf 中,如下所示:

import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.testng.annotations.Test;

/**
 * @author karan
 *
 */
public class HadoopPushTester {

    @Test
    public void run() throws Exception {

        Configuration conf = new Configuration();

        conf.addResource(new Path("src/test/resources/HadoopConfs/core-site.xml"));
        conf.addResource(new Path("src/test/resources/HadoopConfs/hdfs-site.xml"));

        String dirName = "hdfs://hosthdfs:port/user/testJava";

        // Values of hosthdfs:port can be found in the core-site.xml  in the fs.default.name
        FileSystem fileSystem = FileSystem.get(conf);


        Path path = new Path(dirName);
        if (fileSystem.exists(path)) {
            System.out.println("Dir " + dirName + " already exists");
            return;
        }

        // Create directories
        fileSystem.mkdirs(path);

        fileSystem.close();
    }
}

Just add the core-site.xml and the hdfs-site.xml of the hadoop you want to hit to conf, something like this:

import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.testng.annotations.Test;

/**
 * @author karan
 *
 */
public class HadoopPushTester {

    @Test
    public void run() throws Exception {

        Configuration conf = new Configuration();

        conf.addResource(new Path("src/test/resources/HadoopConfs/core-site.xml"));
        conf.addResource(new Path("src/test/resources/HadoopConfs/hdfs-site.xml"));

        String dirName = "hdfs://hosthdfs:port/user/testJava";

        // Values of hosthdfs:port can be found in the core-site.xml  in the fs.default.name
        FileSystem fileSystem = FileSystem.get(conf);


        Path path = new Path(dirName);
        if (fileSystem.exists(path)) {
            System.out.println("Dir " + dirName + " already exists");
            return;
        }

        // Create directories
        fileSystem.mkdirs(path);

        fileSystem.close();
    }
}
魂ガ小子 2024-12-18 06:45:08

关于 hadoop :您需要确保 core-site.xml namenode 条目服务于 0.0.0.0 而不是 127.0.0.1 (localhost )在你的hadoop配置中。重要的是,出于某种原因,clouderas vm distro 默认为 localhost

Regarding hadoop : You need to make sure the core-site.xml namenode entry is serving to 0.0.0.0 instead of 127.0.0.1 (localhost) in your hadoop configuration. Importantly, clouderas vm distro defaults to localhost for some reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文