Search in sources :

Example 16 with URIHandler

use of org.apache.oozie.dependency.URIHandler in project oozie by apache.

the class CoordCommandUtils method createEarlyURIs.

/**
 * The function create a list of URIs separated by "," using the instances
 * time stamp and URI-template
 *
 * @param event : <data-in> event
 * @param instances : List of time stamp separated by ","
 * @param unresolvedInstances : list of instance with latest function
 * @param urisWithDoneFlag : list of URIs with the done flag appended
 * @return : list of URIs separated by ";" as a string.
 * @throws Exception
 */
public static String createEarlyURIs(Element event, String instances, StringBuilder unresolvedInstances, StringBuilder urisWithDoneFlag) throws Exception {
    if (instances == null || instances.length() == 0) {
        return "";
    }
    String[] instanceList = instances.split(CoordELFunctions.INSTANCE_SEPARATOR);
    StringBuilder uris = new StringBuilder();
    Element doneFlagElement = event.getChild("dataset", event.getNamespace()).getChild("done-flag", event.getNamespace());
    URIHandlerService uriService = Services.get().get(URIHandlerService.class);
    for (int i = 0; i < instanceList.length; i++) {
        if (instanceList[i].trim().length() == 0) {
            continue;
        }
        int funcType = getFuncType(instanceList[i]);
        if (funcType == LATEST || funcType == FUTURE) {
            if (unresolvedInstances.length() > 0) {
                unresolvedInstances.append(CoordELFunctions.INSTANCE_SEPARATOR);
            }
            unresolvedInstances.append(instanceList[i]);
            continue;
        }
        ELEvaluator eval = CoordELEvaluator.createURIELEvaluator(instanceList[i]);
        if (uris.length() > 0) {
            uris.append(CoordELFunctions.INSTANCE_SEPARATOR);
            urisWithDoneFlag.append(CoordELFunctions.INSTANCE_SEPARATOR);
        }
        String uriPath = CoordELFunctions.evalAndWrap(eval, event.getChild("dataset", event.getNamespace()).getChild("uri-template", event.getNamespace()).getTextTrim());
        URIHandler uriHandler = uriService.getURIHandler(uriPath);
        uriHandler.validate(uriPath);
        uris.append(uriPath);
        urisWithDoneFlag.append(uriHandler.getURIWithDoneFlag(uriPath, CoordUtils.getDoneFlag(doneFlagElement)));
    }
    return uris.toString();
}
Also used : Element(org.jdom.Element) URIHandlerService(org.apache.oozie.service.URIHandlerService) URIHandler(org.apache.oozie.dependency.URIHandler) CoordELEvaluator(org.apache.oozie.coord.CoordELEvaluator) ELEvaluator(org.apache.oozie.util.ELEvaluator)

Example 17 with URIHandler

use of org.apache.oozie.dependency.URIHandler in project oozie by apache.

the class CoordCommandUtils method pathExists.

public static boolean pathExists(String sPath, Configuration actionConf, String user) throws IOException, URISyntaxException, URIHandlerException {
    URI uri = new URI(sPath);
    URIHandlerService service = Services.get().get(URIHandlerService.class);
    URIHandler handler = service.getURIHandler(uri);
    return handler.exists(uri, actionConf, user);
}
Also used : URIHandlerService(org.apache.oozie.service.URIHandlerService) URIHandler(org.apache.oozie.dependency.URIHandler) URI(java.net.URI)

Example 18 with URIHandler

use of org.apache.oozie.dependency.URIHandler in project oozie by apache.

the class CoordCommandUtils method materializeInputDataEvents.

public static void materializeInputDataEvents(List<Element> events, SyncCoordAction appInst, Configuration conf, CoordinatorActionBean actionBean, boolean isInputLogicSpecified) throws Exception {
    if (events == null) {
        return;
    }
    CoordInputDependency coordPullInputDependency = CoordInputDependencyFactory.createPullInputDependencies(isInputLogicSpecified);
    CoordInputDependency coordPushInputDependency = CoordInputDependencyFactory.createPushInputDependencies(isInputLogicSpecified);
    List<Pair<String, String>> unresolvedList = new ArrayList<Pair<String, String>>();
    URIHandlerService uriService = Services.get().get(URIHandlerService.class);
    for (Element event : events) {
        StringBuilder instances = new StringBuilder();
        ELEvaluator eval = CoordELEvaluator.createInstancesELEvaluator(event, appInst, conf);
        // Handle list of instance tag
        resolveInstances(event, instances, appInst, conf, eval);
        // Handle start-instance and end-instance
        resolveInstanceRange(event, instances, appInst, conf, eval);
        // Separate out the unresolved instances
        String resolvedList = separateResolvedAndUnresolved(event, instances);
        String name = event.getAttribute("name").getValue();
        if (!resolvedList.isEmpty()) {
            Element uri = event.getChild("dataset", event.getNamespace()).getChild("uri-template", event.getNamespace());
            String uriTemplate = uri.getText();
            URI baseURI = uriService.getAuthorityWithScheme(uriTemplate);
            URIHandler handler = uriService.getURIHandler(baseURI);
            List<CoordInputInstance> inputInstanceList = new ArrayList<CoordInputInstance>();
            for (String inputInstance : resolvedList.split("#")) {
                inputInstanceList.add(new CoordInputInstance(inputInstance, false));
            }
            if (handler.getDependencyType(baseURI).equals(DependencyType.PULL)) {
                coordPullInputDependency.addInputInstanceList(name, inputInstanceList);
            } else {
                coordPushInputDependency.addInputInstanceList(name, inputInstanceList);
            }
        }
        String tmpUnresolved = event.getChildTextTrim(UNRESOLVED_INSTANCES_TAG, event.getNamespace());
        if (tmpUnresolved != null) {
            unresolvedList.add(new Pair<String, String>(name, tmpUnresolved));
        }
    }
    for (Pair<String, String> unresolvedDataset : unresolvedList) {
        coordPullInputDependency.addUnResolvedList(unresolvedDataset.getFirst(), unresolvedDataset.getSecond());
    }
    actionBean.setPullInputDependencies(coordPullInputDependency);
    actionBean.setPushInputDependencies(coordPushInputDependency);
    actionBean.setMissingDependencies(coordPullInputDependency.serialize());
    actionBean.setPushMissingDependencies(coordPushInputDependency.serialize());
}
Also used : Element(org.jdom.Element) ArrayList(java.util.ArrayList) URI(java.net.URI) URIHandlerService(org.apache.oozie.service.URIHandlerService) URIHandler(org.apache.oozie.dependency.URIHandler) CoordInputInstance(org.apache.oozie.coord.input.dependency.CoordInputInstance) CoordELEvaluator(org.apache.oozie.coord.CoordELEvaluator) ELEvaluator(org.apache.oozie.util.ELEvaluator) CoordInputDependency(org.apache.oozie.coord.input.dependency.CoordInputDependency) Pair(org.apache.oozie.util.Pair)

Example 19 with URIHandler

use of org.apache.oozie.dependency.URIHandler in project oozie by apache.

the class CoordRerunXCommand method cleanupOutputEvents.

/**
 * Cleanup output-events directories
 *
 * @param eAction coordinator action xml
 */
@SuppressWarnings("unchecked")
private void cleanupOutputEvents(Element eAction, Configuration coordJobConf, Map<String, Context> uriHandlerContextMap) throws CommandException {
    Element outputList = eAction.getChild("output-events", eAction.getNamespace());
    if (outputList != null) {
        for (Element data : (List<Element>) outputList.getChildren("data-out", eAction.getNamespace())) {
            String nocleanup = data.getAttributeValue("nocleanup");
            if (data.getChild("uris", data.getNamespace()) != null && (nocleanup == null || !nocleanup.equals("true"))) {
                String uris = data.getChild("uris", data.getNamespace()).getTextTrim();
                if (uris != null) {
                    String[] uriArr = uris.split(CoordELFunctions.INSTANCE_SEPARATOR);
                    try {
                        for (String uriStr : uriArr) {
                            URI uri = new URI(uriStr);
                            URIHandler handler = Services.get().get(URIHandlerService.class).getURIHandler(uri);
                            String schemeWithAuthority = uri.getScheme() + "://" + uri.getAuthority();
                            if (!uriHandlerContextMap.containsKey(schemeWithAuthority)) {
                                Context context = handler.getContext(uri, coordJobConf, coordJob.getUser(), false);
                                uriHandlerContextMap.put(schemeWithAuthority, context);
                            }
                            handler.delete(uri, uriHandlerContextMap.get(schemeWithAuthority));
                            LOG.info("Cleanup the output data " + uri.toString());
                        }
                    } catch (URISyntaxException e) {
                        throw new CommandException(ErrorCode.E0907, e.getMessage());
                    } catch (URIHandlerException e) {
                        throw new CommandException(ErrorCode.E0907, e.getMessage());
                    }
                }
            }
        }
    } else {
        LOG.info("No output-events defined in coordinator xml. Therefore nothing to cleanup");
    }
}
Also used : Context(org.apache.oozie.dependency.URIHandler.Context) URIHandlerException(org.apache.oozie.dependency.URIHandlerException) Element(org.jdom.Element) URIHandlerService(org.apache.oozie.service.URIHandlerService) URIHandler(org.apache.oozie.dependency.URIHandler) List(java.util.List) URISyntaxException(java.net.URISyntaxException) CommandException(org.apache.oozie.command.CommandException) URI(java.net.URI)

Example 20 with URIHandler

use of org.apache.oozie.dependency.URIHandler in project oozie by apache.

the class CoordELFunctions method coord_futureRange_sync.

private static String coord_futureRange_sync(int startOffset, int endOffset, int instance) throws Exception {
    final XLog LOG = XLog.getLog(CoordELFunctions.class);
    final Thread currentThread = Thread.currentThread();
    ELEvaluator eval = ELEvaluator.getCurrent();
    String retVal = "";
    // in minutes
    int datasetFrequency = (int) getDSFrequency();
    TimeUnit dsTimeUnit = getDSTimeUnit();
    int[] instCount = new int[1];
    Calendar nominalInstanceCal = getCurrentInstance(getActionCreationtime(), instCount);
    StringBuilder resolvedInstances = new StringBuilder();
    StringBuilder resolvedURIPaths = new StringBuilder();
    if (nominalInstanceCal != null) {
        Calendar initInstance = getInitialInstanceCal();
        nominalInstanceCal = (Calendar) initInstance.clone();
        nominalInstanceCal.add(dsTimeUnit.getCalendarUnit(), instCount[0] * datasetFrequency);
        SyncCoordDataset ds = (SyncCoordDataset) eval.getVariable(DATASET);
        if (ds == null) {
            throw new RuntimeException("Associated Dataset should be defined with key " + DATASET);
        }
        String uriTemplate = ds.getUriTemplate();
        Configuration conf = (Configuration) eval.getVariable(CONFIGURATION);
        if (conf == null) {
            throw new RuntimeException("Associated Configuration should be defined with key " + CONFIGURATION);
        }
        int available = 0, checkedInstance = 0;
        boolean resolved = false;
        String user = ParamChecker.notEmpty((String) eval.getVariable(OozieClient.USER_NAME), OozieClient.USER_NAME);
        String doneFlag = ds.getDoneFlag();
        URIHandlerService uriService = Services.get().get(URIHandlerService.class);
        URIHandler uriHandler = null;
        Context uriContext = null;
        try {
            while (instance >= checkedInstance && !currentThread.isInterrupted()) {
                ELEvaluator uriEval = getUriEvaluator(nominalInstanceCal);
                String uriPath = uriEval.evaluate(uriTemplate, String.class);
                if (uriHandler == null) {
                    URI uri = new URI(uriPath);
                    uriHandler = uriService.getURIHandler(uri);
                    uriContext = uriHandler.getContext(uri, conf, user, true);
                }
                String uriWithDoneFlag = uriHandler.getURIWithDoneFlag(uriPath, doneFlag);
                if (uriHandler.exists(new URI(uriWithDoneFlag), uriContext)) {
                    if (available == endOffset) {
                        LOG.debug("Matched future(" + available + "): " + uriWithDoneFlag);
                        resolved = true;
                        resolvedInstances.append(DateUtils.formatDateOozieTZ(nominalInstanceCal));
                        resolvedURIPaths.append(uriPath);
                        retVal = resolvedInstances.toString();
                        eval.setVariable(CoordELConstants.RESOLVED_PATH, resolvedURIPaths.toString());
                        break;
                    } else if (available >= startOffset) {
                        LOG.debug("Matched future(" + available + "): " + uriWithDoneFlag);
                        resolvedInstances.append(DateUtils.formatDateOozieTZ(nominalInstanceCal)).append(INSTANCE_SEPARATOR);
                        resolvedURIPaths.append(uriPath).append(INSTANCE_SEPARATOR);
                    }
                    available++;
                }
                // nominalInstanceCal.add(dsTimeUnit.getCalendarUnit(), datasetFrequency);
                nominalInstanceCal = (Calendar) initInstance.clone();
                instCount[0]++;
                nominalInstanceCal.add(dsTimeUnit.getCalendarUnit(), instCount[0] * datasetFrequency);
                checkedInstance++;
            // DateUtils.moveToEnd(nominalInstanceCal, getDSEndOfFlag());
            }
            if (!StringUtils.isEmpty(resolvedURIPaths.toString()) && eval.getVariable(CoordELConstants.RESOLVED_PATH) == null) {
                eval.setVariable(CoordELConstants.RESOLVED_PATH, resolvedURIPaths.toString());
            }
        } finally {
            if (uriContext != null) {
                uriContext.destroy();
            }
        }
        if (!resolved) {
            // return unchanged future function with variable 'is_resolved'
            // to 'false'
            eval.setVariable(CoordELConstants.IS_RESOLVED, Boolean.FALSE);
            if (startOffset == endOffset) {
                retVal = "${coord:future(" + startOffset + ", " + instance + ")}";
            } else {
                retVal = "${coord:futureRange(" + startOffset + ", " + endOffset + ", " + instance + ")}";
            }
        } else {
            eval.setVariable(CoordELConstants.IS_RESOLVED, Boolean.TRUE);
        }
    } else {
        // No feasible nominal time
        eval.setVariable(CoordELConstants.IS_RESOLVED, Boolean.TRUE);
        retVal = "";
    }
    return retVal;
}
Also used : Context(org.apache.oozie.dependency.URIHandler.Context) Configuration(org.apache.hadoop.conf.Configuration) XLog(org.apache.oozie.util.XLog) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) URI(java.net.URI) URIHandlerService(org.apache.oozie.service.URIHandlerService) URIHandler(org.apache.oozie.dependency.URIHandler) ELEvaluator(org.apache.oozie.util.ELEvaluator)

Aggregations

URIHandler (org.apache.oozie.dependency.URIHandler)22 URI (java.net.URI)15 URIHandlerService (org.apache.oozie.service.URIHandlerService)13 FSURIHandler (org.apache.oozie.dependency.FSURIHandler)9 IOException (java.io.IOException)5 CommandException (org.apache.oozie.command.CommandException)5 Configuration (org.apache.hadoop.conf.Configuration)4 Path (org.apache.hadoop.fs.Path)4 LauncherURIHandler (org.apache.oozie.action.hadoop.LauncherURIHandler)4 HCatURIHandler (org.apache.oozie.dependency.HCatURIHandler)4 JPAExecutorException (org.apache.oozie.executor.jpa.JPAExecutorException)4 ELEvaluator (org.apache.oozie.util.ELEvaluator)4 Element (org.jdom.Element)4 PreconditionException (org.apache.oozie.command.PreconditionException)3 ElException (org.apache.oozie.coord.ElException)3 Context (org.apache.oozie.dependency.URIHandler.Context)3 XConfiguration (org.apache.oozie.util.XConfiguration)3 XLog (org.apache.oozie.util.XLog)3 URISyntaxException (java.net.URISyntaxException)2 Calendar (java.util.Calendar)2