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());
}
}
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());
}
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");
}
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);
}
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());
}
Aggregations