/** * Creates a new instance of the given {@link Class} via the default constructor. * * @param cls the {@link Class} to be instantiated * @return the instance */ public static <T> T instantiateDefault(Class<? extends T> cls) { try { return cls.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RheemException("Could not instantiate class.", e); } }
/** * Ensure that there is a directory represented by the given {@link File}. * * @param file the directory that should be ensured */ public static void ensureDir(File file) { if (file.exists()) { if (!file.isDirectory()) { throw new RheemException(String.format("Could not ensure directory %s: It exists, but is not a directory.", file)); } } else if (!file.mkdirs()) { throw new RheemException(String.format("Could not ensure directory %s: It does not exist, but could also not be created.", file)); } }
/** * Creates a new instance of a {@link Class} via the default constructor. * * @param className name of the {@link Class} to be instantiated * @return the instance */ public static <T> T instantiateDefault(String className) { try { @SuppressWarnings("unchecked") // Will fail anyway, if incorrect. Class<T> cls = (Class<T>) Class.forName(className); return cls.newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new RheemException("Could not instantiate class.", e); } }
@Override @SuppressWarnings("unchecked") public T deserialize(JSONObject json, Class<? extends T> cls) { if (json == null || json.equals(JSONObject.NULL)) return null; try { final Method fromJsonMethod = cls.getMethod("fromJson", JSONObject.class); return (T) fromJsonMethod.invoke(null, json); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RheemException(String.format("Could not execute %s.fromJson(...).", cls.getCanonicalName()), e); } } }
private PlanImplementation selectBestPlanNary(List<PlanImplementation> planImplementation) { assert !planImplementation.isEmpty(); return planImplementation.stream() .reduce(this::selectBestPlanBinary) .orElseThrow(() -> new RheemException("No plan was selected.")); }
public void set(Key key, Value value) { throw new RheemException(String.format("Setting values not supported for %s.", this.getClass().getSimpleName())); }
/** * Converts a {@link File} object to a URL. * * @param file that should be converted * @return the {@link String} representation of the URL */ public static String toURL(File file) { try { return file.toPath().toUri().toURL().toString(); } catch (MalformedURLException e) { throw new RheemException(String.format("Could not create URI for %s", file), e); } }
@Override public boolean isDirectory(String url) { try { final FileStatus fileStatus = this.getHdfs(url).getFileStatus(new Path(url)); return fileStatus.isDirectory(); } catch (IOException e) { throw new RheemException(String.format("Could not access %s.", url), e); } }
/** * As {@link #findActualInputPaths(String)} but requires the presence of only a single input file. */ public static String findActualSingleInputPath(String ostensibleInputFile) { final Collection<String> inputPaths = FileSystems.findActualInputPaths(ostensibleInputFile); if (inputPaths.size() != 1) { throw new RheemException(String.format( "Illegal number of files for \"%s\": %s", ostensibleInputFile, inputPaths )); // TODO: Add support. } return inputPaths.iterator().next(); }
/** * Create an empty file. * * @param file that should be created */ public static void touch(File file) { ensureDir(file.getParentFile()); try (FileOutputStream fos = new FileOutputStream(file)) { } catch (IOException e) { throw new RheemException(String.format("Could not create %s.", file), e); } }
@Override public Collection<String> listChildren(String url) { try { final FileStatus[] fileStatuses = this.getHdfs(url).listStatus(new Path(url)); return Arrays.stream(fileStatuses) .map(status -> status.getPath().toString()) .collect(Collectors.toList()); } catch (IOException e) { throw new RheemException(String.format("Could not access %s.", url), e); } }
@Override @SuppressWarnings("unchecked") public <T> Collection<T> getBroadcast(String name) { final Broadcast<?> broadcast = this.broadcasts.get(name); if (broadcast == null) { throw new RheemException("No such broadcast found: " + name); } return (Collection<T>) broadcast.getValue(); }
/** * Initializes the {@link #writer} if it does not exist currently. * * @return the {@link #writer} */ private BufferedWriter getWriter() throws FileNotFoundException, UnsupportedEncodingException { if (this.writer == null) { File file = new File(this.repositoryPath); final File parentFile = file.getParentFile(); if (!parentFile.exists() && !file.getParentFile().mkdirs()) { throw new RheemException("Could not initialize cardinality repository."); } this.writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8")); } return this.writer; }
private ReplacementSubplanFactory createReplacementSubplanFactory() { return new ReplacementSubplanFactory.OfSingleOperators<PageRankOperator>( (matchedOperator, epoch) -> { // We need to instantiate the SparkPageRankOperator via reflection, because the Scala code will // be compiled only after the Java code, which might cause compile errors. try { final Class<?> cls = Class.forName("org.qcri.rheem.spark.operators.graph.SparkPageRankOperator"); final Constructor<?> constructor = cls.getConstructor(PageRankOperator.class); return (Operator) constructor.newInstance(matchedOperator); } catch (Exception e) { throw new RheemException(String.format("Could not apply %s.", this), e); } } ); } }
private org.apache.hadoop.fs.FileSystem getHdfs(String uri) { this.ensureInitialized(); try { Configuration conf = new Configuration(true); return org.apache.hadoop.fs.FileSystem.get(new URI(uri), conf); } catch (IOException | URISyntaxException e) { throw new RheemException(String.format("Could not obtain an HDFS client for %s.", uri), e); } }
public static FileSystem requireFileSystem(String fileUrl) { return getFileSystem(fileUrl).orElseThrow( () -> new RheemException(String.format("Could not identify filesystem for \"%s\".", fileUrl)) ); }
private void tryAdvance() { if (this.nextElements != null && ++this.nextIndex < this.nextElements.length) return; try { if (!this.sequenceFileReader.next(this.nullWritable, this.bytesWritable)) { this.nextElements = null; return; } this.nextElements = (Object[]) new ObjectInputStream(new ByteArrayInputStream(this.bytesWritable.getBytes())).readObject(); this.nextIndex = 0; } catch (IOException | ClassNotFoundException e) { this.nextElements = null; IOUtils.closeQuietly(this); throw new RheemException("Reading failed.", e); } }
/** * Initializes the {@link #writer} if it does not exist currently. * * @return the {@link #writer} */ private BufferedWriter getWriter() throws FileNotFoundException, UnsupportedEncodingException { if (this.writer != null) { return this.writer; } try { File file = new File(this.repositoryPath); final File parentFile = file.getParentFile(); if (!parentFile.exists() && !file.getParentFile().mkdirs()) { throw new RheemException("Could not initialize cardinality repository."); } return this.writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8")); } catch (RheemException e) { throw e; } catch (Exception e) { throw new RheemException(String.format("Cannot write to %s.", this.repositoryPath), e); } }
/** * Loads a specific {@link Platform} implementation. For platforms to interoperate with this method, they must * provide a {@code static}, parameterless method {@code getInstance()} that returns their singleton instance. * * @param platformClassName the class name of the {@link Platform} * @return the {@link Platform} instance */ public static Platform load(String platformClassName) { try { return ReflectionUtils.executeStaticArglessMethod(platformClassName, "getInstance"); } catch (Exception e) { throw new RheemException("Could not load platform: " + platformClassName, e); } }
/** * Queries the {@link Job}-global cache. * * @param key that is associated with the value to be retrieved * @param resultClass the expected {@link Class} of the retrieved value * @return the value associated with the key or else {@code null} */ public <T> T queryJobCache(String key, Class<T> resultClass) { final Object value = this.queryJobCache(key); try { return resultClass.cast(value); } catch (ClassCastException e) { throw new RheemException("Job-cache value cannot be casted as requested.", e); } }