您的位置:首页 > 运维架构

hadoop Hdfs文件上传下载

2015-07-25 11:00 429 查看
package cn.itheima.bigdata.hadoop.hdfs;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;

public class HdfsClient {

private FileSystem fs = null;

@Before
public void getFs() throws IOException{

//get a configuration object
Configuration conf = new Configuration();
//to set a parameter, figure out the filesystem is hdfs
conf.set("fs.defaultFS", "hdfs://yun12-01:9000/");
conf.set("dfs.replication","1");

//get a instance of HDFS FileSystem Client
fs = FileSystem.get(conf);

}

@Test
public void testDownload() throws IllegalArgumentException, IOException{

FSDataInputStream is = fs.open(new Path("hdfs://yun12-01:9000/jdk.tgz"));

FileOutputStream os = new FileOutputStream("/home/hadoop/jdk.download");

IOUtils.copy(is, os);

}

//upload a local file to hdfs
public static void main(String[] args) throws IOException {

//get a configuration object
Configuration conf = new Configuration();
//to set a parameter, figure out the filesystem is hdfs
/*
相当于在
*core-site.xml
configuration节点中添加
<!-- 指定HADOOP所使用的文件系统schema(URI),HDFS的老大(NameNode)的地址 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://weekend-1206-01:9000</value>   #weekend-1206-01:虚拟机的主机名
</property>
*/
conf.set("fs.defaultFS", "hdfs://itcast:9000/");
/*
相当于在
hdfs-site.xml   hdfs-default.xml  (3)
<!-- 指定HDFS副本的数量 -->
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
*/
conf.set("dfs.replication","1");

//get a instance of HDFS FileSystem Client
FileSystem fs = FileSystem.get(conf);

//open a outputstream of the dest file
Path destFile = new Path("hdfs://itcast:9000/jdk.tgz");
FSDataOutputStream os = fs.create(destFile);

//open a inputstream of the local source file
FileInputStream is = new FileInputStream("/home/hadoop/mysoft/jdk-6u25-linux-i586.bin");

//write the bytes in "is" to "os"
.copy(is, os);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: