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