Search in sources :

Example 56 with InterpreterResult

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

the class FlinkInterpreterTest method testSimpleStatementWithSystemOutput.

@Test
public void testSimpleStatementWithSystemOutput() {
    InterpreterResult result = flink.interpret("val a=1", context);
    result = flink.interpret("System.out.print(a)", context);
    assertEquals("1", result.message().get(0).getData());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Test(org.junit.Test)

Example 57 with InterpreterResult

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

the class FlinkInterpreterTest method testWordCount.

@Test
public void testWordCount() {
    flink.interpret("val text = benv.fromElements(\"To be or not to be\")", context);
    flink.interpret("val counts = text.flatMap { _.toLowerCase.split(\" \") }.map { (_, 1) }.groupBy(0).sum(1)", context);
    InterpreterResult result = flink.interpret("counts.print()", context);
    assertEquals(Code.SUCCESS, result.code());
    String[] expectedCounts = { "(to,2)", "(be,2)", "(or,1)", "(not,1)" };
    Arrays.sort(expectedCounts);
    String[] counts = result.message().get(0).getData().split("\n");
    Arrays.sort(counts);
    assertArrayEquals(expectedCounts, counts);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Test(org.junit.Test)

Example 58 with InterpreterResult

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

the class GeodeOqlInterpreter method executeOql.

private InterpreterResult executeOql(String oql) {
    try {
        if (getExceptionOnConnect() != null) {
            return new InterpreterResult(Code.ERROR, getExceptionOnConnect().getMessage());
        }
        @SuppressWarnings("unchecked") SelectResults<Object> results = (SelectResults<Object>) getQueryService().newQuery(oql).execute();
        StringBuilder msg = new StringBuilder(TABLE_MAGIC_TAG);
        boolean isTableHeaderSet = false;
        Iterator<Object> iterator = results.iterator();
        int rowDisplayCount = 0;
        while (iterator.hasNext() && (rowDisplayCount < getMaxResult())) {
            Object entry = iterator.next();
            rowDisplayCount++;
            if (entry instanceof Number) {
                handleNumberEntry(isTableHeaderSet, entry, msg);
            } else if (entry instanceof Struct) {
                handleStructEntry(isTableHeaderSet, entry, msg);
            } else if (entry instanceof PdxInstance) {
                handlePdxInstanceEntry(isTableHeaderSet, entry, msg);
            } else {
                handleUnsupportedTypeEntry(isTableHeaderSet, entry, msg);
            }
            isTableHeaderSet = true;
            msg.append(NEWLINE);
        }
        return new InterpreterResult(Code.SUCCESS, msg.toString());
    } catch (Exception ex) {
        logger.error("Cannot run " + oql, ex);
        return new InterpreterResult(Code.ERROR, ex.getMessage());
    }
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Struct(com.gemstone.gemfire.cache.query.Struct) SelectResults(com.gemstone.gemfire.cache.query.SelectResults) PdxInstance(com.gemstone.gemfire.pdx.PdxInstance)

Example 59 with InterpreterResult

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

the class GeodeOqlInterpreterTest method testOql.

private void testOql(Iterator<Object> queryResponseIterator, String expectedOutput, int maxResult) throws Exception {
    GeodeOqlInterpreter spyGeodeOqlInterpreter = spy(new GeodeOqlInterpreter(new Properties()));
    QueryService mockQueryService = mock(QueryService.class, RETURNS_DEEP_STUBS);
    when(spyGeodeOqlInterpreter.getQueryService()).thenReturn(mockQueryService);
    when(spyGeodeOqlInterpreter.getMaxResult()).thenReturn(maxResult);
    @SuppressWarnings("unchecked") SelectResults<Object> mockResults = mock(SelectResults.class);
    when(mockQueryService.newQuery(eq(OQL_QUERY)).execute()).thenReturn(mockResults);
    when(mockResults.iterator()).thenReturn(queryResponseIterator);
    InterpreterResult interpreterResult = spyGeodeOqlInterpreter.interpret(OQL_QUERY, null);
    assertEquals(Code.SUCCESS, interpreterResult.code());
    assertEquals(expectedOutput, interpreterResult.message().get(0).getData());
}
Also used : QueryService(com.gemstone.gemfire.cache.query.QueryService) GeodeOqlInterpreter(org.apache.zeppelin.geode.GeodeOqlInterpreter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Properties(java.util.Properties)

Example 60 with InterpreterResult

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

the class GeodeOqlInterpreterTest method oqlWithQueryException.

@Test
public void oqlWithQueryException() throws Exception {
    GeodeOqlInterpreter spyGeodeOqlInterpreter = spy(new GeodeOqlInterpreter(new Properties()));
    when(spyGeodeOqlInterpreter.getExceptionOnConnect()).thenReturn(new RuntimeException("Test Exception On Connect"));
    InterpreterResult interpreterResult = spyGeodeOqlInterpreter.interpret(OQL_QUERY, null);
    assertEquals(Code.ERROR, interpreterResult.code());
    assertEquals("Test Exception On Connect", interpreterResult.message().get(0).getData());
}
Also used : GeodeOqlInterpreter(org.apache.zeppelin.geode.GeodeOqlInterpreter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)159 Test (org.junit.Test)68 Properties (java.util.Properties)17 File (java.io.File)10 IOException (java.io.IOException)9 AlluxioURI (alluxio.AlluxioURI)8 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)7 Theory (org.junit.experimental.theories.Theory)7 FileWriter (java.io.FileWriter)6 PrintStream (java.io.PrintStream)5 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)5 Code (org.apache.zeppelin.interpreter.InterpreterResult.Code)5 InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)5 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)4 FileInStream (alluxio.client.file.FileInStream)3 JsonObject (com.google.gson.JsonObject)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3