我在包含的测试程序中尝试将文件从本地磁盘复制到 HDFS。代码如下:
package foo.foo1.foo2.test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class TestTestTest {
public static void main(String[] args) {
String srcLocation = "foo";
String destination = "hdfs:///tmp/";
FileSystem hdfs = null;
Configuration configuration = new Configuration();
configuration.set("fs.default.name", "hdfs://namenode:54310/");
try {
hdfs = FileSystem.get(configuration);
} catch (IOException e2) {
e2.printStackTrace();
return;
}
Path srcpath = new Path(srcLocation);
Path dstpath = new Path(destination);
try {
hdfs.copyFromLocalFile(srcpath, dstpath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这失败了,但有以下例外:
java.io.IOException: Call to namenode/10.1.1.1:54310 failed on local exception: java.io.EOFException
at org.apache.hadoop.ipc.Client.wrapException(Client.java:775)
at org.apache.hadoop.ipc.Client.call(Client.java:743)
at org.apache.hadoop.ipc.RPC$Invoker.invoke(RPC.java:220)
at $Proxy0.getProtocolVersion(Unknown Source)
at org.apache.hadoop.ipc.RPC.getProxy(RPC.java:359)
at org.apache.hadoop.hdfs.DFSClient.createRPCNamenode(DFSClient.java:106)
at org.apache.hadoop.hdfs.DFSClient.<init>(DFSClient.java:207)
at org.apache.hadoop.hdfs.DFSClient.<init>(DFSClient.java:170)
at org.apache.hadoop.hdfs.DistributedFileSystem.initialize(DistributedFileSystem.java:82)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:1378)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:66)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:1390)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:196)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:95)
at foo.foo1.foo2.test.TestTestTest.main(TestTestTest.java:22)
Caused by: java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:375)
at org.apache.hadoop.ipc.Client$Connection.receiveResponse(Client.java:501)
at org.apache.hadoop.ipc.Client$Connection.run(Client.java:446)
我的问题看似简单:是什么原因造成的,我怎样才能使这个程序工作?根据我能找到的少量信息,我推测连接到 HDFS 存在问题,这与配置中的 fs.default.name 属性有关。以下是我的 core-site.xml 文件的相关部分:
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://namenode:54310</value>
</property>
</configuration>
也许特别有趣的是,如果我将类路径中的所有 jar 包捆绑到一个巨型 jar 中,并通过 hadoop 命令运行该程序,它就可以正常工作。那么我做错了什么?