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); } }
相关文章推荐
- UVa 11078 - Open Credit System(维护最大值)
- centos7 安装R和Rstudio客户端
- Linux之GTK系列教程
- 新浪微博架构
- Openssl rand命令
- OPENCV 学习例程(1)图片读写 色彩空间转换
- JAVA & PHP 结合 开发系统网站
- Openssl oscp命令
- 介紹好用工具:RichCopy (檔案複製與備份的絕佳工具)
- opencv+pcl配置
- Openssl pkcs12命令
- iis中默认网站被误删了,EXCHANGE的OWA不能访问了,请问怎么修复?
- 目前Lodop支持IE系列、IE内核系列(遨游、360、世界之窗、腾讯TT、搜狗等)浏览器,以及Firefox(火狐)系列、Chrome(谷歌)系列、Opera系列、Safari系列等Windows下
- [AD]查看森林中的架构主控和五种角色属主
- 大概CentOS7 安装LAMP的步骤
- linux c 获取当前运行进程总数
- [转]GStreamer资料(摄像头采集,视频保存,远程监控)DVR
- Linux中的BSS段、数据段、堆栈段和代码段
- 提高网站的性能
- linux下ok6410驱动开发环境搭建基本教程