设为首页 收藏本站
查看: 1032|回复: 0

[经验分享] Hadoop HDFS文件常用操作及注意事项(更新)

[复制链接]

尚未签到

发表于 2015-7-11 10:49:12 | 显示全部楼层 |阅读模式
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.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 = dataNodeStats.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。



  
dfs.support.append
true

  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));
  异常的原因:FSDataOutputStream create(Path f) 产生了一个输出流,创建完后需要关闭。
  解决:创建完文件之后,关闭流FSDataOutputStream。



FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path(dst);
if (!fs.exists(dstPath)) {
fs.create(dstPath).close();
}
FSDataOutputStream fsout = fs.append(dstPath);
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-85476-1-1.html 上篇帖子: Hadoop:The Definitive Guid 总结 Chapter 3 Hadoop分布式文件系统 下篇帖子: hadoop MapReduce Job失效模型
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表