hadoop hdfs 一些用法

本文介绍了使用Hadoop进行文件读写的多种方式,包括通过URLStreamHandler从Hadoop文件系统中读取文件显示到标准输出、直接使用FileSystem API读取文件、重复读取文件以及将本地文件复制到Hadoop文件系统并显示进度。
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
...............
### Hadoop HDFS 使用教程与操作指南 #### 了解HDFS基本结构 HDFS由多个组件构成,主要包括NameNode、DataNode以及Secondary NameNode。NameNode负责管理文件系统的命名空间和控制外部访问;DataNode存储实际的数据块并执行数据块的读写操作;Secondary NameNode辅助NameNode完成部分任务,如定期合并Fsimage和Edits文件[^4]。 #### 初始化环境准备 在使用HDFS之前,需确保已安装好Hadoop集群,并启动相应服务。通常通过`start-dfs.sh`脚本来开启必要的守护进程。 #### 常见命令行工具介绍 对于日常管理和维护而言,掌握命令行工具至关重要。这里主要讨论两个常用的命令前缀——`hadoop fs`和`hdfs dfs`,两者功能相同,在不同版本间可能有所偏好[^1]。 - **查看帮助文档** 获取所有可用选项的帮助信息: ```bash hdfs dfs -help ``` - **创建目录** 创建一个新的目录于指定路径下: ```bash hdfs dfs -mkdir /user/hadoop/testdir ``` - **上传文件至HDFS** 将本地机器上的单个文件或整个目录复制到分布式文件系统中: ```bash hdfs dfs -put local_file_path hdfs_directory_path ``` - **下载文件** 把位于远程服务器端的目标对象拉取回当前计算机保存: ```bash hdfs dfs -get hdfs_source_path local_destination_path ``` - **删除资源** 移除不再需要的对象(注意此动作不可逆): ```bash hdfs dfs -rm hdfs_object_path ``` - **显示详情列表** 展示特定位置下的内容概览及其属性特征: ```bash hdfs dfs -ls / ``` 上述指令覆盖了大部分基础场景的应用需求,更多高级特性可参阅官方手册进一步学习探索。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值