Search in sources :

Example 16 with Resource

use of org.apache.zeppelin.resource.Resource in project zeppelin by apache.

the class ApplicationLoader method findRequiredResourceSet.

static ResourceSet findRequiredResourceSet(String[][] requiredResources, String noteId, String paragraphId, ResourceSet resources) {
    ResourceSet args = new ResourceSet();
    if (requiredResources == null || requiredResources.length == 0) {
        return args;
    }
    resources = resources.filterByNoteId(noteId).filterByParagraphId(paragraphId);
    for (String[] requires : requiredResources) {
        args.clear();
        for (String require : requires) {
            boolean found = false;
            for (Resource r : resources) {
                if (require.startsWith(":") && r.getClassName().equals(require.substring(1))) {
                    found = true;
                } else if (r.getResourceId().getName().equals(require)) {
                    found = true;
                }
                if (found) {
                    args.add(r);
                    break;
                }
            }
            if (found == false) {
                break;
            }
        }
        if (args.size() == requires.length) {
            return args;
        }
    }
    return null;
}
Also used : Resource(org.apache.zeppelin.resource.Resource) ResourceSet(org.apache.zeppelin.resource.ResourceSet)

Example 17 with Resource

use of org.apache.zeppelin.resource.Resource in project zeppelin by apache.

the class AbstractInterpreter method interpolate.

static String interpolate(String cmd, ResourcePool resourcePool) {
    StringBuilder sb = new StringBuilder();
    Matcher m;
    String st = cmd;
    while ((m = VARIABLES.matcher(st)).matches()) {
        sb.append(m.group(1));
        String varPat = m.group(2);
        if (VARIABLE_IN_BRACES.matcher(varPat).matches()) {
            // substitute {variable} only if 'variable' has a value ...
            Resource resource = resourcePool.get(varPat.substring(1, varPat.length() - 1));
            Object variableValue = resource == null ? null : resource.get();
            if (variableValue != null)
                sb.append(variableValue);
            else
                return cmd;
        } else if (VARIABLE_IN_DOUBLE_BRACES.matcher(varPat).matches()) {
            // escape {{text}} ...
            sb.append("{").append(varPat, 2, varPat.length() - 2).append("}");
        } else {
            // mismatched {{ }} or more than 2 braces ...
            return cmd;
        }
        st = m.group(3);
    }
    sb.append(st);
    return sb.toString();
}
Also used : Matcher(java.util.regex.Matcher) Resource(org.apache.zeppelin.resource.Resource)

Example 18 with Resource

use of org.apache.zeppelin.resource.Resource in project zeppelin by apache.

the class ZeppelinContext method get.

/**
 * Get object from resource pool
 * Search local process first and then the other processes
 *
 * @param name
 * @param clazz  The class of the returned value
 * @return null if resource not found
 */
@ZeppelinApi
public <T> T get(String name, Class<T> clazz) {
    ResourcePool resourcePool = interpreterContext.getResourcePool();
    Resource resource = resourcePool.get(name);
    if (resource != null) {
        return resource.get(clazz);
    } else {
        return null;
    }
}
Also used : Resource(org.apache.zeppelin.resource.Resource) ResourcePool(org.apache.zeppelin.resource.ResourcePool) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi)

Example 19 with Resource

use of org.apache.zeppelin.resource.Resource in project zeppelin by apache.

the class ZeppelinContext method get.

/**
 * Get object from resource pool
 * Search local process first and then the other processes
 *
 * @param name
 * @return null if resource not found
 */
@ZeppelinApi
public Object get(String name) {
    ResourcePool resourcePool = interpreterContext.getResourcePool();
    Resource resource = resourcePool.get(name);
    if (resource != null) {
        return resource.get();
    } else {
        return null;
    }
}
Also used : Resource(org.apache.zeppelin.resource.Resource) ResourcePool(org.apache.zeppelin.resource.ResourcePool) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi)

Example 20 with Resource

use of org.apache.zeppelin.resource.Resource in project zeppelin by apache.

the class RemoteInterpreterEventClient method invokeMethod.

/**
 * Invoke method and save result in resourcePool as another resource
 *
 * @param resourceId
 * @param methodName
 * @param paramTypes
 * @param params
 * @param returnResourceName
 * @return
 */
@Override
public Resource invokeMethod(ResourceId resourceId, String methodName, Class[] paramTypes, Object[] params, String returnResourceName) {
    LOGGER.debug("Request Invoke method {} of Resource {}", methodName, resourceId.getName());
    InvokeResourceMethodEventMessage invokeMethod = new InvokeResourceMethodEventMessage(resourceId, methodName, paramTypes, params, returnResourceName);
    try {
        ByteBuffer serializedResource = callRemoteFunction(client -> client.invokeMethod(intpGroupId, invokeMethod.toJson()));
        Resource deserializedResource = (Resource) Resource.deserializeObject(serializedResource);
        RemoteResource remoteResource = RemoteResource.fromJson(GSON.toJson(deserializedResource));
        remoteResource.setResourcePoolConnector(this);
        return remoteResource;
    } catch (IOException | ClassNotFoundException e) {
        LOGGER.error("Failed to invoke method", e);
        return null;
    }
}
Also used : RemoteResource(org.apache.zeppelin.resource.RemoteResource) Resource(org.apache.zeppelin.resource.Resource) RemoteResource(org.apache.zeppelin.resource.RemoteResource) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Resource (org.apache.zeppelin.resource.Resource)23 ResourcePool (org.apache.zeppelin.resource.ResourcePool)11 Gson (com.google.gson.Gson)6 IOException (java.io.IOException)6 LinkedList (java.util.LinkedList)6 ResourceSet (org.apache.zeppelin.resource.ResourceSet)6 ZeppelinApi (org.apache.zeppelin.annotation.ZeppelinApi)5 ByteBuffer (java.nio.ByteBuffer)4 TException (org.apache.thrift.TException)4 AngularObject (org.apache.zeppelin.display.AngularObject)3 TypeToken (com.google.gson.reflect.TypeToken)2 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 TTransportException (org.apache.thrift.transport.TTransportException)2 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)2