hadoop文件读写示例
import java.io.InputStream;import java.net.URI; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils; /** * @author feixu */public class FileSystemCat { /** * main * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String uri = args[0]; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri), conf); InputStream in = null; try { in = fs.open(new Path(uri)); IOUtils.copyBytes(in, System.out, 4096, false); } finally { IOUtils.closeStream(in); } } }
2 自行在本地文件系统生成一个大约100字节的文本文件,写一段程序,读入这个文件,并将其第101-120字节的内容写入HDFS成为一个新文件
import java.net.URI; 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.apache.hadoop.io.IOUtils; /** * @author feixu * @see FSDataInputStream * @see FSDataOutputStream */public class WriteToHdfs { /** * This method is for reading 101~120 bytes from local file and write to hdfs * @param args which arg[0] should be local disk uri and arg[1] should be hdfs uri * @throws Exception */ public static void main(String[] args) throws Exception{ //hdfs uri String dst = args[1]; //get hdfs file system instance Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(dst), conf); FSDataOutputStream out = null; try{ //open hdfs output stream out = fs.create(new Path(dst)); //write the data from local disk to hdfs out.write(readFromLocal(args)); }finally{ IOUtils.closeStream(out); } } /** * readFromLocal which read 101~120 bytes from the local disk file * @param args * @return * @throws Exception */ private static byte[] readFromLocal(String[] args) throws Exception{ //local disk uri String uri = args[0]; //get local disk file system instance Configuration conf = new Configuration(); FileSystem fs = FileSystem.getLocal(conf); FSDataInputStream in = null; try{ //open local disk input stream in = fs.open(new Path(uri)); //read 101~120 bytes byte[] buffer = new byte[1024]; in.read(101, buffer, 0, 20); return buffer; }finally{ IOUtils.closeStream(in); } }}
3 反向操作,在HDFS中生成一个大约100字节的文本文件,写一段程序,读入这个文件,并将其第101-120字节的内容写入本地文件系统成为一个新文件
import java.net.URI; 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.apache.hadoop.io.IOUtils; /** * @author feixu * @see FSDataInputStream * @see FSDataOutputStream * */public class WriteToLocal { /** * This method is for reading 101~120 bytes from hdfs file and write to local disk * @param args which arg[0] should be hdfs uri and arg[1] should be local disk uri * @throws Exception */ public static void main(String[] args) throws Exception{ //local disk uri String dst = args[1]; //get local file system instance Configuration conf = new Configuration(); FileSystem fs = FileSystem.getLocal(conf); FSDataOutputStream out = null; try{ //open local file's output stream out = fs.create(new Path(dst)); //write the data from hdfs to local disk file out.write(readFromHdfs(args)); }finally{ IOUtils.closeStream(out); } } /** * readFromHdfs which read 101~120 bytes from the hdfs file * @param args * @return byte[] * @throws Exception */ public static byte[] readFromHdfs(String[] args) throws Exception{ //hdfs uri String uri = args[0]; //get hdfs file system instance Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri), conf); FSDataInputStream in = null; try{ //open hdfs input stream in = fs.open(new Path(uri)); //read 101~120 bytes byte[] buffer = new byte[1024]; in.read(101, buffer, 0, 20); return buffer; }finally{ IOUtils.closeStream(in); } } }
hadoop文件读写示例相关推荐
- java流读写_java流概述以及文件读写示例
1. 先分清楚是字节流还是字符流. 字节流:InputStream OutputStream 字符流:Reader Writer 字符流与字节流的区别是读取的单位长度不同,字节流读8字节,字符流读16 ...
- Qt下实现XML、INI、JSON的文件读写示例开发
文章目录 前言 一.XML文件 二.INI文件 三.JSON文件 四.示例代码及文件内容 五.下载链接 总结 前言 在项目开发过程中,发现经常遇见一些比如XML.INI.JSON等文件的读写,这里对这 ...
- linux 目录 读写,Linux C 文件与目录3 文件读写(示例代码)
文件读写 文件读写是指从文件中读出信息或将信息写入到文件中.Linux文件读取可使用read函数来实现的,文件写入可使用write函数来实现.在进行文件写入的操作时,只是在文件的缓冲区中操作,可能没有 ...
- php 跨进程读写,php使用多个进程同时控制文件读写示例
代码如下: /** * 写入数据 * @param [string] $path [文件路径] * @param [string] $mode [文件打开模式] * @param [string ...
- php多文件读写,php使用多个进程同时控制文件读写示例
代码如下: /** * 写入数据 * @param [string] $path [文件路径] * @param [string] $mode [文件打开模式] * @param [string ...
- 《Essential C++》笔记之文件读写示例
一.文件写入 // 小问学编程 #include<iostream> #include<fstream>using namespace std;int main() {stri ...
- 读写c语言编程,c语言文件读写示例(c语言文件操作)
long filesize(char* filename); char* file_get_contents(char* filename); void file_put_contents(char* ...
- 《Python数据分析基础教程:NumPy学习指南(第2版)》笔记5:第三章 常用函数1——文件读写、算术平均值、最大值最小值、极值
本章将介绍NumPy的常用函数.具体来说,我们将以分析历史股价为例,介绍怎样从文件中载入数据,以及怎样使用NumPy的基本数学和统计分析函数.这里还将学习读写文件的方法,并尝试函数式编程和NumPy线 ...
- Hadoop 系列 HDFS:分布式文件系统(HDFS文件读写)
HDFS:分布式文件系统 HDFS文件读写 文件访问权限 针对文件和目录,HDFS有与POSIX非常相似的权限模式. 一共提供三类权限模式:只读权限(r).写入权限(w)和可执行权限(x).读取文件或 ...
最新文章
- Windows服务安装、卸载方法,卸载后在服务列表中仍显示问题,指定的服务已经标记为删除
- fanuc机器人码垛编程实例_FANUC 机器人码垛编程详细讲解 记得收藏!
- mysql数据库文件位置
- 用kotlin方式打开《第一行代码:Android》之开发酷欧天气(2)
- Fission:基于 Kubernetes 的 Serverless 函数框架
- LeetCode.206. Reverse Linked List(反转有序链表)C++ and PYTHON
- Caffe 的深度学习训练全过程
- Google play谷歌应用商店 APP上包上架的一些策略和技巧
- 分布式统一框架的设计与实现(数据库)
- 82、组合分配式气体灭火系统所需的气体单向阀的数量
- 为什么打印还要另存为_为什么打印图片时出现文件另存为
- 查找同一网络的计算机,怎么从网络中查询另一台计算机的ip地址
- sc-RNA seq与Illumina测序
- 优化理论12---- Rosen的梯度投影法 、投影矩阵
- 敏捷已死:一场程序员们历经20年的失败反叛
- 在滴滴云上搭建 API-Gateway Kong 实践
- Strange Fractions(奇怪的分数)-数论
- Android so文件函数加密
- IT过来人的10点经验谈
- opa example