Search in sources :

Example 91 with InterpreterResult

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

the class ElasticsearchInterpreter method interpret.

@Override
public InterpreterResult interpret(String cmd, InterpreterContext interpreterContext) {
    logger.info("Run Elasticsearch command '" + cmd + "'");
    if (StringUtils.isEmpty(cmd) || StringUtils.isEmpty(cmd.trim())) {
        return new InterpreterResult(InterpreterResult.Code.SUCCESS);
    }
    int currentResultSize = resultSize;
    if (elsClient == null) {
        return new InterpreterResult(InterpreterResult.Code.ERROR, "Problem with the Elasticsearch client, please check your configuration (host, port,...)");
    }
    String[] items = StringUtils.split(cmd.trim(), " ", 3);
    // Process some specific commands (help, size, ...)
    if ("help".equalsIgnoreCase(items[0])) {
        return processHelp(InterpreterResult.Code.SUCCESS, null);
    }
    if ("size".equalsIgnoreCase(items[0])) {
        // In this case, the line with size must be followed by a search,
        // so we will continue with the next lines
        final String[] lines = StringUtils.split(cmd.trim(), "\n", 2);
        if (lines.length < 2) {
            return processHelp(InterpreterResult.Code.ERROR, "Size cmd must be followed by a search");
        }
        final String[] sizeLine = StringUtils.split(lines[0], " ", 2);
        if (sizeLine.length != 2) {
            return processHelp(InterpreterResult.Code.ERROR, "Right format is : size <value>");
        }
        currentResultSize = Integer.parseInt(sizeLine[1]);
        items = StringUtils.split(lines[1].trim(), " ", 3);
    }
    if (items.length < 2) {
        return processHelp(InterpreterResult.Code.ERROR, "Arguments missing");
    }
    final String method = items[0];
    final String url = items[1];
    final String data = items.length > 2 ? items[2].trim() : null;
    final String[] urlItems = StringUtils.split(url.trim(), "/");
    try {
        if ("get".equalsIgnoreCase(method)) {
            return processGet(urlItems, interpreterContext);
        } else if ("count".equalsIgnoreCase(method)) {
            return processCount(urlItems, data, interpreterContext);
        } else if ("search".equalsIgnoreCase(method)) {
            return processSearch(urlItems, data, currentResultSize, interpreterContext);
        } else if ("index".equalsIgnoreCase(method)) {
            return processIndex(urlItems, data);
        } else if ("delete".equalsIgnoreCase(method)) {
            return processDelete(urlItems);
        }
        return processHelp(InterpreterResult.Code.ERROR, "Unknown command");
    } catch (final Exception e) {
        return new InterpreterResult(InterpreterResult.Code.ERROR, "Error : " + e.getMessage());
    }
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) IOException(java.io.IOException)

Example 92 with InterpreterResult

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

the class ElasticsearchInterpreter method processCount.

/**
   * Processes a "count" request.
   *
   * @param urlItems Items of the URL
   * @param data May contains the JSON of the request
   * @param interpreterContext Instance of the context
   * @return Result of the count request, it contains the total hits
   */
private InterpreterResult processCount(String[] urlItems, String data, InterpreterContext interpreterContext) {
    if (urlItems.length > 2) {
        return new InterpreterResult(InterpreterResult.Code.ERROR, "Bad URL (it should be /index1,index2,.../type1,type2,...)");
    }
    final ActionResponse response = searchData(urlItems, data, 0);
    addAngularObject(interpreterContext, "count", response.getTotalHits());
    return new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, "" + response.getTotalHits());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 93 with InterpreterResult

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

the class ElasticsearchInterpreter method processDelete.

/**
   * Processes a "delete" request.
   *
   * @param urlItems Items of the URL
   * @return Result of the delete request, it contains the id of the deleted document
   */
private InterpreterResult processDelete(String[] urlItems) {
    final String[] indexTypeId = getIndexTypeId(urlItems);
    if (indexTypeId == null) {
        return new InterpreterResult(InterpreterResult.Code.ERROR, "Bad URL (it should be /index/type/id)");
    }
    final ActionResponse response = elsClient.delete(indexTypeId[0], indexTypeId[1], indexTypeId[2]);
    if (response.isSucceeded()) {
        return new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, response.getHit().getId());
    }
    return new InterpreterResult(InterpreterResult.Code.ERROR, "Document not found");
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 94 with InterpreterResult

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

the class AlluxioInterpreterTest method mkdirTest.

@Test
public void mkdirTest() throws IOException, AlluxioException {
    String qualifiedPath = "tachyon://" + mLocalAlluxioCluster.getMasterHostname() + ":" + mLocalAlluxioCluster.getMasterPort() + "/root/testFile1";
    InterpreterResult output = alluxioInterpreter.interpret("mkdir " + qualifiedPath, null);
    boolean existsDir = fs.exists(new AlluxioURI("/root/testFile1"));
    Assert.assertEquals("Successfully created directory " + qualifiedPath + "\n\n", output.message().get(0).getData());
    Assert.assertTrue(existsDir);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) AlluxioURI(alluxio.AlluxioURI)

Example 95 with InterpreterResult

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

the class BeamInterpreterTest method testStaticReplWithoutMain.

@Test
public void testStaticReplWithoutMain() {
    StringBuffer sourceCode = new StringBuffer();
    sourceCode.append("package org.mdkt;\n");
    sourceCode.append("public class HelloClass {\n");
    sourceCode.append("   public String hello() { return \"hello\"; }");
    sourceCode.append("}");
    InterpreterResult res = beam.interpret(sourceCode.toString(), context);
    assertEquals(InterpreterResult.Code.ERROR, res.code());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) 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