Search in sources :

Example 41 with InterpreterResult

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

the class JDBCInterpreter method executeSql.

private InterpreterResult executeSql(String propertyKey, String sql, InterpreterContext interpreterContext) {
    Connection connection;
    Statement statement;
    ResultSet resultSet = null;
    String paragraphId = interpreterContext.getParagraphId();
    String user = interpreterContext.getAuthenticationInfo().getUser();
    InterpreterResult interpreterResult = new InterpreterResult(InterpreterResult.Code.SUCCESS);
    try {
        connection = getConnection(propertyKey, interpreterContext);
        if (connection == null) {
            return new InterpreterResult(Code.ERROR, "Prefix not found.");
        }
        ArrayList<String> multipleSqlArray = splitSqlQueries(sql);
        for (int i = 0; i < multipleSqlArray.size(); i++) {
            String sqlToExecute = multipleSqlArray.get(i);
            statement = connection.createStatement();
            if (statement == null) {
                return new InterpreterResult(Code.ERROR, "Prefix not found.");
            }
            try {
                getJDBCConfiguration(user).saveStatement(paragraphId, statement);
                boolean isResultSetAvailable = statement.execute(sqlToExecute);
                getJDBCConfiguration(user).setConnectionInDBDriverPoolSuccessful(propertyKey);
                if (isResultSetAvailable) {
                    resultSet = statement.getResultSet();
                    // Regards that the command is DDL.
                    if (isDDLCommand(statement.getUpdateCount(), resultSet.getMetaData().getColumnCount())) {
                        interpreterResult.add(InterpreterResult.Type.TEXT, "Query executed successfully.");
                    } else {
                        interpreterResult.add(getResults(resultSet, !containsIgnoreCase(sqlToExecute, EXPLAIN_PREDICATE)));
                    }
                } else {
                    // Response contains either an update count or there are no results.
                    int updateCount = statement.getUpdateCount();
                    interpreterResult.add(InterpreterResult.Type.TEXT, "Query executed successfully. Affected rows : " + updateCount);
                }
            } finally {
                if (resultSet != null) {
                    try {
                        resultSet.close();
                    } catch (SQLException e) {
                    /*ignored*/
                    }
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                    /*ignored*/
                    }
                }
            }
        }
        //In case user ran an insert/update/upsert statement
        if (connection != null) {
            try {
                if (!connection.getAutoCommit()) {
                    connection.commit();
                }
                connection.close();
            } catch (SQLException e) {
            /*ignored*/
            }
        }
        getJDBCConfiguration(user).removeStatement(paragraphId);
    } catch (Throwable e) {
        if (e.getCause() instanceof TTransportException && Throwables.getStackTraceAsString(e).contains("GSS") && getJDBCConfiguration(user).isConnectionInDBDriverPoolSuccessful(propertyKey)) {
            return reLoginFromKeytab(propertyKey, sql, interpreterContext, interpreterResult);
        } else {
            logger.error("Cannot run " + sql, e);
            String errorMsg = Throwables.getStackTraceAsString(e);
            try {
                closeDBPool(user, propertyKey);
            } catch (SQLException e1) {
                logger.error("Cannot close DBPool for user, propertyKey: " + user + propertyKey, e1);
            }
            interpreterResult.add(errorMsg);
            return new InterpreterResult(Code.ERROR, interpreterResult.message());
        }
    }
    return interpreterResult;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) TTransportException(org.apache.thrift.transport.TTransportException)

Example 42 with InterpreterResult

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

the class ElasticsearchInterpreterTest method testDelete.

@Theory
public void testDelete(ElasticsearchInterpreter interpreter) {
    InterpreterResult res = interpreter.interpret("delete /logs/http/unknown", null);
    assertEquals(Code.ERROR, res.code());
    res = interpreter.interpret("delete /unknown/unknown/unknown", null);
    assertEquals(Code.ERROR, res.code());
    final int testDeleteId = deleteId.decrementAndGet();
    res = interpreter.interpret("delete /logs/http/" + testDeleteId, null);
    assertEquals(Code.SUCCESS, res.code());
    assertEquals("" + testDeleteId, res.message().get(0).getData());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) DataPoint(org.junit.experimental.theories.DataPoint) Theory(org.junit.experimental.theories.Theory)

Example 43 with InterpreterResult

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

the class ElasticsearchInterpreterTest method testCount.

@Theory
public void testCount(ElasticsearchInterpreter interpreter) {
    final InterpreterContext ctx = buildContext("testCount");
    InterpreterResult res = interpreter.interpret("count /unknown", ctx);
    assertEquals(Code.ERROR, res.code());
    res = interpreter.interpret("count /logs", ctx);
    assertEquals(Code.SUCCESS, res.code());
    assertEquals("50", res.message().get(0).getData());
    assertNotNull(ctx.getAngularObjectRegistry().get("count_testCount", null, null));
    assertEquals(50l, ctx.getAngularObjectRegistry().get("count_testCount", null, null).get());
    res = interpreter.interpret("count /logs { \"query\": { \"match\": { \"status\": 500 } } }", ctx);
    assertEquals(Code.SUCCESS, res.code());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) Theory(org.junit.experimental.theories.Theory)

Example 44 with InterpreterResult

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

the class ElasticsearchInterpreterTest method testIndex.

@Theory
public void testIndex(ElasticsearchInterpreter interpreter) {
    InterpreterResult res = interpreter.interpret("index /logs { \"date\": \"" + new Date() + "\", \"method\": \"PUT\", \"status\": \"500\" }", null);
    assertEquals(Code.ERROR, res.code());
    res = interpreter.interpret("index /logs/http { bad ", null);
    assertEquals(Code.ERROR, res.code());
    res = interpreter.interpret("index /logs/http { \"date\": \"2015-12-06T14:54:23.368Z\", \"method\": \"PUT\", \"status\": \"500\" }", null);
    assertEquals(Code.SUCCESS, res.code());
    res = interpreter.interpret("index /logs/http/1000 { \"date\": \"2015-12-06T14:54:23.368Z\", \"method\": \"PUT\", \"status\": \"500\" }", null);
    assertEquals(Code.SUCCESS, res.code());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Date(java.util.Date) Theory(org.junit.experimental.theories.Theory)

Example 45 with InterpreterResult

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

the class ElasticsearchInterpreterTest method testGet.

@Theory
public void testGet(ElasticsearchInterpreter interpreter) {
    final InterpreterContext ctx = buildContext("get");
    InterpreterResult res = interpreter.interpret("get /logs/http/unknown", ctx);
    assertEquals(Code.ERROR, res.code());
    res = interpreter.interpret("get /logs/http/unknown/unknown", ctx);
    assertEquals(Code.ERROR, res.code());
    res = interpreter.interpret("get /unknown/unknown/unknown", ctx);
    assertEquals(Code.ERROR, res.code());
    res = interpreter.interpret("get /logs/http/very/strange/id#1", ctx);
    assertEquals(Code.SUCCESS, res.code());
    res = interpreter.interpret("get /logs/http/4", ctx);
    assertEquals(Code.SUCCESS, res.code());
    res = interpreter.interpret("get /logs/_all/4", ctx);
    assertEquals(Code.SUCCESS, res.code());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) Theory(org.junit.experimental.theories.Theory)

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