HDFS 读取、写入、遍历目录获取文件全路径、append文件创建或者写入报错问题分析

来源:互联网 发布:大数据 编辑:程序博客网 时间:2024/06/04 20:29

程序如:

 


1.报错类似下面的信息:

  org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException): Failed to create file [/user/hive/warehouse/test.db/incr_iot_report_gsh_1/odq_changedate=2017-10-19/incr_000000_Meter_0] for [DFSClient_NONMAPREDUCE_1573234124_13] for client [10.2.65.201], because this file is already being created by [DFSClient_NONMAPREDUCE_-1394763282_13] on [10.2.65.201]
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.recoverLeaseInternal(FSNamesystem.java:3187)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFileInternal(FSNamesystem.java:2968)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFileInt(FSNamesystem.java:3252)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFile(FSNamesystem.java:3216)
at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.append(NameNodeRpcServer.java:618)
at org.apache.hadoop.hdfs.server.namenode.AuthorizationProviderProxyClientProtocol.append(AuthorizationProviderProxyClientProtocol.java:126)
at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.append(ClientNamenodeProtocolServerSideTranslatorPB.java:422)
at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java)

分析:一般性是多线程造成的文件锁问题,说白点就是一个程序已经打开了这个目录下的这个文件,突然又有一个程序去打开这个文件,才会报如上错误

2.提示文件已经被一个client创建,try again 。。。也是因为文件被多个程序使用,如下错误

org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException): Failed to create file [/user/hive/warehouse/test.db/incr_iot_report_gsh_1/odq_changedate=2017-10-19/incr_000000_Meter_0] for [DFSClient_NONMAPREDUCE_76714100_13] for client [10.2.65.201], because this file is already being created by [DFSClient_NONMAPREDUCE_1322133037_13] on [10.2.65.201]
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.recoverLeaseInternal(FSNamesystem.java:3187)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFileInternal(FSNamesystem.java:2968)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFileInt(FSNamesystem.java:3252)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFile(FSNamesystem.java:3216)
at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.append(NameNodeRpcServer.java:618)
at org.apache.hadoop.hdfs.server.namenode.AuthorizationProviderProxyClientProtocol.append(AuthorizationProviderProxyClientProtocol.java:126)
at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.append(ClientNamenodeProtocolServerSideTranslatorPB.java:422)
at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java)
at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:617)
at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:1073)

处理如上错误可以使用hdfs参数进项控制:代码如下


重新初始化hdfs的句柄




1、从HDFS中读取数据

[java] view plain copy
  1. Configuration conf = getConf();  
  2.   Path path = new Path(pathstr);   
  3.   FileSystem fs = FileSystem.get(conf);  
  4.    FSDataInputStream fsin= fs.open(path );   
  5.    BufferedReader br =null;  
  6.    String line ;  
  7.    try{  
  8.     br = new BufferedReader(new InputStreamReader(fsin));  
  9.        while ((line = br.readLine()) != null) {  
  10.          System.out.println(line);  
  11.         }   
  12.    }finally{  
  13.     br.close();  
  14.    }  


2、写HDFS

[java] view plain copy
  1. Configuration conf = getConf();  
  2. Path path = new Path(mid_sort);   
  3. FileSystem fs = FileSystem.get(conf);   
  4. FSDataOutputStream out = fs.create(resultpath);  
  5. out.write(sb.toString().getBytes());  
  6. out.close();  


3、遍历目录 获取文件 全路径


[java] view plain copy
  1. /** 
  2.   * 得到一个目录(不包括子目录)下的所有名字匹配上pattern的文件名 
  3.   * @param fs 
  4.   * @param folderPath 
  5.   * @param pattern 用于匹配文件名的正则 
  6.   * @return 
  7.   * @throws IOException 
  8.   */  
  9.  public static List<Path> getFilesUnderFolder(FileSystem fs, Path folderPath, String pattern) throws IOException {  
  10.   List<Path> paths = new ArrayList<Path>();  
  11.   if (fs.exists(folderPath)) {  
  12.    FileStatus[] fileStatus = fs.listStatus(folderPath);  
  13.    for (int i = 0; i < fileStatus.length; i++) {  
  14.     FileStatus fileStatu = fileStatus[i];  
  15.     if (!fileStatu.isDir()) {//只要文件  
  16.      Path oneFilePath = fileStatu.getPath();  
  17.      if (pattern == null) {  
  18.       paths.add(oneFilePath);  
  19.      } else {  
  20.       if (oneFilePath.getName().contains(pattern)) {  
  21.        paths.add(oneFilePath);  
  22.       }  
  23.      }    
  24.     }  
  25.    }  
  26.   }  
  27.   return paths;  
  28.  }  

4、追加数据 append

[java] view plain copy
  1. public static boolean appendRTData(String hdfsFile, String appendFile) {  
  2.   boolean flag = false;  
  3.   
  4.   Configuration conf = new Configuration();  
  5.   FileSystem fs = null;  
  6.   try {  
  7.     fs = FileSystem.get(URI.create(hdfsFile), conf);  
  8.     InputStream in = new BufferedInputStream(new FileInputStream(appendFile));  
  9.     OutputStream out = fs.append(new Path(hdfsFile));  
  10.     IOUtils.copyBytes(in, out, 4096true);  
  11.   } catch (IOException e) {  
  12.     e.printStackTrace();  
  13.   }  
  14.   
  15.   return flag;  
  16. }  


***********************************************************************************************************************************************

***********************************************************************************************************************************************

异常信息

1、Exception in thread "main" java.lang.IllegalArgumentException: java.net.UnknownHostException: ns6

原因是没有加载hdfs的配置信息,需要添加下面的代码:

[java] view plain copy
  1. conf.addResource(new Path("/xxxx/hdfs-site.xml"));//path是配置文件地址  
如果配置了环境变量可以在不同的机器上使用:

[java] view plain copy
  1. conf.addResource(new Path(System.getenv("HADOOP_CONF") + "/hdfs-site.xml"));  

原创粉丝点击