`
gcgmh
  • 浏览: 353113 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hadoop hdfs 一些用法

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

//Reading Data from a Hadoop URL

public class URLCat {
	static {
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
	}

	public static void main(String[] args) throws Exception {
		InputStream in = null;
		try {
			in = new URL(args[0]).openStream();
			IOUtils.copyBytes(in, System.out, 4096, false);
		} finally {
			IOUtils.closeStream(in);
		}
	}
}
-----------------------------------------
result:
Here’s a sample run:
% hadoop URLCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.


Example 3-2. Displaying files from a Hadoop filesystem on standard output by using the FileSystem
directly
public class FileSystemCat {
	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);
		}
	}
}

------------------------------------------
The program runs as follows:
% hadoop FileSystemCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
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.

//Example 3-3. Displaying files from a Hadoop filesystem on standard output twice, by using seek

public class FileSystemDoubleCat {
	public static void main(String[] args) throws Exception {
		String uri = args[0];
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create(uri), conf); //通过get()方法获得一个FileSystem流
		FSDataInputStream in = null;
		try {
			in = fs.open(new Path(uri)); //通过open()方法打开一个FSDataInputStream流
			IOUtils.copyBytes(in, System.out, 4096, false);
			in.seek(0); // go back to the start of the file
			IOUtils.copyBytes(in, System.out, 4096, false);
		} finally {
			IOUtils.closeStream(in);
		}
	}
}
----------------------------------------------------
Here’s the result of running it on a small file:
% hadoop FileSystemDoubleCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
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.”)

//Example 3-4. Copying a local file to a Hadoop filesystem, and shows progress
public class FileCopyWithProgress {
	public static void main(String[] args) throws Exception {
		String localSrc = args[0];
		String dst = args[1];
		InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create(dst), conf);
		OutputStream out = fs.create(new Path(dst), new Progressable() {
			public void progress() {
				System.out.print(".");
			}
		});
		IOUtils.copyBytes(in, out, 4096, true);
	}
}

Typical usage:
% hadoop FileCopyWithProgress input/docs/1400-8.txt hdfs://localhost/user/tom/1400-8.txt
...............


分享到:
评论
1 楼 menghuannvxia 2015-03-13  
您好,我通过java上传文件到hadoop速度特别慢,怎么回事呢

相关推荐

    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