首页 > Hadoop的HDFS的java client jar包在哪下载?

Hadoop的HDFS的java client jar包在哪下载?

Hadoop的HDFS的java客户端编程接口的jar包在哪?
我在网上找到如下示例代码:

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;

class HDFSClient {

    static public void sampleHdfsWrite(String hdfs_url, String file) throws Exception {
        FileSystem hdfs = FileSystem.get(new Configuration());
        Path localfile = new Path(file);
        Path hdfspath = new Path("/");
        hdfs.copyFromLocalFile(localfile, hdfspath);
    }

    static public void sampleHdfsRead(String hdfs_url, String file) throws Exception {
        FileSystem hdfs = FileSystem.get(new Configuration());
        Path localfile = new Path("./" + file + ".backup");
        Path hdfspath = new Path("/" + file);
        hdfs.copyToLocalFile(hdfspath, localfile);
    }

}

一般来说,mysql、redis、mongodb都会有一个java client jar包,导入项目就可以调用API来存取数据了。HDFS我找了半天,发现在maven repository上的Hadoop-Core这个jar包可用:Maven Repository: org.apache.hadoop Â
尝试本地FileSystem拷贝文件,缺少一大堆apache.common、apache.lang之类的包,没关系,我都搜到了,下载导入项目,然后第一个例子跑通了。

第二个例子是从windows host向我的Vmware虚拟机Fedora上运行的伪分布式HDFS中拷贝一个文件,出错信息是org.apache.hadoop.ipc.RemoteException: Server IPC version 9 cannot communicate with client version 4

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;

class HDFSClient {

    static public void sampleHdfsWrite(String hdfs_url, String file) throws Exception {
         FileSystem hdfs = FileSystem.get(new URI(hdfs_url), new Configuration(), "root");
        Path localfile = new Path(file);
        Path hdfspath = new Path("/");
        hdfs.copyFromLocalFile(localfile, hdfspath);
    }

    static public void sampleHdfsRead(String hdfs_url, String file) throws Exception {
        FileSystem hdfs = FileSystem.get(new URI(hdfs_url), new Configuration(), "root");
        Path localfile = new Path("./" + file + ".backup");
        Path hdfspath = new Path("/" + file);
        hdfs.copyToLocalFile(hdfspath, localfile);
    }
     static public void main(String arg[]) throws Exception {
        sampleHdfsWrite("hdfs://192.168.150.149", "testfile.js");
        sampleHdfsRead("hdfs://192.168.150.149", "testfile.js");
    }
}

bing了一下大概意思是client的版本和Fedora中的版本不一致。
maven上Hadoop-Core这个jar包的版本是从0.20.x一直到1.2.x,而Fedora上的Hadoop是2.4.1(-2.7.2),我猜可能我根本就用错了包,可能这个hadoop-core是1.x代的hadoop。可能hadoop本身就是没有java client jar包的。我现在有2.7.1的hadoop-2.7.1.tar.gz,但是里面哪个jar才有我上面import的那些类呢?


你的项目不用Maven的话,手动去Maven的中央仓库找吧,地址:http://mvnrepository.com/

【热门文章】
【热门文章】