Search in sources :

Example 66 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class RemoteInterpreterTest method testExecuteCorrectPrecode.

@Test
public void testExecuteCorrectPrecode() throws TTransportException, IOException, InterpreterException {
    interpreterSetting.getOption().setPerUser(InterpreterOption.SHARED);
    interpreterSetting.setProperty("zeppelin.SleepInterpreter.precode", "1");
    Interpreter interpreter1 = interpreterSetting.getInterpreter("user1", note1Id, "sleep");
    InterpreterContext context1 = createDummyInterpreterContext();
    assertEquals(Code.SUCCESS, interpreter1.interpret("10", context1).code());
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Example 67 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class ParagraphTest method returnUnchangedResultsWithDifferentUser.

@Ignore
public void returnUnchangedResultsWithDifferentUser() throws Throwable {
    Note mockNote = mock(Note.class);
    when(mockNote.getCredentials()).thenReturn(mock(Credentials.class));
    Paragraph spyParagraph = spy(new Paragraph("para_1", mockNote, null));
    Interpreter mockInterpreter = mock(Interpreter.class);
    spyParagraph.setInterpreter(mockInterpreter);
    doReturn(mockInterpreter).when(spyParagraph).getBindedInterpreter();
    ManagedInterpreterGroup mockInterpreterGroup = mock(ManagedInterpreterGroup.class);
    when(mockInterpreter.getInterpreterGroup()).thenReturn(mockInterpreterGroup);
    when(mockInterpreterGroup.getId()).thenReturn("mock_id_1");
    when(mockInterpreterGroup.getAngularObjectRegistry()).thenReturn(mock(AngularObjectRegistry.class));
    when(mockInterpreterGroup.getResourcePool()).thenReturn(mock(ResourcePool.class));
    List<InterpreterSetting> spyInterpreterSettingList = spy(new ArrayList<>());
    InterpreterSetting mockInterpreterSetting = mock(InterpreterSetting.class);
    when(mockInterpreterGroup.getInterpreterSetting()).thenReturn(mockInterpreterSetting);
    InterpreterOption mockInterpreterOption = mock(InterpreterOption.class);
    when(mockInterpreterSetting.getOption()).thenReturn(mockInterpreterOption);
    when(mockInterpreterOption.permissionIsSet()).thenReturn(false);
    when(mockInterpreterSetting.getStatus()).thenReturn(Status.READY);
    when(mockInterpreterSetting.getId()).thenReturn("mock_id_1");
    when(mockInterpreterSetting.getOrCreateInterpreterGroup(anyString(), anyString())).thenReturn(mockInterpreterGroup);
    when(mockInterpreterSetting.isUserAuthorized(any(List.class))).thenReturn(true);
    spyInterpreterSettingList.add(mockInterpreterSetting);
    when(mockNote.getId()).thenReturn("any_id");
    when(mockInterpreter.getFormType()).thenReturn(FormType.NONE);
    ParagraphJobListener mockJobListener = mock(ParagraphJobListener.class);
    doReturn(mockJobListener).when(spyParagraph).getListener();
    InterpreterResult mockInterpreterResult = mock(InterpreterResult.class);
    when(mockInterpreter.interpret(anyString(), Mockito.<InterpreterContext>any())).thenReturn(mockInterpreterResult);
    when(mockInterpreterResult.code()).thenReturn(Code.SUCCESS);
    // Actual test
    List<InterpreterResultMessage> result1 = new ArrayList<>();
    result1.add(new InterpreterResultMessage(Type.TEXT, "result1"));
    when(mockInterpreterResult.message()).thenReturn(result1);
    AuthenticationInfo user1 = new AuthenticationInfo("user1");
    spyParagraph.setAuthenticationInfo(user1);
    spyParagraph.jobRun();
    Paragraph p1 = spyParagraph.getUserParagraph(user1.getUser());
    mockInterpreterResult = mock(InterpreterResult.class);
    when(mockInterpreter.interpret(anyString(), Mockito.<InterpreterContext>any())).thenReturn(mockInterpreterResult);
    when(mockInterpreterResult.code()).thenReturn(Code.SUCCESS);
    List<InterpreterResultMessage> result2 = new ArrayList<>();
    result2.add(new InterpreterResultMessage(Type.TEXT, "result2"));
    when(mockInterpreterResult.message()).thenReturn(result2);
    AuthenticationInfo user2 = new AuthenticationInfo("user2");
    spyParagraph.setAuthenticationInfo(user2);
    spyParagraph.jobRun();
    Paragraph p2 = spyParagraph.getUserParagraph(user2.getUser());
    assertNotEquals(p1.getReturn().toString(), p2.getReturn().toString());
    assertEquals(p1, spyParagraph.getUserParagraph(user1.getUser()));
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) ArrayList(java.util.ArrayList) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ResourcePool(org.apache.zeppelin.resource.ResourcePool) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) InterpreterOption(org.apache.zeppelin.interpreter.InterpreterOption) ArrayList(java.util.ArrayList) List(java.util.List) UserCredentials(org.apache.zeppelin.user.UserCredentials) Credentials(org.apache.zeppelin.user.Credentials) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Ignore(org.junit.Ignore)

Example 68 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class KSQLInterpreterTest method shouldRenderKSQLSelectAsTable.

@Test
public void shouldRenderKSQLSelectAsTable() throws InterpreterException, IOException, InterruptedException {
    // given
    Properties p = new Properties();
    p.putAll(PROPS);
    KSQLRestService service = Mockito.mock(KSQLRestService.class);
    Stubber stubber = Mockito.doAnswer((invocation) -> {
        Consumer<KSQLResponse> callback = (Consumer<KSQLResponse>) invocation.getArguments()[2];
        IntStream.range(1, 5).forEach(i -> {
            Map<String, Object> map = new HashMap<>();
            if (i == 4) {
                map.put("row", null);
                map.put("terminal", true);
            } else {
                map.put("row", Collections.singletonMap("columns", Arrays.asList("value " + i)));
                map.put("terminal", false);
            }
            callback.accept(new KSQLResponse(Arrays.asList("fieldName"), map));
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return null;
    });
    stubber.when(service).executeQuery(Mockito.any(String.class), Mockito.anyString(), Mockito.any(Consumer.class));
    Interpreter interpreter = new KSQLInterpreter(p, service);
    // when
    String query = "select * from orders";
    interpreter.interpret(query, context);
    // then
    String expected = "%table fieldName\n" + "value 1\n" + "value 2\n" + "value 3\n";
    context.out.flush();
    assertEquals(1, context.out.toInterpreterResultMessage().size());
    assertEquals(expected, context.out.toInterpreterResultMessage().get(0).toString());
    assertEquals(InterpreterResult.Type.TABLE, context.out.toInterpreterResultMessage().get(0).getType());
    interpreter.close();
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) HashMap(java.util.HashMap) Properties(java.util.Properties) Consumer(java.util.function.Consumer) Stubber(org.mockito.stubbing.Stubber) Test(org.junit.Test)

Example 69 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter 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)

Example 70 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class IPySparkInterpreterTest method startInterpreter.

@Override
protected void startInterpreter(Properties properties) throws InterpreterException {
    InterpreterContext context = getInterpreterContext();
    context.setIntpEventClient(mockIntpEventClient);
    InterpreterContext.set(context);
    LazyOpenInterpreter sparkInterpreter = new LazyOpenInterpreter(new SparkInterpreter(properties));
    intpGroup = new InterpreterGroup();
    intpGroup.put("session_1", new ArrayList<Interpreter>());
    intpGroup.get("session_1").add(sparkInterpreter);
    sparkInterpreter.setInterpreterGroup(intpGroup);
    LazyOpenInterpreter pySparkInterpreter = new LazyOpenInterpreter(new PySparkInterpreter(properties));
    intpGroup.get("session_1").add(pySparkInterpreter);
    pySparkInterpreter.setInterpreterGroup(intpGroup);
    interpreter = new LazyOpenInterpreter(new IPySparkInterpreter(properties));
    intpGroup.get("session_1").add(interpreter);
    interpreter.setInterpreterGroup(intpGroup);
    pySparkInterpreter.open();
    interpreter.open();
}
Also used : LazyOpenInterpreter(org.apache.zeppelin.interpreter.LazyOpenInterpreter) Interpreter(org.apache.zeppelin.interpreter.Interpreter) LazyOpenInterpreter(org.apache.zeppelin.interpreter.LazyOpenInterpreter) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext)

Aggregations

Interpreter (org.apache.zeppelin.interpreter.Interpreter)85 Test (org.junit.Test)46 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)42 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)30 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)27 LazyOpenInterpreter (org.apache.zeppelin.interpreter.LazyOpenInterpreter)26 AbstractInterpreterTest (org.apache.zeppelin.interpreter.AbstractInterpreterTest)21 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)21 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)20 Properties (java.util.Properties)19 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)11 ExecutionContext (org.apache.zeppelin.interpreter.ExecutionContext)11 IOException (java.io.IOException)10 InterpreterOutput (org.apache.zeppelin.interpreter.InterpreterOutput)10 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)9 WrappedInterpreter (org.apache.zeppelin.interpreter.WrappedInterpreter)7 ArrayList (java.util.ArrayList)6 InterpreterNotFoundException (org.apache.zeppelin.interpreter.InterpreterNotFoundException)6 ManagedInterpreterGroup (org.apache.zeppelin.interpreter.ManagedInterpreterGroup)6 Before (org.junit.Before)6