Hadoop的FileSystem 文件系统实现上传下载:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
public class UploadAndDown {
public static void main(String[] args) {
UploadAndDown uploadAndDown = new UploadAndDown();
try {
//将本地文件local.txt上传为HDFS上cloud.txt文件
uploadAndDown.upLoadToCloud("local.txt", "cloud.txt");
//将HDFS上的cloud.txt文件下载到本地cloudTolocal.txt文件
uploadAndDown.downFromCloud("cloudTolocal.txt", "cloud.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void upLoadToCloud(String srcFileName, String cloudFileName)
throws FileNotFoundException, IOException {
// 本地文件存取的位置
String LOCAL_SRC = "/home/linuxidc/hbase2/bin/" + srcFileName;
// 存放到云端HDFS的位置
String CLOUD_DEST = "hdfs://localhost:9000/user/linuxidc/" + cloudFileName;
InputStream in = new BufferedInputStream(new FileInputStream(LOCAL_SRC));
// 获取一个conf对象
Configuration conf = new Configuration();
// 文件系统
FileSystem fs = FileSystem.get(URI.create(CLOUD_DEST), conf);
// 输出流
OutputStream out = fs.create(new Path(CLOUD_DEST), new Progressable() {
@Override
public void progress() {
System.out.println("上传完成一个文件到HDFS");
}
});
// 连接两个流,形成通道,使输入流向输出流传输数据
IOUtils.copyBytes(in, out, 1024, true);
}
private void downFromCloud(String srcFileName, String cloudFileName) throws FileNotFoundException, IOException {
// 云端HDFS上的文件
String CLOUD_DESC = "hdfs://localhost:9000/user/linuxidc/"+cloudFileName;
// down到本地的文件
String LOCAL_SRC = "/home/linuxidc/hbase2/bin/"+srcFileName;
// 获取conf配置
Configuration conf = new Configuration();
// 实例化一个文件系统
FileSystem fs = FileSystem.get(URI.create(CLOUD_DESC), conf);
// 读出流
FSDataInputStream HDFS_IN = fs.open(new Path(CLOUD_DESC));
// 写入流
OutputStream OutToLOCAL = new FileOutputStream(LOCAL_SRC);
// 将InputStrteam 中的内容通过IOUtils的copyBytes方法复制到OutToLOCAL中
IOUtils.copyBytes(HDFS_IN, OutToLOCAL, 1024, true);
}
}
在本地建立一个local.txt文件,运行个java程序,在eclipse下看到HDFS文件系统多了一个cloud.txt
同样也可以用hadoop的命令察看
linuxidc@Ubuntu:~/hadoop/bin$ hadoop fs -ls
Found 4 items
-rw-r--r-- 3 linuxidc supergroup 11 2012-12-23 23:23 /user/linuxidc/Google.pdf
-rw-r--r-- 3 linuxidc supergroup 61 2012-12-23 23:59 /user/linuxidc/cloud.txt
-rw-r--r-- 3 linuxidc supergroup 13 2012-12-23 22:49 /user/linuxidc/demo.txt
drwxr-xr-x - linuxidc supergroup 0 2012-11-18 15:17 /user/linuxidc/docs
同时在本地多了个cloudTolocal.txt文件: