io 流操作hdfs
hdfs 文件上传
本地 --------> 文件系统对象 --------> hdfs 文件系统
输入流 输出流
// 将流从本地 上传到 hdfs 文件系统。
@Test
public void ioPut() throws IOException, InterruptedException, URISyntaxException{
//1 获取文件系统对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs"); //2 输出流 流到 hdfs
FSDataOutputStream fo = fs.create(new Path("/user/ljs/job")); //3 定义输出流 来自 本地磁盘
InputStream is = new FileInputStream(new File("d:/job.txt")); //4 流对接
IOUtils.copyBytes(is, fo, ); IOUtils.closeStream(fs);
//5 关闭流
}
hdfs 文件下载
本地 <----------- 文件系统对象 < -------------------- hdfs文件系统 集群
输出流 输出流
@Test
public void ioGet() throws IOException, InterruptedException, URISyntaxException{ // 1 创建文件系统
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"),
conf, "ljs");
// 2 创建 输入流 来着 hdfs 文件系统
FSDataInputStream fis = fs.open(new Path("/user/ljs/cook.txt"));
// 3 创建输出流 送到 本地磁盘。
OutputStream os = new FileOutputStream(new File("d:/cook.txt"));
// 4 流对接
IOUtils.copyBytes(fis, os, );
// 5 关闭流
IOUtils.closeStream(fis);
}
定位文件读取
读取第一块 128M
// 从hdfs文件系统中获取第一块block (128M)
@Test
public void fileSeek1() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
// 2 创建打开输入流
FSDataInputStream fis = fs.open(new Path("/user/ljs/hadoop-2.7.2.tar.gz"));
// 3 创建 输出流
OutputStream fos = new FileOutputStream(new File("d:/hadoop-2.7.2.tar.gz.part01"));
// 4 流对接
byte [] buff = new byte[]; // 1kB;
for(int i =; i < *; ++i){
fis.read(buff);
fos.write(buff);
}
// 5 关闭流
fis.close();
fos.close();
}
从第二块文件开始读取
@Test
public void fileSeek02() throws IOException, InterruptedException, URISyntaxException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
FSDataInputStream fis = fs.open(new Path("/user/ljs/hadoop-2.7.2.tar.gz"));
OutputStream fos = new FileOutputStream(new File("d:/hadoop-2.7.2.tar.gz.part02")); fis.seek(1024*1024*128);
IOUtils.copyBytes(fis, fos,1024); fis.close();
fos.close(); }
io 流操作hdfs的更多相关文章
- Hadoop基础-通过IO流操作HDFS
Hadoop基础-通过IO流操作HDFS 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.上传文件 /* @author :yinzhengjie Blog:http://www ...
- Java IO流操作汇总: inputStream 和 outputStream【转】
我们在进行Android java 开发的时候,经常会遇到各种IO流操作.IO流操作一般分为两类:字符流和字节流.以“Reader”结尾都是字符流,操作的都是字符型的数据:以“Stream”结尾的都是 ...
- IO流----操作文件的9种方法代码实现
IO流----操作文件的9种方法代码实现: 1:使用字节流读写数据: 四种方式: method1: 每次读写一个字节,边读边写: /* * 复制文本文件. * * 数据源:从哪里来 ...
- python IO流操作
python IO流操作 学习完本篇,你将会独立完成 实现操作系统中文件及文件目录的拷贝功能. 将目标图片拷贝到指定的目录中 实现一个自动阅卷程序, Right.txt保存正确答案,xx(学生姓名). ...
- io流操作大全
JAVA 中的IO流 一.流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部 ...
- java+++IO流操作
序:IO流的操作主要分为两种读和写.一方面:我们可以通过不加缓冲类字符流BufferedReader/Writer和字节流BufferedInputStream/OutputStream来进行简单的读 ...
- IO流操作详解
注:FileReader继承InputStreamReader类,InputStreamReader实现Reader接口,其他同理. 对于文件内容的操作主要分为两大类 分别是: 字符流 字节流 其中, ...
- Java 常用IO流操作详解
1.基本概念 IO:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中. 2.IO流的分类 图示:(主要IO流) 3.字节流 ...
- Java IO流操作 (I)
1.FileWriter 写数据---输出流---由控制台向文件中写数据 import java.io.FileWriter; import java.io.IOException; /* * 写数据 ...
随机推荐
- ios中输入框的父类--文本框,DataPick,pickerview
父控制器 #import <UIKit/UIKit.h> #import "ScrollViewExt.h" @interface BaseKeyBoardCtrl : ...
- Backbone hello world
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- linux删除软连接
#mkdir test_chk #touch test_chk/test.txt #vim test_chk/test.txt (这一步随便在这个test.txt里写点东东即可) 下面我们来创建tes ...
- Atitit 翻页功能的解决方案与版本历史 v4 r49
Atitit 翻页功能的解决方案与版本历史 v4 r49 1. 版本历史与分支版本,项目版本记录1 1.1. 主干版本历史1 1.2. 分支版本 项目版本记录.1 2. Easyui 的翻页组件2 ...
- [INS-20802] Oracle Net Configuration Assistant failed
[INS-20802] Oracle Net Configuration Assistant failed.在安装Oracle 11g R2时出现了该错误提示.以前安装的时候没有碰到过类似的错误.原来 ...
- MapReduce教程(二)MapReduce框架Partitioner分区<转>
1 Partitioner分区 1.1 Partitioner分区描述 在进行MapReduce计算时,有时候需要把最终的输出数据分到不同的文件中,按照手机号码段划分的话,需要把同一手机号码段的数据放 ...
- mariadb 10.1.26 二进制包安装笔记
报错链接:http://php.upupw.net/ank2discuss/40/6841-e.html mariadb 10.1.26 二进制版本 全名称: mariadb-10.1.26-linu ...
- 【消息】Pivotal Pivots 开源大数据处理的核心组件
Pivotal Pivots 开源大数据处理的核心组件 Pivotal 今天宣布将其大数据套件的三个核心组件开源,同时商业版本继续提供更高级特性和商业支持服务. 这三个开源的组件分别是: GemFir ...
- Implementing HTTPS Everywhere in ASP.Net MVC application.
Implementing HTTPS Everywhere in ASP.Net MVC application. HTTPS everywhere is a common theme of the ...
- linux memcached
依赖库 yum install libevent libevent-deve 云安装 yum install memcached 源代码安装 wget http://memcached.org/lat ...