`

hadoop hdfs的一些用法

 
阅读更多

Example 3-1. Displaying files from a Hadoop filesystem on standard output using a 
URLStreamHandler 

Java代码  收藏代码
  1. //Reading Data from a Hadoop URL  
  2.   
  3. public class URLCat {  
  4.     static {  
  5.         URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());  
  6.     }  
  7.   
  8.     public static void main(String[] args) throws Exception {  
  9.         InputStream in = null;  
  10.         try {  
  11.             in = new URL(args[0]).openStream();  
  12.             IOUtils.copyBytes(in, System.out, 4096false);  
  13.         } finally {  
  14.             IOUtils.closeStream(in);  
  15.         }  
  16.     }  
  17. }  
  18. -----------------------------------------  
  19. result:  
  20. Here’s a sample run:  
  21. % hadoop URLCat hdfs://localhost/user/tom/quangle.txt  
  22. On the top of the Crumpetty Tree  
  23. The Quangle Wangle sat,  
  24. But his face you could not see,  
  25. On account of his Beaver Hat.  


Example 3-2. Displaying files from a Hadoop filesystem on standard output by using the FileSystem 
directly 
Java代码  收藏代码
  1. public class FileSystemCat {  
  2.     public static void main(String[] args) throws Exception {  
  3.         String uri = args[0];  
  4.         Configuration conf = new Configuration();  
  5.         FileSystem fs = FileSystem.get(URI.create(uri), conf);  
  6.         InputStream in = null;  
  7.         try {  
  8.             in = fs.open(new Path(uri));  
  9.             IOUtils.copyBytes(in, System.out, 4096false);  
  10.         } finally {  
  11.             IOUtils.closeStream(in);  
  12.         }  
  13.     }  
  14. }  
  15.   
  16. ------------------------------------------  
  17. The program runs as follows:  
  18. % hadoop FileSystemCat hdfs://localhost/user/tom/quangle.txt  
  19. On the top of the Crumpetty Tree  
  20. The Quangle Wangle sat,  
  21. But his face you could not see,  
  22. On account of his Beaver Hat.  
  23. The  


Example 3-3 is a simple extension of Example 3-2 that writes a file to standard out 
twice: after writing it once, it seeks to the start of the file and streams through it once 
again. 

Java代码  收藏代码
  1. //Example 3-3. Displaying files from a Hadoop filesystem on standard output twice, by using seek  
  2.   
  3. public class FileSystemDoubleCat {  
  4.     public static void main(String[] args) throws Exception {  
  5.         String uri = args[0];  
  6.         Configuration conf = new Configuration();  
  7.         FileSystem fs = FileSystem.get(URI.create(uri), conf); //通过get()方法获得一个FileSystem流  
  8.         FSDataInputStream in = null;  
  9.         try {  
  10.             in = fs.open(new Path(uri)); //通过open()方法打开一个FSDataInputStream流  
  11.             IOUtils.copyBytes(in, System.out, 4096false);  
  12.             in.seek(0); // go back to the start of the file  
  13.             IOUtils.copyBytes(in, System.out, 4096false);  
  14.         } finally {  
  15.             IOUtils.closeStream(in);  
  16.         }  
  17.     }  
  18. }  
  19. ----------------------------------------------------  
  20. Here’s the result of running it on a small file:  
  21. % hadoop FileSystemDoubleCat hdfs://localhost/user/tom/quangle.txt  
  22. On the top of the Crumpetty Tree  
  23. The Quangle Wangle sat,  
  24. But his face you could not see,  
  25. On account of his Beaver Hat.  
  26. On the top of the Crumpetty Tree  
  27. The Quangle Wangle sat,  
  28. But his face you could not see,  
  29. On account of his Beaver Hat.  


Example 3-4 shows how to copy a local file to a Hadoop filesystem. We illustrate progress 
by printing a period every time the progress() method is called by Hadoop, which 
is after each 64 K packet of data is written to the datanode pipeline. (Note that this 
particular behavior is not specified by the API, so it is subject to change in later versions 
of Hadoop. The API merely allows you to infer that “something is happening.”) 

Java代码  收藏代码
  1. //Example 3-4. Copying a local file to a Hadoop filesystem, and shows progress  
  2. public class FileCopyWithProgress {  
  3.     public static void main(String[] args) throws Exception {  
  4.         String localSrc = args[0];  
  5.         String dst = args[1];  
  6.         InputStream in = new BufferedInputStream(new FileInputStream(localSrc));  
  7.         Configuration conf = new Configuration();  
  8.         FileSystem fs = FileSystem.get(URI.create(dst), conf);  
  9.         OutputStream out = fs.create(new Path(dst), new Progressable() {  
  10.             public void progress() {  
  11.                 System.out.print(".");  
  12.             }  
  13.         });  
  14.         IOUtils.copyBytes(in, out, 4096true);  
  15.     }  
  16. }  
  17.   
  18. Typical usage:  
  19. % hadoop FileCopyWithProgress input/docs/1400-8.txt hdfs://localhost/user/tom/1400-8.txt  
  20. ...............  

分享到:
评论

相关推荐

    java整合spring和hadoop HDFS全部jar

    同时,考虑性能优化,比如使用HDFS的批量操作,或者根据业务需求调整Hadoop的配置参数。 5. **异常处理与日志记录**:在处理分布式系统时,异常处理和日志记录至关重要。确保捕获可能的异常并适当地记录,以便于...

    python 操作 Hadoop hdfs

    在大数据处理领域,Hadoop HDFS(Hadoop Distributed File System)是广泛使用的分布式文件系统,它为大规模数据处理提供了高效、可靠的数据存储解决方案。而Python作为一种灵活易用的编程语言,常常被用来与Hadoop ...

    hadoop-hdfs-2.7.3-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    《HDFS——Hadoop分布式文件系统深度实践》PDF

    《HDFS——Hadoop分布式文件系统深度实践》这本书是针对Hadoop分布式文件系统(HDFS)的详尽指南,旨在帮助读者深入理解HDFS的工作原理、设计思想以及在实际应用中的最佳实践。HDFS是Apache Hadoop项目的核心组件之...

    java 从hadoop hdfs读取文件 进行groupby并显示为条形图

    3. **从HDFS读取文件**:使用`FileSystem`类的`open()`方法可以打开HDFS中的文件,然后通过`FSDataInputStream`读取内容。数据通常是以文本格式存储,如CSV或TSV,便于解析成Java对象。 4. **数据解析与预处理**:...

    hadoop-hdfs-2.6.5-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-2.6.5.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    hadoop-hdfs-client-2.9.1-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-client-2.9.1.jar ...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    hadoop-hdfs-fsimage-exporter:将Hadoop HDFS内容统计信息导出到Prometheus

    Prometheus Hadoop HDFS FSImage导出器 | 将Hadoop HDFS统计信息导出到包括 总数/每个用户/每个组/每个配置的目录路径/每个路径集 目录数 文件数 文件大小和大小分布(可选) 块数 文件复制(总体/每个用户摘要)...

    I001-hadoophdfs-mkdirs.7z

    这个"I001-hadoophdfs-mkdirs"压缩包可能包含详细的Hadoop HDFS使用指南,关于`mkdirs`命令的用法示例,以及如何在实际大数据项目中运用这些知识的实践案例。学习这些内容对于理解和操作Hadoop集群至关重要,特别是...

    Hadoop HDFS_Shell命令详解.pdf

    **使用方法**: `hadoop fs -chmod [-R] [,MODE]|OCTALMODE> URI [URI...]` 该命令用于更改文件的权限。如果使用 `-R` 选项,则递归地更改目录结构中所有文件的权限。 - **要求**: 执行该命令的用户必须是文件的所有...

    hadoop-hdfs-client-2.9.1-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    Hadoop HDFS初级部分

    理解HDFS的基本原理、架构设计及其使用方法对于掌握Hadoop技术至关重要。通过本文的介绍,我们对HDFS有了初步的认识,包括其架构、概念以及基本的命令行操作。这对于进一步深入学习Hadoop具有重要的意义。

    Hadoop hdfs文件操作,mr demo,topN demo

    例如,可以使用`hadoop fs -put`命令将本地文件上传到HDFS,用`hadoop fs -get`下载文件,`hadoop fs -rm`删除文件,`hadoop fs -mv`移动文件,以及`hadoop fs -ls`列出目录内容。 接着,MapReduce是一种编程模型,...

    Hadoop(HDFS).docx

    ### Hadoop HDFS知识点解析 #### 一、HDFS产出背景及定义 随着信息技术的快速发展,数据量呈现出爆炸性增长的趋势。传统的数据存储方法已经难以满足海量数据的存储需求。在这种背景下,分布式文件系统...

    hadoop hdfs配置

    ### Hadoop HDFS配置详解 #### 一、概述 在大数据处理领域,Hadoop作为一款开源软件框架,被广泛应用于分布式存储与计算。Hadoop Distributed File System ...希望本文能帮助初学者快速掌握Hadoop HDFS的配置方法。

    hadoop HDFS增删改

    ### Hadoop HDFS 增删改操作及配置详解 Hadoop 分布式文件系统 (HDFS) 是 Hadoop 的核心...以上是 HDFS 配置中的关键参数以及 Java API 的基础使用方法,通过合理的配置这些参数可以显著提升 HDFS 的性能和稳定性。

    hadoop-hdfs-2.7.3-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-2.7.3.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    hadoop-hdfs-2.5.1-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-2.5.1.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    hadoop-hdfs-2.5.1-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

Global site tag (gtag.js) - Google Analytics