java怎么连接hdfs文件系统,需要哪些包?

我最近在做一个(hadoop)云盘客户端,客户端准备用java编写,hadoop平台已经在linux系统上搭建成功,可是不知道怎么样才能在windows上用java连接上hdfs,实现上传下载的功能。求赐教!

apache的Hadoop项目提供一类api可以通过java工程操作hdfs中的文件,包括:文件打开,读写,删除等、目录的创建,删除,读取目录中所有文件等。
1、到http://hadoop.apache.org/releases.html下载Hadoop,解压后把所有jar加入项目的lib里
2、程序处理步骤: 1)得到Configuration对象,2)得到FileSystem对象,3)进行文件操作,简单示例如下:
/**
*
*/
package org.jrs.wlh;

import java.io.IOException;

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

/**
* @PutMeger.java
* java操作hdfs 往 hdfs中上传数据
* @version $Revision$</br>
* update: $Date$
*/
public class PutMeger {

public static void main(String[] args) throws IOException {

String[] str = new String[]{"E:\\hadoop\\UploadFileClient.java","hdfs://master:9000/user/hadoop/inccnt.java"};
Configuration conf = new Configuration();
FileSystem fileS= FileSystem.get(conf);
FileSystem localFile = FileSystem.getLocal(conf); //得到一个本地的FileSystem对象

Path input = new Path(str[0]); //设定文件输入保存路径
Path out = new Path(str[1]); //文件到hdfs输出路径

try{
FileStatus[] inputFile = localFile.listStatus(input); //listStatus得到输入文件路径的文件列表
FSDataOutputStream outStream = fileS.create(out); //创建输出流
for (int i = 0; i < inputFile.length; i++) {
System.out.println(inputFile[i].getPath().getName());
FSDataInputStream in = localFile.open(inputFile[i].getPath());

byte buffer[] = new byte[1024];
int bytesRead = 0;
while((bytesRead = in.read(buffer))>0){ //按照字节读取数据
System.out.println(buffer);
outStream.write(buffer,0,bytesRead);
}

in.close();
}

}catch(Exception e){
e.printStackTrace();
}
}

}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答