hadoop Hdfs文件上传下载

来源:互联网 发布:手机淘宝4.0 编辑:程序博客网 时间:2024/05/22 05:12
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;@Beforepublic void getFs() throws IOException{//get a configuration objectConfiguration conf = new Configuration();//to set a parameter, figure out the filesystem is hdfsconf.set("fs.defaultFS", "hdfs://yun12-01:9000/");conf.set("dfs.replication","1");//get a instance of HDFS FileSystem Clientfs = FileSystem.get(conf);}@Testpublic 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 hdfspublic static void main(String[] args) throws IOException {//get a configuration objectConfiguration 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 ClientFileSystem fs = FileSystem.get(conf);//open a outputstream of the dest filePath destFile = new Path("hdfs://itcast:9000/jdk.tgz");FSDataOutputStream os = fs.create(destFile);//open a inputstream of the local source fileFileInputStream is = new FileInputStream("/home/hadoop/mysoft/jdk-6u25-linux-i586.bin");//write the bytes in "is" to "os" .copy(is, os);}}

0 0