@Override public JsonIterator iter(final Context context) throws Exception { final JsonString cmd = (JsonString) exprs[1].eval(context); if (cmd == null) { return JsonIterator.NULL; } iter = exprs[0].iter(context); if (iter.isNull()) { return JsonIterator.NULL; } proc = Runtime.getRuntime().exec(cmd.toString()); // TODO: add thread pool to context InputThread inputThread = new InputThread(); ErrorThread errorThread = new ErrorThread(); final MutableJsonString str = new MutableJsonString(); try { InputStream is = proc.getInputStream(); final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); errorThread.start(); inputThread.start(); return new JsonIterator(str) { @Override public boolean moveNext() throws Exception { try { String s = reader.readLine(); if (s == null) { reader.close(); int rc = proc.waitFor(); if (rc != 0) { System.err.println("non-zero exit code from process [" + cmd + "]: " + rc); } return false; } str.setCopy(s); return true; // currentValue == str } catch (Throwable e) { if (error == null) { error = e; } proc.destroy(); if (error instanceof Exception) { throw (Exception) error; } throw new UndeclaredThrowableException(error); } } }; } catch (Throwable e) { if (error == null) { error = e; } proc.destroy(); if (error instanceof Exception) { throw (Exception) error; } throw new UndeclaredThrowableException(error); } }
@Override public JsonString eval(Context context) throws Exception { JsonString inpath = (JsonString) exprs[0].eval(context); String outpath; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path path = new Path(inpath.toString()); if (path.isAbsolute()) { String uri = fs.getUri().toString(); outpath = uri + inpath; } else { String home = fs.getHomeDirectory().toString(); outpath = home + "/" + inpath; } if (!fs.exists(new Path(outpath))) { throw new IllegalArgumentException("The input path doesn't exist in HDFS"); } return new JsonString(outpath); }
/* * (non-Javadoc) * * @see com.ibm.jaql.lang.expr.core.IterExpr#iter(com.ibm.jaql.lang.core.Context) */ public JsonArray eval(final Context context) throws Exception { final JsonRegex regex = (JsonRegex) exprs[0].eval(context); if (regex == null) { return null; } JsonString text = (JsonString) exprs[1].eval(context); if (text == null) { return null; } final Matcher matcher = regex.takeMatcher(); matcher.reset(text.toString()); if (!matcher.find()) { regex.returnMatcher(matcher); return null; } int n = matcher.groupCount(); BufferedJsonArray arr = new BufferedJsonArray(n); // TODO: memory for (int i = 0; i < n; i++) { String s = matcher.group(i + 1); arr.set(i, s == null ? null : new JsonString(s)); // TODO: memory } regex.returnMatcher(matcher); return arr; }