map->shuffle->reduce

map(k1,v1)--->(k2,v2)

reduce(k2,List<v2>)--->(k2,v3)

传输类型:org.apache.hadoop.io

访问HDFS文件系统

1.java.net.URL 的setURLStreamHandlerFactory() 方法。每个java虚拟机只能调用一次,因此通常在静态方法中调用。如果引用的第三方组件调用过,再次调用会报错。

public class App 
{
static{
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
} static InputStream inputStream=null;
public static void main( String[] args ) throws Exception
{
try{
inputStream=new URL(args[0]).openStream();
IOUtils.copyBytes(inputStream,System.out,4096,false);
}finally {
IOUtils.closeStream(inputStream);
}
}
}

2.FileSystem API 读取数据

public class App {
public static void main(String[] args) throws Exception {
String uri = args[];
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI(uri), configuration);
InputStream inputStream = null;
try {
inputStream = fs.open(new Path(uri));
IOUtils.copyBytes(inputStream, System.out, , false);
} finally {
IOUtils.closeStream(inputStream);
}
}
}
//实际上,FileSystem对象中open()方法返回的是FSDataInputStream对象。其实现了Seekable接口和PositionedReadable接口
public class FSDataInputStream extends DataInputStream implements Seekable, PositionedReadable, ByteBufferReadable, HasFileDescriptor, CanSetDropBehind, CanSetReadahead, HasEnhancedByteBufferAccess, CanUnbuffer {
}
public interface Seekable {
/**
* Seek to the given offset from the start of the file.
* The next read() will be from that location. Can't
* seek past the end of the file.
*/
void seek(long pos) throws IOException; /**
* Return the current offset from the start of the file
*/
long getPos() throws IOException; /**
* Seeks a different copy of the data. Returns true if
* found a new source, false otherwise.
*/
@InterfaceAudience.Private
boolean seekToNewSource(long targetPos) throws IOException;
} public interface PositionedReadable {
/**
* Read upto the specified number of bytes, from a given
* position within a file, and return the number of bytes read. This does not
* change the current offset of a file, and is thread-safe.
*/
public int read(long position, byte[] buffer, int offset, int length)
throws IOException; /**
* Read the specified number of bytes, from a given
* position within a file. This does not
* change the current offset of a file, and is thread-safe.
*/
public void readFully(long position, byte[] buffer, int offset, int length)
throws IOException; /**
* Read number of bytes equal to the length of the buffer, from a given
* position within a file. This does not
* change the current offset of a file, and is thread-safe.
*/
public void readFully(long position, byte[] buffer) throws IOException;
}

read()和readFully()的区别是readFully()在读取到length之前会阻塞,read()如果读到的小于length,读到多少返回多少。

seek()方法的开销较高,要谨慎使用。

3.写入数据

public class App {
public static void main(String[] args) throws Exception { String localSrc=args[0];
String dstSrc=args[1]; InputStream inputStream=new BufferedInputStream(new FileInputStream(localSrc)); Configuration configuration=new Configuration();
FileSystem fs=FileSystem.get(URI.create(dstSrc),configuration); OutputStream outputStream=fs.create(new Path(dstSrc), new Progressable() {
@Override
public void progress() {
System.out.print(".");
}
}); IOUtils.copyBytes(inputStream,outputStream,4096,true);
}
}

4.目录与查询

FileSystem提供mkdir方法创建目录。通常不需要,因为create方法写入文件时会自动创建目录

public boolean mkdirs(Path f) throws IOException {
}

FileSystem提供getFileStatus方法返回文件元数据。元数据包括文件的地址,大小,权限等

public abstract FileStatus getFileStatus(Path f) throws IOException;

FIleSystem提供listStatus()方法列出目录中的文件

public class App {
public static void main(String[] args) throws Exception { String uri = args[0];
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), configuration); Path[] paths = new Path[args.length]; for (int i = 0; i < paths.length; i++) {
paths[i] = new Path(args[i]);
} FileStatus[] status = fs.listStatus(paths);
Path[] listPaths = FileUtil.stat2Paths(status); for (Path path : listPaths) {
System.out.println(path);
}
}
}

FileSystem还提供globStatus方法返回与指定格式匹配的所有FIleStatus

 public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throws IOException ;

FileSystem提供delete方法永久删除文件或目录。如果f是一个空目录,recursive就会被忽略。如果f非空,只有在recursive为true时才会执行删除。

public abstract boolean delete(Path f, boolean recursive) throws IOException;

hadoop 学习笔记(第三章 Hadoop分布式文件系统 )的更多相关文章

  1. Hadoop学习笔记(三):分布式文件系统的写和读流程

    写流程:怎么将文件切割成块,上传到服务器 读流程:怎么从不同的服务器来读取数据块 写流程 图一 图二 写的过程中:NameNode会给块分配存储块的位置,每次想要存储文件的时候都会在NameNode创 ...

  2. Hadoop学习笔记(6) ——重新认识Hadoop

    Hadoop学习笔记(6) ——重新认识Hadoop 之前,我们把hadoop从下载包部署到编写了helloworld,看到了结果.现是得开始稍微更深入地了解hadoop了. Hadoop包含了两大功 ...

  3. 《DOM Scripting》学习笔记-——第三章 DOM

    <Dom Scripting>学习笔记 第三章 DOM 本章内容: 1.节点的概念. 2.四个DOM方法:getElementById, getElementsByTagName, get ...

  4. The Road to learn React书籍学习笔记(第三章)

    The Road to learn React书籍学习笔记(第三章) 代码详情 声明周期方法 通过之前的学习,可以了解到ES6 类组件中的生命周期方法 constructor() 和 render() ...

  5. [HeadFrist-HTMLCSS学习笔记]第三章构建模块:Web页面建设

    [HeadFrist-HTMLCSS学习笔记]第三章构建模块:Web页面建设 敲黑板!! <q>元素添加短引用,<blockquote>添加长引用 在段落里添加引用就使用< ...

  6. JVM学习笔记-第三章-垃圾收集器与内存分配策略

    JVM学习笔记-第三章-垃圾收集器与内存分配策略 tips:对于3.4之前的章节可见博客:https://blog.csdn.net/sanhewuyang/article/details/95380 ...

  7. [BigData]关于Hadoop学习笔记第三天(PPT总结)(一)

     课程安排 MapReduce原理*** MapReduce执行过程** 数据类型与格式*** Writable接口与序列化机制*** ---------------------------加深拓展- ...

  8. hadoop学习笔记(五)hadoop伪分布式集群的搭建

    本文原创,如需转载,请注明作者和原文链接 1.集群搭建的前期准备   见      搭建分布式hadoop环境的前期准备---需要检查的几个点 2.解压tar.gz包 [root@node01 ~]# ...

  9. [hadoop读书笔记] 第三章 HDFS

    P49 当数据集的大小超过一台计算机存储能力时,就有必要对数据集分区(partition)并将分区存储到若干台独立的计算机上. 管理网络中跨多台计算机存储的系统就叫分布式文件系统  Distribut ...

  10. Hadoop学习笔记(三):java操作Hadoop

    1. 启动hadoop服务. 2. hadoop默认将数据存储带/tmp目录下,如下图: 由于/tmp是linux的临时目录,linux会不定时的对该目录进行清除,因此hadoop可能就会出现意外情况 ...

随机推荐

  1. sqlserver 获取所有表的字段类型等信息

    USE [MultipleAnalysisDataFY] GO /****** Object: View [dbo].[selectfieldtype] Script Date: 2018/11/7 ...

  2. 论文阅读笔记:《Contextual String Embeddings for Sequence Labeling》

    文章引起我关注的主要原因是在CoNLL03 NER的F1值超过BERT达到了93.09左右,名副其实的state-of-art.考虑到BERT训练的数据量和参数量都极大,而该文方法只用一个GPU训了一 ...

  3. 20175204 张湲祯 2018-2019-2《Java程序设计》第四周学习总结

    20175204 张湲祯 2018-2019-2<Java程序设计>第四周学习总结 教材学习内容总结 -第五章子类与继承要点: -子类与父类: 1.通过使用关键字extends来定义一个类 ...

  4. Java基础10-集合

    作业回顾 蜜蜂和熊的生产消费关系,熊在蜂蜜满10斤吃掉.蜜蜂一次生产一斤蜂蜜,且蜜蜂生成一斤蜂蜜花费的时间是10s. 十只蜜蜂和两只熊. 蜜蜂 bag: 20 每次产1,耗时10ms 满5的时候给蜜罐 ...

  5. vim配置(使用Vundle)

    1.前言 Vim的配置文件位于~/.vimrc,文件使用VimScript语法来编写. 2. vim插件管理 Vundle是一个全自动的插件管理器,让我们通过维护插件列表的方式管理插件.它为安装.更新 ...

  6. 【算法】K最近邻算法(K-NEAREST NEIGHBOURS,KNN)

    K最近邻算法(k-nearest neighbours,KNN) 算法 对一个元素进行分类 查看它k个最近的邻居 在这些邻居中,哪个种类多,这个元素有更大概率是这个种类 使用 使用KNN来做两项基本工 ...

  7. php 两变量值互换 方法

    //方法一:$a ="abc";$b="def"; $a = $a^$b;$b = $b^$a;$a = $a^$b; //方法二:list($a, $b)= ...

  8. Python-Django基础

    django目录 -settings -urls -views ******强调:setting中'django.middleware.csrf.CsrfViewMiddleware'中间件先注释掉 ...

  9. yagmail 实现发邮件

    yagmail 实现发邮件 yagmail 可以更简单的来实现自动发邮件功能. github项目地址: https://github.com/kootenpv/yagmail 安装 pip insta ...

  10. 使用mysqlslap进行MySQL压力测试

    使用mysqlslap进行MySQL压力测试发表于236 天前 ? MySQL ? 暂无评论 MySQL从5.1.4版开始带有一个压力测试工具mysqlslap,通过模拟多个并发客户端访问mysql来 ...