Hadoop HDFS文件常用操作及注意事项
Hadoop HDFS文件常用操作及注意事项
1.Copy a file from the local file system to HDFS
The srcFile variable needs to contain the full name (path + file name) of the file in the local file system.
The dstFile variable needs to contain the desired full name of the file in the Hadoop file system.
1 Configuration config = new Configuration();
2 FileSystem hdfs = FileSystem.get(config);
3 Path srcPath = new Path(srcFile);
4 Path dstPath = new Path(dstFile);
5 hdfs.copyFromLocalFile(srcPath, dstPath);
2.Create HDFS file
The fileName variable contains the file name and path in the Hadoop file system.
The content of the file is the buff variable which is an array of bytes.
1 //byte[] buff - The content of the file
2 //创建了一个HDFS文件,并且把buff数组的内容写到了HDFS文件中。
3 Configuration config = new Configuration();
4 FileSystem hdfs = FileSystem.get(config);
5 Path path = new Path(fileName);
6 FSDataOutputStream outputStream = hdfs.create(path);
7 outputStream.write(buff, 0, buff.length);
3.Rename HDFS file
In order to rename a file in Hadoop file system, we need the full name (path + name) of
the file we want to rename. The rename method returns true if the file was renamed, otherwise false.
1 Configuration config = new Configuration();
2 FileSystem hdfs = FileSystem.get(config);
3 Path fromPath = new Path(fromFileName);
4 Path toPath = new Path(toFileName);
5 boolean isRenamed = hdfs.rename(fromPath, toPath);
4.Delete HDFS file
In order to delete a file in Hadoop file system, we need the full name (path + name)
of the file we want to delete. The delete method returns true if the file was deleted, otherwise false.
1 Configuration config = new Configuration();
2 FileSystem hdfs = FileSystem.get(config);
3 Path path = new Path(fileName);
4 boolean isDeleted = hdfs.delete(path, false);
5
6 //Recursive delete:估计true是递归删除该目录下面的文件。
7 Configuration config = new Configuration();
8 FileSystem hdfs = FileSystem.get(config);
9 Path path = new Path(fileName);
10 boolean isDeleted = hdfs.delete(path, true);
5.Get HDFS file last modification time
In order to get the last modification time of a file in Hadoop file system,
we need the full name (path + name) of the file.
1 Configuration config = new Configuration();
2 FileSystem hdfs = FileSystem.get(config);
3 Path path = new Path(fileName);
4 FileStatus fileStatus = hdfs.getFileStatus(path);
5 long modificationTime = fileStatus.getModificationTime
6.Check if a file exists in HDFS
In order to check the existance of a file in Hadoop file system,
we need the full name (path + name) of the file we want to check.
The exists methods returns true if the file exists, otherwise false.
1 Configuration config = new Configuration();
2 FileSystem hdfs = FileSystem.get(config);
3 Path path = new Path(fileName);
4 boolean isExists = hdfs.exists(path);
7.Get the locations of a file in the HDFS cluster
A file can exist on more than one node in the Hadoop file system cluster for two reasons:
Based on the HDFS cluster configuration, Hadoop saves parts of files on different nodes in the cluster.
Based on the HDFS cluster configuration, Hadoop saves more than one copy of each file on different nodes for redundancy (The default is three).
1 Configuration config = new Configuration();
2 FileSystem hdfs = FileSystem.get(config);
3 Path path = new Path(fileName);
4 FileStatus fileStatus = hdfs.getFileStatus(path);
5 BlockLocation[] blkLocations = hdfs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
6 int blkCount = blkLocations.length;
7 for (int i = 0; i < blkCount; i++) {
8 String[] hosts = blkLocations[i].getHosts();
9 // Do something with the block hosts
10 }
8. Get a list of all the nodes host names in the HDFS cluster
his method casts the FileSystem Object to a DistributedFileSystem Object.
This method will work only when Hadoop is configured as a cluster.
Running Hadoop on the local machine only, in a non cluster configuration will cause this method to throw an Exception.
1 Configuration config = new Configuration();
2 FileSystem fs = FileSystem.get(config);
3 DistributedFileSystem hdfs = (DistributedFileSystem) fs;
4 DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
5 String[] names = new String[dataNodeStats.length];
6 for (int i = 0; i < dataNodeStats.length; i++) {
7 names[i] = dataNodeStats[i].getHostName();
8 }
遇到的问题及解决:
1.文件append的问题
hadoop的版本1.0.4以后,API中已经有了追加写入的功能,但不建议在生产环境中使用,原因如下:Does HDFS allow appends to files? This is currently set to false because there are bugs in the "append code" and is not supported in any prodction cluster.
如果要测试的话,需要把dfs.support.appen的参数设置为true,不然客户端写入的时候会报错:
Exception in thread "main" org.apache.hadoop.ipc.RemoteException: java.io.IOException: Append to hdfs not supported. Please refer to dfs.support.append configuration parameter.
解决:修改namenode节点上的hdfs-site.xml。
<property>
<name>dfs.support.append</name>
<value>true</value>
</property>
2.在Hadoop-1.0.4和Hadoop-2.2的使用append时,需求:追加写入文件,如果文件不存在,需求先创建。
异常:
Exception in thread "main" org.apache.hadoop.ipc.RemoteException: org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException: failed to create file /huangq/dailyRolling/mommy-dailyRolling for DFSClient_-1456545217 on client 10.1.85.243 because current leaseholder is trying to recreate file.
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.recoverLeaseInternal(FSNamesystem.java:1374)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.startFileInternal(FSNamesystem.java:1246)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFile(FSNamesystem.java:1426)
at org.apache.hadoop.hdfs.server.namenode.NameNode.append(NameNode.java:643)
at sun.reflect.GeneratedMethodAccessor25.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
代码版本1:(出现上述报错的代码)
1 FileSystem fs = FileSystem.get(conf);
2 Path dstPath = new Path(dst);
3 if (!fs.exists(dstPath)) {
4 fs.create(dstPath);
5 }
6 FSDataOutputStream fsout = fs.append(dstPath);
7 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fsout));
异常的原因:fs句柄的原因,改成下面就ok了。
解决:创建完文件之后,关闭流fs,再次获得一次新的fs。
1 FileSystem fs = FileSystem.get(conf);
2 Path dstPath = new Path(dst);
3 if (!fs.exists(dstPath)) {
4 fs.create(dstPath);
5 fs.close();
6 fs = FileSystem.get(conf);
7 }
8 FSDataOutputStream fsout = fs.append(dstPath);
原文地址:http://www.cnblogs.com/byrhuangqiang/p/3926663.html
Hadoop HDFS文件常用操作及注意事项的更多相关文章
- Hadoop HDFS文件常用操作及注意事项(更新)
1.Copy a file from the local file system to HDFS The srcFile variable needs to contain the full name ...
- [b0002] Hadoop HDFS cmd常用命令练手
目的: 学会HDFS CLI 常用操作 环境: Hadoop 2.6.4 伪分布式版 环境搭建参考本博客前篇文章: 伪分布式 hadoop 2.6.4 帮助: hadoop@ssmaster:~$ h ...
- python 异常处理、文件常用操作
异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm
- go语言之进阶篇文件常用操作接口介绍和使用
一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...
- Python基础灬文件常用操作
文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...
- hdfs 文件系统命令操作
hdfs 文件系统命令操作 [1]hdfs dfs -ls [目录]. 显示所有文件 hdfs dfs -ls -h /user/20170214.txt 显示文件时,文件大小以人易读的形式显示 [2 ...
- Hadoop HDFS文件操作
1.创建目录 import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.ha ...
- HDFS文件读写操作(基础基础超基础)
环境 OS: Ubuntu 16.04 64-Bit JDK: 1.7.0_80 64-Bit Hadoop: 2.6.5 原理 <权威指南>有两张图,下次po上来好好聊一下 实测 读操作 ...
- Spark环境搭建(二)-----------HDFS shell 常用操作
配置好HDFS,也学习了点HDFS的简单操作,跟Linux命令相似 1) 配置Hadoop的环境变量,类似Java的配置 在 ~/.bash_profile 中加入 export HADOOP_HO ...
随机推荐
- preseed.cfg分区设定案例
很久之前做ubuntu的PXE配置ubuntu的preseed费了很大的力气,总结的不多,现在温习一下. 就我所接触的,有分区普通磁盘,LVM,和raid三种方式.其中前两中方式比较多,raid方式是 ...
- 2014 ACM/ICPC Asia Regional Guangzhou Online
Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...
- [ActionScript 3] 本地安全沙箱问题最快解决方法
使用FLex4.0时, Chrome等浏览器会报 安全水箱问题..... 解决方案: 打开flash全局安全配置面板并在页面中的配置面板中添加可信任的swf文件或者文件夹: 若是记不住全局安全配置 ...
- XSS的原理分析与解剖(二)
0×01 前言: 上节(http://www.freebuf.com/articles/web/40520.html)已经说明了xss的原理及不同环境的构造方法.本期来说说XSS的分类及挖掘方法. ...
- Python3中的新特性(1)——新的语言特性
1.源代码编码和标识符 Python3假定源代码使用UTF-8编码.另外,关于标识符中哪些字符是合法的规则也放宽了.特别是,标识符可以包含代码点为U+0080及以上的任意有效Unico ...
- UML类图(转载)
概述: 类图是静态图.它代表了一个应用程序的静态视图.类图不仅用于可视化描述和记录系统的不同方面,但也为构建可执行代码的软件应用程序. 类图描述一类的属性和操作,也对系统的约束.被广泛应用于类图的建模 ...
- svn 分支与合并的使用
在使用svn的时候我们往往有这样的需求.我们修改某些代码,因为对某项技术不是非常的熟悉,担心自己当前的修改(或者叫测试)会影响到服务器中版本库代码的崩溃等.传统做法我们会手动复制一份代码,然后修改 ...
- CSS3:线上编辑工具及实用资料整理
an I Use 个人最常用的,资料比较全,桌面和移动浏览器支持HTML5,CSS3,SVG和兼容性表. 官网地址:http://caniuse.com/ CSS3 Click Chart CSS3 ...
- 偏序集的Dilworth定理
定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小.则X可以被划分成r个但不能再少的反链.其对偶定理称为Dilworth定理:定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小. ...
- ASP.NET MVC的处理管线
原文:http://www.cnblogs.com/fzrain/p/3651693.html 下面开始解释各个部分: 路由模块 1.在ASP.NET MVC处理管线中的第一站就是路由模块.当请求到达 ...