Search in sources :

Example 21 with ResourcePool

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

the class MockInterpreterResourcePool method interpret.

@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
    String[] stmt = st.split(" ");
    String cmd = stmt[0];
    String noteId = null;
    String paragraphId = null;
    String name = null;
    if (stmt.length >= 2) {
        String[] npn = stmt[1].split(":");
        if (npn.length >= 3) {
            noteId = npn[0];
            paragraphId = npn[1];
            name = npn[2];
        } else {
            name = stmt[1];
        }
    }
    String value = null;
    if (stmt.length >= 3) {
        value = stmt[2];
    }
    ResourcePool resourcePool = context.getResourcePool();
    Object ret = null;
    if (cmd.equals("put")) {
        resourcePool.put(noteId, paragraphId, name, value);
    } else if (cmd.equalsIgnoreCase("get")) {
        Resource resource = resourcePool.get(noteId, paragraphId, name);
        if (resource != null) {
            ret = resourcePool.get(noteId, paragraphId, name).get();
        } else {
            ret = "";
        }
    } else if (cmd.equals("remove")) {
        ret = resourcePool.remove(noteId, paragraphId, name);
    } else if (cmd.equals("getAll")) {
        ret = resourcePool.getAll();
    } else if (cmd.equals("invoke")) {
        Resource resource = resourcePool.get(noteId, paragraphId, name);
        LOGGER.info("Resource: " + resource);
        if (stmt.length >= 4) {
            Resource res = resource.invokeMethod(value, stmt[3]);
            LOGGER.info("After invokeMethod: " + resource);
            ret = res.get();
        } else {
            ret = resource.invokeMethod(value);
            LOGGER.info("After invokeMethod: " + ret);
        }
    }
    try {
        // wait for watcher executed
        Thread.sleep(500);
    } catch (InterruptedException e) {
    }
    Gson gson = new Gson();
    return new InterpreterResult(Code.SUCCESS, gson.toJson(ret));
}
Also used : Resource(org.apache.zeppelin.resource.Resource) Gson(com.google.gson.Gson) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ResourcePool(org.apache.zeppelin.resource.ResourcePool)

Example 22 with ResourcePool

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

the class ZeppelinContext method put.

/**
 * Add object into resource pool
 *
 * @param name
 * @param value
 */
@ZeppelinApi
public void put(String name, Object value) {
    ResourcePool resourcePool = interpreterContext.getResourcePool();
    resourcePool.put(name, value);
}
Also used : ResourcePool(org.apache.zeppelin.resource.ResourcePool) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi)

Example 23 with ResourcePool

use of org.apache.zeppelin.resource.ResourcePool 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 24 with ResourcePool

use of org.apache.zeppelin.resource.ResourcePool 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 25 with ResourcePool

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

the class Helium method suggestApp.

public HeliumPackageSuggestion suggestApp(Paragraph paragraph) {
    HeliumPackageSuggestion suggestion = new HeliumPackageSuggestion();
    Interpreter intp = null;
    try {
        intp = paragraph.getBindedInterpreter();
    } catch (InterpreterNotFoundException e) {
        return suggestion;
    }
    ResourcePool resourcePool = intp.getInterpreterGroup().getResourcePool();
    ResourceSet allResources;
    if (resourcePool != null) {
        if (resourcePool instanceof DistributedResourcePool) {
            allResources = ((DistributedResourcePool) resourcePool).getAll(true);
        } else {
            allResources = resourcePool.getAll();
        }
    } else {
        allResources = interpreterSettingManager.getAllResources();
    }
    for (List<HeliumPackageSearchResult> pkgs : allPackages.values()) {
        for (HeliumPackageSearchResult pkg : pkgs) {
            if (pkg.getPkg().getType() == HeliumType.APPLICATION && pkg.isEnabled()) {
                ResourceSet resources = ApplicationLoader.findRequiredResourceSet(pkg.getPkg().getResources(), paragraph.getNote().getId(), paragraph.getId(), allResources);
                if (resources == null) {
                    continue;
                } else {
                    suggestion.addAvailablePackage(pkg);
                }
                break;
            }
        }
    }
    suggestion.sort();
    return suggestion;
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterNotFoundException(org.apache.zeppelin.interpreter.InterpreterNotFoundException) ResourcePool(org.apache.zeppelin.resource.ResourcePool) DistributedResourcePool(org.apache.zeppelin.resource.DistributedResourcePool) ResourceSet(org.apache.zeppelin.resource.ResourceSet) DistributedResourcePool(org.apache.zeppelin.resource.DistributedResourcePool)

Aggregations

ResourcePool (org.apache.zeppelin.resource.ResourcePool)27 Resource (org.apache.zeppelin.resource.Resource)11 ZeppelinApi (org.apache.zeppelin.annotation.ZeppelinApi)9 Gson (com.google.gson.Gson)6 TException (org.apache.thrift.TException)6 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)6 ResourceSet (org.apache.zeppelin.resource.ResourceSet)6 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)5 Client (org.apache.zeppelin.interpreter.thrift.RemoteInterpreterService.Client)5 ByteBuffer (java.nio.ByteBuffer)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 AngularObject (org.apache.zeppelin.display.AngularObject)4 Credentials (org.apache.zeppelin.user.Credentials)4 UserCredentials (org.apache.zeppelin.user.UserCredentials)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 TypeToken (com.google.gson.reflect.TypeToken)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2