| 175 | } |
| 176 | |
| 177 | public static ResourceSchema getSchema(LoadFunc wrappedLoadFunc, String location, boolean checkExistence, Job job) |
| 178 | throws IOException { |
| 179 | Configuration conf = job.getConfiguration(); |
| 180 | if (checkExistence) { |
| 181 | Path path = new Path(location); |
| 182 | if (!FileSystem.get(conf).exists(path)) { |
| 183 | // At compile time in batch mode, the file may not exist |
| 184 | // (such as intermediate file). Just return null - the |
| 185 | // same way as we would if we did not get a valid record |
| 186 | return null; |
| 187 | } |
| 188 | } |
| 189 | ReadToEndLoader loader = new ReadToEndLoader(wrappedLoadFunc, conf, location, 0); |
| 190 | // get the first record from the input file |
| 191 | // and figure out the schema from the data in |
| 192 | // the first record |
| 193 | Tuple t = loader.getNext(); |
| 194 | if (t == null) { |
| 195 | // we couldn't get a valid record from the input |
| 196 | return null; |
| 197 | } |
| 198 | int numFields = t.size(); |
| 199 | Schema s = new Schema(); |
| 200 | for (int i = 0; i < numFields; i++) { |
| 201 | try { |
| 202 | s.add(DataType.determineFieldSchema(t.get(i))); |
| 203 | } |
| 204 | catch (Exception e) { |
| 205 | int errCode = 2104; |
| 206 | String msg = "Error while determining schema of SequenceFileStorage data."; |
| 207 | throw new ExecException(msg, errCode, PigException.BUG, e); |
| 208 | } |
| 209 | } |
| 210 | return new ResourceSchema(s); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @param schemaString a String representation of the Schema <b>without</b> |