使用DFSClient实现 hadoop上传文件功能,采用使用输入输出流实现 Hadoop 版本2.7.0
2016-06-27 10:44
645 查看
Hadoop 版本2.7.0
使用DFSClient实现hadoop上传文件功能,采用使用输入输出流实现。
另外说明:代码运行环境中需要配置 /etc/hosts
添加
10.11.12.45 master
否则无法识别 hdfs://master:9000 报错!
使用DFSClient实现hadoop上传文件功能,采用使用输入输出流实现。
另外说明:代码运行环境中需要配置 /etc/hosts
添加
10.11.12.45 master
否则无法识别 hdfs://master:9000 报错!
package com.feng.test.hdfs; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.hdfs.DFSClient; public class UploadFile { /** * @param args */ public static void main(String[] args) { String hostname = "master"; int port = 9000; String destFile = "/feng/83.txt"; String fsrc = "/Users/Feng/Development/hadoopworkspace/TestHadoop/files/83.txt"; FileInputStream fis = null; OutputStream outputStream = null; DFSClient client = null; try{ //构造 Configuration Configuration config = new Configuration(); //程序配置 config.set("hadoop.job.ugi", "feng,111111"); config.set("fs.default.name", "hdfs://master:9000"); //config.set("hadoop.tmp.dir", "/tmp/hadoop-yshengClient"); config.set("dfs.replication", "1"); config.set("mapred.job.tracker", "master:9001"); //构造DFClient InetSocketAddress isa = new InetSocketAddress(hostname, port); client = new DFSClient(isa, config); //获取输出流 boolean isWrite = true; outputStream = client.create(destFile, isWrite); File file = new File(fsrc); if(file.exists()){ fis = new FileInputStream(file); byte[] buff = new byte[1024]; int readCount = 0; readCount = fis.read(buff); while (readCount != -1) { outputStream.write(buff, 0, readCount); readCount = fis.read(buff); } } outputStream.flush(); System.out.println("success end!"); }catch(Exception e){ e.printStackTrace(); }finally{ if(outputStream != null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(client != null){ try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
相关文章推荐
- POP Animation 和 layoutSubviews 的冲突
- centos7 apache2.4 测试
- linux目录审计
- Linux 命令
- win2003 sp2+iis 6.0上部署.net 2.0和.net 4.0网站的方法
- linux下的crontab服务
- 使用POI和DynaBean导出Excel的完整例子
- 初学CentOS——chmod命令
- CentOS7.2安装MySQL5.7及遇到的坑
- nginx+php_php-fpm编译版
- Hadoop HA 搭建
- Docker实战(十五):Docker安装Hive环境
- Docker实战(十四):Docker安装Hadoop环境
- Docker实战(十三):Docker安装MySQL数据库
- 用date命令给CentOS Linux设置修改系统日期和时间的方法
- Linux配置防火墙 开启80端口的方法
- Docker实战(十二):Docker安装ElasticSearch集群环境
- Docker实战(十一):Docker安装ELK环境(二)
- Docker实战(十一):Docker安装ELK环境(二)
- linux ip 配置及配置保存