Search in sources :

Example 51 with InterpreterResult

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

the class ElasticsearchInterpreter method buildAggResponseMessage.

private InterpreterResult buildAggResponseMessage(Aggregations aggregations) {
    // Only the result of the first aggregation is returned
    //
    final Aggregation agg = aggregations.asList().get(0);
    InterpreterResult.Type resType = InterpreterResult.Type.TEXT;
    String resMsg = "";
    if (agg instanceof InternalMetricsAggregation) {
        resMsg = XContentHelper.toString((InternalMetricsAggregation) agg).toString();
    } else if (agg instanceof InternalSingleBucketAggregation) {
        resMsg = XContentHelper.toString((InternalSingleBucketAggregation) agg).toString();
    } else if (agg instanceof InternalMultiBucketAggregation) {
        final Set<String> headerKeys = new HashSet<>();
        final List<Map<String, Object>> buckets = new LinkedList<>();
        final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;
        for (final MultiBucketsAggregation.Bucket bucket : multiBucketAgg.getBuckets()) {
            try {
                final XContentBuilder builder = XContentFactory.jsonBuilder();
                bucket.toXContent(builder, null);
                final Map<String, Object> bucketMap = JsonFlattener.flattenAsMap(builder.string());
                headerKeys.addAll(bucketMap.keySet());
                buckets.add(bucketMap);
            } catch (final IOException e) {
                logger.error("Processing bucket: " + e.getMessage(), e);
            }
        }
        final StringBuffer buffer = new StringBuffer();
        final String[] keys = headerKeys.toArray(new String[0]);
        for (final String key : keys) {
            buffer.append("\t" + key);
        }
        buffer.deleteCharAt(0);
        for (final Map<String, Object> bucket : buckets) {
            buffer.append("\n");
            for (final String key : keys) {
                buffer.append(bucket.get(key)).append("\t");
            }
            buffer.deleteCharAt(buffer.length() - 1);
        }
        resType = InterpreterResult.Type.TABLE;
        resMsg = buffer.toString();
    }
    return new InterpreterResult(InterpreterResult.Code.SUCCESS, resType, resMsg);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) IOException(java.io.IOException) InternalMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation) LinkedList(java.util.LinkedList) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) InternalMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) JsonObject(com.google.gson.JsonObject) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HashSet(java.util.HashSet)

Example 52 with InterpreterResult

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

the class ElasticsearchInterpreter method buildAggResponseMessage.

private InterpreterResult buildAggResponseMessage(List<AggWrapper> aggregations) {
    final InterpreterResult.Type resType = InterpreterResult.Type.TABLE;
    String resMsg = "";
    final Set<String> headerKeys = new HashSet<>();
    final List<Map<String, Object>> buckets = new LinkedList<>();
    for (final AggWrapper aggregation : aggregations) {
        final Map<String, Object> bucketMap = JsonFlattener.flattenAsMap(aggregation.getResult());
        headerKeys.addAll(bucketMap.keySet());
        buckets.add(bucketMap);
    }
    final StringBuffer buffer = new StringBuffer();
    final String[] keys = headerKeys.toArray(new String[0]);
    for (final String key : keys) {
        buffer.append("\t" + key);
    }
    buffer.deleteCharAt(0);
    for (final Map<String, Object> bucket : buckets) {
        buffer.append("\n");
        for (final String key : keys) {
            buffer.append(bucket.get(key)).append("\t");
        }
        buffer.deleteCharAt(buffer.length() - 1);
    }
    resMsg = buffer.toString();
    return new InterpreterResult(InterpreterResult.Code.SUCCESS, resType, resMsg);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) AggWrapper(org.apache.zeppelin.elasticsearch.action.AggWrapper) LinkedList(java.util.LinkedList) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 53 with InterpreterResult

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

the class ElasticsearchInterpreter method processSearch.

/**
   * Processes a "search" request.
   *
   * @param urlItems Items of the URL
   * @param data May contains the JSON of the request
   * @param size Limit of result set
   * @param interpreterContext Instance of the context
   * @return Result of the search request, it contains a tab-formatted string of the matching hits
   */
private InterpreterResult processSearch(String[] urlItems, String data, int size, 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, size);
    addAngularObject(interpreterContext, "search", (response.getAggregations() != null && response.getAggregations().size() > 0) ? response.getAggregations() : response.getHits());
    return buildResponseMessage(response);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 54 with InterpreterResult

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

the class ElasticsearchInterpreter method processGet.

/**
   * Processes a "get" request.
   *
   * @param urlItems Items of the URL
   * @param interpreterContext Instance of the context
   * @return Result of the get request, it contains a JSON-formatted string
   */
private InterpreterResult processGet(String[] urlItems, InterpreterContext interpreterContext) {
    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.get(indexTypeId[0], indexTypeId[1], indexTypeId[2]);
    if (response.isSucceeded()) {
        final JsonObject json = response.getHit().getSourceAsJsonObject();
        final String jsonStr = gson.toJson(json);
        addAngularObject(interpreterContext, "get", json);
        return new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, jsonStr);
    }
    return new InterpreterResult(InterpreterResult.Code.ERROR, "Document not found");
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) JsonObject(com.google.gson.JsonObject) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 55 with InterpreterResult

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

the class FlinkInterpreterTest method testSimpleStatement.

@Test
public void testSimpleStatement() {
    InterpreterResult result = flink.interpret("val a=1", context);
    result = flink.interpret("print(a)", context);
    assertEquals("1", result.message().get(0).getData());
}
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