001 /** 002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved. 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. See accompanying LICENSE file. 014 */ 015 package org.apache.oozie.action.hadoop; 016 017 import java.io.IOException; 018 import java.net.URI; 019 import java.net.URISyntaxException; 020 021 import org.apache.hadoop.fs.FileStatus; 022 import org.apache.hadoop.fs.FileSystem; 023 import org.apache.hadoop.fs.Path; 024 import org.apache.hadoop.conf.Configuration; 025 import org.apache.oozie.DagELFunctions; 026 import org.apache.oozie.client.WorkflowJob; 027 import org.apache.oozie.service.HadoopAccessorException; 028 import org.apache.oozie.service.Services; 029 import org.apache.oozie.service.HadoopAccessorService; 030 031 /** 032 * EL function for fs action executor. 033 */ 034 public class FsELFunctions { 035 036 private static FileSystem getFileSystem(URI uri) throws HadoopAccessorException { 037 WorkflowJob workflow = DagELFunctions.getWorkflow(); 038 String user = workflow.getUser(); 039 String group = workflow.getGroup(); 040 return Services.get().get(HadoopAccessorService.class). 041 createFileSystem(user, group, uri, DagELFunctions.getProtoActionConf()); 042 } 043 044 /** 045 * Get file status. 046 * 047 * @param pathUri fs path uri 048 * @return file status 049 * @throws URISyntaxException 050 * @throws IOException 051 * @throws Exception 052 */ 053 private static FileStatus getFileStatus(String pathUri) throws Exception { 054 URI uri = new URI(pathUri); 055 String path = uri.getPath(); 056 FileSystem fs = getFileSystem(uri); 057 Path p = new Path(path); 058 return fs.exists(p) ? fs.getFileStatus(p) : null; 059 } 060 061 /** 062 * Return if a path exists. 063 * 064 * @param pathUri file system path uri. 065 * @return <code>true</code> if the path exists, <code>false</code> if it does not. 066 * @throws Exception 067 */ 068 public static boolean fs_exists(String pathUri) throws Exception { 069 URI uri = new URI(pathUri); 070 String path = uri.getPath(); 071 FileSystem fs = getFileSystem(uri); 072 return fs.exists(new Path(path)); 073 } 074 075 /** 076 * Return if a path is a directory. 077 * 078 * @param pathUri fs path uri. 079 * @return <code>true</code> if the path exists and it is a directory, <code>false</code> otherwise. 080 * @throws Exception 081 */ 082 public static boolean fs_isDir(String pathUri) throws Exception { 083 boolean isDir = false; 084 FileStatus fileStatus = getFileStatus(pathUri); 085 if (fileStatus != null) { 086 isDir = fileStatus.isDir(); 087 } 088 return isDir; 089 } 090 091 /** 092 * Return the len of a file. 093 * 094 * @param pathUri file system path uri. 095 * @return the file len in bytes, -1 if the file does not exist or if it is a directory. 096 * @throws Exception 097 */ 098 public static long fs_fileSize(String pathUri) throws Exception { 099 long len = -1; 100 FileStatus fileStatus = getFileStatus(pathUri); 101 if (fileStatus != null) { 102 len = fileStatus.getLen(); 103 } 104 return len; 105 } 106 107 /** 108 * Return the size of all files in the directory, it is not recursive. 109 * 110 * @param pathUri file system path uri. 111 * @return the size of all files in the directory, -1 if the directory does not exist or if it is a file. 112 * @throws Exception 113 */ 114 public static long fs_dirSize(String pathUri) throws Exception { 115 URI uri = new URI(pathUri); 116 String path = uri.getPath(); 117 long size = -1; 118 try { 119 FileSystem fs = getFileSystem(uri); 120 Path p = new Path(path); 121 if (fs.exists(p) && !fs.isFile(p)) { 122 FileStatus[] stati = fs.listStatus(p); 123 size = 0; 124 if (stati != null) { 125 for (FileStatus status : stati) { 126 if (!status.isDir()) { 127 size += status.getLen(); 128 } 129 } 130 } 131 } 132 } 133 catch (Exception ex) { 134 throw new RuntimeException(ex); 135 } 136 return size; 137 } 138 139 /** 140 * Return the file block size in bytes. 141 * 142 * @param pathUri file system path uri. 143 * @return the block size of the file in bytes, -1 if the file does not exist or if it is a directory. 144 * @throws Exception 145 */ 146 public static long fs_blockSize(String pathUri) throws Exception { 147 long blockSize = -1; 148 FileStatus fileStatus = getFileStatus(pathUri); 149 if (fileStatus != null) { 150 blockSize = fileStatus.getBlockSize(); 151 } 152 return blockSize; 153 } 154 155 }