`
gcgmh
  • 浏览: 348698 次
  • 性别: 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速度特别慢,怎么回事呢

相关推荐

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

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

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

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

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

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

    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

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

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

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

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

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

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

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

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

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

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

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

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

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

    hio:与 Hadoop HDFS 交互的命令行实用程序

    o 与 Hadoop HDFS 交互的命令行实用程序。 想想:Hadoop I/O CLI。 “hio”是 Hadoop I/O ... 用法 概述安装后,主要入口点是hio可执行文件: $ hiohio: Command line tools to interact with Hadoop HDFS and other su

    webhdfs:Hadoop WebHDFS的Ruby客户端

    安装gem install webhdfs用法WebHDFS :: Client 对于客户端对象接口: require 'webhdfs'client = WebHDFS::Client.new(hostname, port)# or with pseudo username authenticationclient = WebHDFS::Client.new...

    MahoutHadoopUseCase:电影推荐(1. 下载并预处理数据集;2. 将数据放入 hadoop HDFS;3. 在 Hadoop 上使用 MapReduce 运行 Mahout 协作过滤 4. 获取推荐的前 k 部电影列表)

    (问题已于 14 年 2 月 25 日解决),我想找出一种在 Hadoop 2.2.0 上使用 Mahout 0.8 协同过滤库运行用例的替代方法 在 Eclipse 和 Yarn 上运行 hadoop 程序 步骤: sbin/start_classicCF.sh 该脚本用于下载数据...

    高可用性的HDFS:Hadoop分布式文件系统深度实践

    第1章 HDFS HA及解决方案 1.1 HDFS系统架构 1.2 HA定义 1.3 HDFS HA原因分析及应对措施 1.3.1 可靠性 1.3.2 可维护性 1.4 现有HDFS HA解决方案 1.4.1 Hadoop的元数据备份方案 1.4.2 Hadoop的SecondaryNameNode方案 ...

    实验2:用Hadoop进行HDFS实验

    安装虚拟机和linux,虚拟机推荐使用vmware,PC可以使用workstation,服务器可 以使用ESXi,在管理上比较方便。ESXi还可以通过拷贝镜像文件复制虚拟机,复制后 自动修改网卡号和ip,非常快捷。如果只是实验用途,硬盘大约...

    hadoop之HDFS

    HDFS常用命令的用法技巧等,hadoop学习的有效利器。

    Php-Hadoop-Hdfs:通过 CLI 和 WebHDFS 用于 HDFS 的 PHP 客户端

    每个方法都有详细的文档块,所以使用 phpDocumentor 应该足以开始。 应用程序接口: 有关每种方法抛出的异常,请参阅文档块 * 实例化 CLI 实现: $ hdfs = new \ Hdfs \ Cli (); 实例化 WebHDFS 实现: $ ...

    HDFS管理工具HDFS Explorer下载地址、使用方法.docx

    windows平台下的HDFS文件浏览器,就像windows管理器一样管理你的hdfs文件系统。现在官网已经停止更新这款软件。具体配置如下: HDFS配置页面及端口http://master:50070 配置HDFS服务器 配置WebHDFS HDFS Explorer...

    新版Hadoop视频教程 段海涛老师Hadoop八天完全攻克Hadoop视频教程 Hadoop开发

    第一天 hadoop的基本概念 伪分布式hadoop集群安装 hdfs mapreduce 演示 01-hadoop职位需求状况.avi 02-hadoop课程安排.avi 03-hadoop应用场景.avi 04-hadoop对海量数据处理的解决思路.avi 05-hadoop版本选择和...

Global site tag (gtag.js) - Google Analytics