You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zeppelin.apache.org by zj...@apache.org on 2018/02/21 04:06:32 UTC
zeppelin git commit: ZEPPELIN-3152. Fixed Checkstyle errors and
warnings in elasticsearch module
Repository: zeppelin
Updated Branches:
refs/heads/master 8df623b63 -> b335caed3
ZEPPELIN-3152. Fixed Checkstyle errors and warnings in elasticsearch module
### What is this PR for?
Fixed all Checkstyle errors and warnings in the elasticsearch module.
### What type of PR is it?
Improvement
### Todos
* [ ] - Task
### What is the Jira issue?
* https://issues.apache.org/jira/browse/ZEPPELIN-3152
### How should this be tested?
* CI pass
### Screenshots (if appropriate)
### Questions:
* Does the licenses files need update? no
* Is there breaking changes for older versions? no
* Does this needs documentation? no
Author: Jan Hentschel <ja...@ultratendency.com>
Closes #2786 from HorizonNet/ZEPPELIN-3152 and squashes the following commits:
17e0325 [Jan Hentschel] ZEPPELIN-3152. Fixed Checkstyle errors and warnings in elasticsearch module
Project: http://git-wip-us.apache.org/repos/asf/zeppelin/repo
Commit: http://git-wip-us.apache.org/repos/asf/zeppelin/commit/b335caed
Tree: http://git-wip-us.apache.org/repos/asf/zeppelin/tree/b335caed
Diff: http://git-wip-us.apache.org/repos/asf/zeppelin/diff/b335caed
Branch: refs/heads/master
Commit: b335caed34e2d874c2e7a2efd103042db3ea5863
Parents: 8df623b
Author: Jan Hentschel <ja...@ultratendency.com>
Authored: Sat Feb 10 13:06:58 2018 +0100
Committer: Jeff Zhang <zj...@apache.org>
Committed: Wed Feb 21 12:06:26 2018 +0800
----------------------------------------------------------------------
elasticsearch/pom.xml | 7 ++
.../elasticsearch/ElasticsearchInterpreter.java | 91 +++++++-------------
.../elasticsearch/action/AggWrapper.java | 5 +-
.../elasticsearch/client/HttpBasedClient.java | 85 ++++++++----------
.../client/TransportBasedClient.java | 51 +++++------
.../ElasticsearchInterpreterTest.java | 85 +++++++++---------
6 files changed, 142 insertions(+), 182 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/zeppelin/blob/b335caed/elasticsearch/pom.xml
----------------------------------------------------------------------
diff --git a/elasticsearch/pom.xml b/elasticsearch/pom.xml
index f80cbc5..4e4021f 100644
--- a/elasticsearch/pom.xml
+++ b/elasticsearch/pom.xml
@@ -101,6 +101,13 @@
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-checkstyle-plugin</artifactId>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+ </plugin>
</plugins>
</build>
http://git-wip-us.apache.org/repos/asf/zeppelin/blob/b335caed/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreter.java
----------------------------------------------------------------------
diff --git a/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreter.java b/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreter.java
index 6251b92..45b37c4 100644
--- a/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreter.java
+++ b/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreter.java
@@ -17,6 +17,23 @@
package org.apache.zeppelin.elasticsearch;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonObject;
+
+import org.apache.commons.lang3.StringUtils;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.common.xcontent.XContentHelper;
+import org.elasticsearch.search.aggregations.Aggregation;
+import org.elasticsearch.search.aggregations.Aggregations;
+import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation;
+import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation;
+import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
+import org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -32,7 +49,8 @@ import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.apache.commons.lang3.StringUtils;
+import com.github.wnameless.json.flattener.JsonFlattener;
+
import org.apache.zeppelin.completer.CompletionType;
import org.apache.zeppelin.elasticsearch.action.ActionResponse;
import org.apache.zeppelin.elasticsearch.action.AggWrapper;
@@ -44,29 +62,11 @@ import org.apache.zeppelin.interpreter.Interpreter;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
-import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.common.xcontent.XContentFactory;
-import org.elasticsearch.common.xcontent.XContentHelper;
-import org.elasticsearch.search.aggregations.Aggregation;
-import org.elasticsearch.search.aggregations.Aggregations;
-import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation;
-import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation;
-import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
-import org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.github.wnameless.json.flattener.JsonFlattener;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonObject;
-
/**
* Elasticsearch Interpreter for Zeppelin.
*/
public class ElasticsearchInterpreter extends Interpreter {
-
private static Logger logger = LoggerFactory.getLogger(ElasticsearchInterpreter.class);
private static final String HELP = "Elasticsearch interpreter:\n"
@@ -92,7 +92,6 @@ public class ElasticsearchInterpreter extends Interpreter {
private static final Pattern FIELD_NAME_PATTERN = Pattern.compile("\\[\\\\\"(.+)\\\\\"\\](.*)");
-
public static final String ELASTICSEARCH_HOST = "elasticsearch.host";
public static final String ELASTICSEARCH_PORT = "elasticsearch.port";
public static final String ELASTICSEARCH_CLIENT_TYPE = "elasticsearch.client.type";
@@ -107,7 +106,6 @@ public class ElasticsearchInterpreter extends Interpreter {
public ElasticsearchInterpreter(Properties property) {
super(property);
-
}
@Override
@@ -119,8 +117,7 @@ public class ElasticsearchInterpreter extends Interpreter {
try {
this.resultSize = Integer.parseInt(getProperty(ELASTICSEARCH_RESULT_SIZE));
- }
- catch (final NumberFormatException e) {
+ } catch (final NumberFormatException e) {
this.resultSize = 10;
logger.error("Unable to parse " + ELASTICSEARCH_RESULT_SIZE + " : " +
getProperty(ELASTICSEARCH_RESULT_SIZE), e);
@@ -129,15 +126,12 @@ public class ElasticsearchInterpreter extends Interpreter {
try {
if (StringUtils.isEmpty(clientType) || "transport".equals(clientType)) {
elsClient = new TransportBasedClient(getProperties());
- }
- else if ("http".equals(clientType)) {
+ } else if ("http".equals(clientType)) {
elsClient = new HttpBasedClient(getProperties());
- }
- else {
+ } else {
logger.error("Unknown type of Elasticsearch client: " + clientType);
}
- }
- catch (final IOException e) {
+ } catch (final IOException e) {
logger.error("Open connection with Elasticsearch", e);
}
}
@@ -203,23 +197,18 @@ public class ElasticsearchInterpreter extends Interpreter {
try {
if ("get".equalsIgnoreCase(method)) {
return processGet(urlItems, interpreterContext);
- }
- else if ("count".equalsIgnoreCase(method)) {
+ } else if ("count".equalsIgnoreCase(method)) {
return processCount(urlItems, data, interpreterContext);
- }
- else if ("search".equalsIgnoreCase(method)) {
+ } else if ("search".equalsIgnoreCase(method)) {
return processSearch(urlItems, data, currentResultSize, interpreterContext);
- }
- else if ("index".equalsIgnoreCase(method)) {
+ } else if ("index".equalsIgnoreCase(method)) {
return processIndex(urlItems, data);
- }
- else if ("delete".equalsIgnoreCase(method)) {
+ } else if ("delete".equalsIgnoreCase(method)) {
return processDelete(urlItems);
}
return processHelp(InterpreterResult.Code.ERROR, "Unknown command");
- }
- catch (final Exception e) {
+ } catch (final Exception e) {
return new InterpreterResult(InterpreterResult.Code.ERROR, "Error : " + e.getMessage());
}
}
@@ -259,7 +248,6 @@ public class ElasticsearchInterpreter extends Interpreter {
}
private String[] getIndexTypeId(String[] urlItems) {
-
if (urlItems.length < 3) {
return null;
}
@@ -279,7 +267,6 @@ public class ElasticsearchInterpreter extends Interpreter {
private InterpreterResult processHelp(InterpreterResult.Code code, String additionalMessage) {
final StringBuffer buffer = new StringBuffer();
-
if (additionalMessage != null) {
buffer.append(additionalMessage).append("\n");
}
@@ -297,7 +284,6 @@ public class ElasticsearchInterpreter extends Interpreter {
* @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) {
@@ -332,7 +318,6 @@ public class ElasticsearchInterpreter extends Interpreter {
*/
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,...)");
@@ -359,7 +344,6 @@ public class ElasticsearchInterpreter extends Interpreter {
*/
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,...)");
@@ -382,7 +366,6 @@ public class ElasticsearchInterpreter extends Interpreter {
* @return Result of the index request, it contains the id of the document
*/
private InterpreterResult processIndex(String[] urlItems, String data) {
-
if (urlItems.length < 2 || urlItems.length > 3) {
return new InterpreterResult(InterpreterResult.Code.ERROR,
"Bad URL (it should be /index/type or /index/type/id)");
@@ -404,7 +387,6 @@ public class ElasticsearchInterpreter extends Interpreter {
* @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) {
@@ -426,7 +408,6 @@ public class ElasticsearchInterpreter extends Interpreter {
}
private ActionResponse searchData(String[] urlItems, String query, int size) {
-
String[] indices = null;
String[] types = null;
@@ -441,7 +422,6 @@ public class ElasticsearchInterpreter extends Interpreter {
}
private InterpreterResult buildAggResponseMessage(Aggregations aggregations) {
-
// Only the result of the first aggregation is returned
//
final Aggregation agg = aggregations.asList().get(0);
@@ -450,11 +430,9 @@ public class ElasticsearchInterpreter extends Interpreter {
if (agg instanceof InternalMetricsAggregation) {
resMsg = XContentHelper.toString((InternalMetricsAggregation) agg).toString();
- }
- else if (agg instanceof InternalSingleBucketAggregation) {
+ } else if (agg instanceof InternalSingleBucketAggregation) {
resMsg = XContentHelper.toString((InternalSingleBucketAggregation) agg).toString();
- }
- else if (agg instanceof InternalMultiBucketAggregation) {
+ } else if (agg instanceof InternalMultiBucketAggregation) {
final Set<String> headerKeys = new HashSet<>();
final List<Map<String, Object>> buckets = new LinkedList<>();
final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;
@@ -466,8 +444,7 @@ public class ElasticsearchInterpreter extends Interpreter {
final Map<String, Object> bucketMap = JsonFlattener.flattenAsMap(builder.string());
headerKeys.addAll(bucketMap.keySet());
buckets.add(bucketMap);
- }
- catch (final IOException e) {
+ } catch (final IOException e) {
logger.error("Processing bucket: " + e.getMessage(), e);
}
}
@@ -496,7 +473,6 @@ public class ElasticsearchInterpreter extends Interpreter {
}
private InterpreterResult buildAggResponseMessage(List<AggWrapper> aggregations) {
-
final InterpreterResult.Type resType = InterpreterResult.Type.TABLE;
String resMsg = "";
@@ -531,7 +507,6 @@ public class ElasticsearchInterpreter extends Interpreter {
}
private String buildSearchHitsResponseMessage(ActionResponse response) {
-
if (response.getHits() == null || response.getHits().size() == 0) {
return "";
}
@@ -553,8 +528,7 @@ public class ElasticsearchInterpreter extends Interpreter {
if (fieldNameMatcher.matches()) {
flattenMap.put(fieldNameMatcher.group(1) + fieldNameMatcher.group(2),
flattenJsonMap.get(fieldName));
- }
- else {
+ } else {
flattenMap.put(fieldName, flattenJsonMap.get(fieldName));
}
}
@@ -590,7 +564,6 @@ public class ElasticsearchInterpreter extends Interpreter {
}
private InterpreterResult buildResponseMessage(ActionResponse response) {
-
final List<AggWrapper> aggregations = response.getAggregations();
if (aggregations != null && aggregations.size() > 0) {
http://git-wip-us.apache.org/repos/asf/zeppelin/blob/b335caed/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/action/AggWrapper.java
----------------------------------------------------------------------
diff --git a/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/action/AggWrapper.java b/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/action/AggWrapper.java
index 14446db..a3ed951 100644
--- a/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/action/AggWrapper.java
+++ b/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/action/AggWrapper.java
@@ -21,8 +21,9 @@ package org.apache.zeppelin.elasticsearch.action;
* Contains the result of an aggregation.
*/
public class AggWrapper {
-
- /** Type of an aggregation (to know if there are buckets or not) */
+ /**
+ * Type of an aggregation (to know if there are buckets or not).
+ */
public enum AggregationType { SIMPLE, MULTI_BUCKETS };
private final AggregationType type;
http://git-wip-us.apache.org/repos/asf/zeppelin/blob/b335caed/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/HttpBasedClient.java
----------------------------------------------------------------------
diff --git a/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/HttpBasedClient.java b/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/HttpBasedClient.java
index d691597..f2a9f02 100644
--- a/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/HttpBasedClient.java
+++ b/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/HttpBasedClient.java
@@ -17,26 +17,21 @@
package org.apache.zeppelin.elasticsearch.client;
+import com.google.common.base.Joiner;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParseException;
+
+import org.apache.commons.lang3.StringUtils;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.zeppelin.elasticsearch.ElasticsearchInterpreter;
-import org.apache.zeppelin.elasticsearch.action.ActionException;
-import org.apache.zeppelin.elasticsearch.action.ActionResponse;
-import org.apache.zeppelin.elasticsearch.action.AggWrapper;
-import org.apache.zeppelin.elasticsearch.action.AggWrapper.AggregationType;
-import org.apache.zeppelin.elasticsearch.action.HitWrapper;
-import org.json.JSONArray;
-import org.json.JSONObject;
-
-import com.google.common.base.Joiner;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonParseException;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
@@ -44,11 +39,17 @@ import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest;
import com.mashape.unirest.request.HttpRequestWithBody;
+import org.apache.zeppelin.elasticsearch.ElasticsearchInterpreter;
+import org.apache.zeppelin.elasticsearch.action.ActionException;
+import org.apache.zeppelin.elasticsearch.action.ActionResponse;
+import org.apache.zeppelin.elasticsearch.action.AggWrapper;
+import org.apache.zeppelin.elasticsearch.action.AggWrapper.AggregationType;
+import org.apache.zeppelin.elasticsearch.action.HitWrapper;
+
/**
* Elasticsearch client using the HTTP API.
*/
public class HttpBasedClient implements ElasticsearchClient {
-
private static final String QUERY_STRING_TEMPLATE =
"{ \"query\": { \"query_string\": { \"query\": \"_Q_\", \"analyze_wildcard\": \"true\" } } }";
@@ -114,24 +115,20 @@ public class HttpBasedClient implements ElasticsearchClient {
if (id.equals(encodedId)) {
// No difference, use directly the id
buffer.append("/").append(id);
- }
- else {
+ } else {
// There are differences: to avoid problems with some special characters
// such as / and # in id, use a "terms" query
- buffer.append("/_search?source=")
- .append(URLEncoder
+ buffer.append("/_search?source=").append(URLEncoder
.encode("{\"query\":{\"terms\":{\"_id\":[\"" + id + "\"]}}}", "UTF-8"));
}
- }
- else {
+ } else {
buffer.append("/").append(id);
}
}
}
}
return buffer.toString();
- }
- catch (final UnsupportedEncodingException e) {
+ } catch (final UnsupportedEncodingException e) {
throw new ActionException(e);
}
}
@@ -164,8 +161,7 @@ public class HttpBasedClient implements ElasticsearchClient {
getFieldAsString(body, "_type"),
getFieldAsString(body, "_id"),
getFieldAsString(body, "_source")));
- }
- else {
+ } else {
final JSONArray hits = getFieldAsArray(body.getObject(), "hits/hits");
final JSONObject hit = (JSONObject) hits.iterator().next();
response = new ActionResponse()
@@ -176,18 +172,15 @@ public class HttpBasedClient implements ElasticsearchClient {
hit.getString("_id"),
hit.opt("_source").toString()));
}
- }
- else {
+ } else {
if (result.getStatus() == 404) {
response = new ActionResponse()
.succeeded(false);
- }
- else {
+ } else {
throw new ActionException(result.getBody());
}
}
- }
- catch (final UnirestException e) {
+ } catch (final UnirestException e) {
throw new ActionException(e);
}
return response;
@@ -214,12 +207,10 @@ public class HttpBasedClient implements ElasticsearchClient {
getFieldAsString(body, "_type"),
getFieldAsString(body, "_id"),
null));
- }
- else {
+ } else {
throw new ActionException(result.getBody());
}
- }
- catch (final UnirestException e) {
+ } catch (final UnirestException e) {
throw new ActionException(e);
}
return response;
@@ -232,8 +223,7 @@ public class HttpBasedClient implements ElasticsearchClient {
HttpRequestWithBody request = null;
if (StringUtils.isEmpty(id)) {
request = Unirest.post(getUrl(index, type, id, false));
- }
- else {
+ } else {
request = Unirest.put(getUrl(index, type, id, false));
}
request
@@ -255,12 +245,10 @@ public class HttpBasedClient implements ElasticsearchClient {
getFieldAsString(result, "_type"),
getFieldAsString(result, "_id"),
null));
- }
- else {
+ } else {
throw new ActionException(result.getBody().toString());
}
- }
- catch (final UnirestException e) {
+ } catch (final UnirestException e) {
throw new ActionException(e);
}
return response;
@@ -275,8 +263,7 @@ public class HttpBasedClient implements ElasticsearchClient {
// So, try to parse as a JSON => if there is an error, consider the query a Lucene one
try {
gson.fromJson(query, Map.class);
- }
- catch (final JsonParseException e) {
+ } catch (final JsonParseException e) {
// This is not a JSON (or maybe not well formatted...)
query = QUERY_STRING_TEMPLATE.replace("_Q_", query);
}
@@ -320,15 +307,13 @@ public class HttpBasedClient implements ElasticsearchClient {
response.addAggregation(
new AggWrapper(AggregationType.MULTI_BUCKETS, buckets.next().toString()));
}
- }
- else {
+ } else {
response.addAggregation(
new AggWrapper(AggregationType.SIMPLE, aggregationsMap.toString()));
}
break; // Keep only one aggregation
}
- }
- else if (size > 0 && total > 0) {
+ } else if (size > 0 && total > 0) {
final JSONArray hits = getFieldAsArray(body, "hits/hits");
final Iterator<Object> iter = hits.iterator();
@@ -343,12 +328,10 @@ public class HttpBasedClient implements ElasticsearchClient {
data.toString()));
}
}
- }
- else {
+ } else {
throw new ActionException(body.get("error").toString());
}
- }
- catch (final UnirestException e) {
+ } catch (final UnirestException e) {
throw new ActionException(e);
}
http://git-wip-us.apache.org/repos/asf/zeppelin/blob/b335caed/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/TransportBasedClient.java
----------------------------------------------------------------------
diff --git a/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/TransportBasedClient.java b/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/TransportBasedClient.java
index 1451019..2af37bd 100644
--- a/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/TransportBasedClient.java
+++ b/elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/TransportBasedClient.java
@@ -17,22 +17,11 @@
package org.apache.zeppelin.elasticsearch.client;
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonSyntaxException;
import org.apache.commons.lang3.StringUtils;
-import org.apache.zeppelin.elasticsearch.ElasticsearchInterpreter;
-import org.apache.zeppelin.elasticsearch.action.ActionResponse;
-import org.apache.zeppelin.elasticsearch.action.AggWrapper;
-import org.apache.zeppelin.elasticsearch.action.HitWrapper;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
@@ -56,15 +45,26 @@ import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregat
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonSyntaxException;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.zeppelin.elasticsearch.ElasticsearchInterpreter;
+import org.apache.zeppelin.elasticsearch.action.ActionResponse;
+import org.apache.zeppelin.elasticsearch.action.AggWrapper;
+import org.apache.zeppelin.elasticsearch.action.HitWrapper;
/**
* Elasticsearch client using the transport protocol.
*/
public class TransportBasedClient implements ElasticsearchClient {
-
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final Client client;
@@ -151,8 +151,7 @@ public class TransportBasedClient implements ElasticsearchClient {
@SuppressWarnings("rawtypes")
final Map source = gson.fromJson(query, Map.class);
reqBuilder.setExtraSource(source);
- }
- catch (final JsonSyntaxException e) {
+ } catch (final JsonSyntaxException e) {
// This is not a JSON (or maybe not well formatted...)
reqBuilder.setQuery(QueryBuilders.queryStringQuery(query).analyzeWildcard(true));
}
@@ -168,8 +167,7 @@ public class TransportBasedClient implements ElasticsearchClient {
if (searchResp.getAggregations() != null) {
setAggregations(searchResp.getAggregations(), actionResp);
- }
- else {
+ } else {
for (final SearchHit hit: searchResp.getHits()) {
// Fields can be found either in _source, or in fields (it depends on the query)
// => specific for elasticsearch's version < 5
@@ -197,12 +195,10 @@ public class TransportBasedClient implements ElasticsearchClient {
if (agg instanceof InternalMetricsAggregation) {
actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE,
XContentHelper.toString((InternalMetricsAggregation) agg).toString()));
- }
- else if (agg instanceof InternalSingleBucketAggregation) {
+ } else if (agg instanceof InternalSingleBucketAggregation) {
actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE,
XContentHelper.toString((InternalSingleBucketAggregation) agg).toString()));
- }
- else if (agg instanceof InternalMultiBucketAggregation) {
+ } else if (agg instanceof InternalMultiBucketAggregation) {
final Set<String> headerKeys = new HashSet<>();
final List<Map<String, Object>> buckets = new LinkedList<>();
final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;
@@ -213,8 +209,7 @@ public class TransportBasedClient implements ElasticsearchClient {
bucket.toXContent(builder, null);
actionResp.addAggregation(
new AggWrapper(AggWrapper.AggregationType.MULTI_BUCKETS, builder.string()));
- }
- catch (final IOException e) {
+ } catch (final IOException e) {
// Ignored
}
}
http://git-wip-us.apache.org/repos/asf/zeppelin/blob/b335caed/elasticsearch/src/test/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreterTest.java
----------------------------------------------------------------------
diff --git a/elasticsearch/src/test/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreterTest.java b/elasticsearch/src/test/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreterTest.java
index 64562b1..e186b44 100644
--- a/elasticsearch/src/test/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreterTest.java
+++ b/elasticsearch/src/test/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreterTest.java
@@ -21,6 +21,20 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import org.apache.commons.lang.math.RandomUtils;
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
+import org.elasticsearch.client.Client;
+import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.node.Node;
+import org.elasticsearch.node.NodeBuilder;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -30,29 +44,15 @@ import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
-import org.apache.commons.lang.math.RandomUtils;
import org.apache.zeppelin.completer.CompletionType;
import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
-import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
-import org.elasticsearch.client.Client;
-import org.elasticsearch.common.settings.Settings;
-import org.elasticsearch.node.Node;
-import org.elasticsearch.node.NodeBuilder;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.experimental.theories.DataPoint;
-import org.junit.experimental.theories.Theories;
-import org.junit.experimental.theories.Theory;
-import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class ElasticsearchInterpreterTest {
-
@DataPoint public static ElasticsearchInterpreter transportInterpreter;
@DataPoint public static ElasticsearchInterpreter httpInterpreter;
@@ -70,17 +70,15 @@ public class ElasticsearchInterpreterTest {
private static final AtomicInteger deleteId = new AtomicInteger(2);
-
@BeforeClass
public static void populate() throws IOException {
-
final Settings settings = Settings.settingsBuilder()
- .put("cluster.name", ELS_CLUSTER_NAME)
- .put("network.host", ELS_HOST)
- .put("http.port", ELS_HTTP_PORT)
- .put("transport.tcp.port", ELS_TRANSPORT_PORT)
- .put("path.home", ELS_PATH)
- .build();
+ .put("cluster.name", ELS_CLUSTER_NAME)
+ .put("network.host", ELS_HOST)
+ .put("http.port", ELS_HTTP_PORT)
+ .put("transport.tcp.port", ELS_TRANSPORT_PORT)
+ .put("path.home", ELS_PATH)
+ .build();
elsNode = NodeBuilder.nodeBuilder().settings(settings).node();
elsClient = elsNode.client();
@@ -170,7 +168,6 @@ public class ElasticsearchInterpreterTest {
@Theory
public void testCount(ElasticsearchInterpreter interpreter) {
-
final InterpreterContext ctx = buildContext("testCount");
InterpreterResult res = interpreter.interpret("count /unknown", ctx);
@@ -180,15 +177,15 @@ public class ElasticsearchInterpreterTest {
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());
+ assertEquals(50L, ctx.getAngularObjectRegistry().get("count_testCount", null, null).get());
- res = interpreter.interpret("count /logs { \"query\": { \"match\": { \"status\": 500 } } }", ctx);
+ res = interpreter.interpret("count /logs { \"query\": { \"match\": { \"status\": 500 } } }",
+ ctx);
assertEquals(Code.SUCCESS, res.code());
}
@Theory
public void testGet(ElasticsearchInterpreter interpreter) {
-
final InterpreterContext ctx = buildContext("get");
InterpreterResult res = interpreter.interpret("get /logs/http/unknown", ctx);
@@ -212,7 +209,6 @@ public class ElasticsearchInterpreterTest {
@Theory
public void testSearch(ElasticsearchInterpreter interpreter) {
-
final InterpreterContext ctx = buildContext("search");
InterpreterResult res = interpreter.interpret("size 10\nsearch /logs *", ctx);
@@ -221,23 +217,25 @@ public class ElasticsearchInterpreterTest {
res = interpreter.interpret("search /logs {{{hello}}}", ctx);
assertEquals(Code.ERROR, res.code());
- res = interpreter.interpret("search /logs { \"query\": { \"match\": { \"status\": 500 } } }", ctx);
+ res = interpreter.interpret("search /logs { \"query\": { \"match\": { \"status\": 500 } } }",
+ ctx);
assertEquals(Code.SUCCESS, res.code());
res = interpreter.interpret("search /logs status:404", ctx);
assertEquals(Code.SUCCESS, res.code());
- res = interpreter.interpret("search /logs { \"fields\": [ \"date\", \"request.headers\" ], \"query\": { \"match\": { \"status\": 500 } } }", ctx);
+ res = interpreter.interpret("search /logs { \"fields\": [ \"date\", \"request.headers\" ], " +
+ "\"query\": { \"match\": { \"status\": 500 } } }", ctx);
assertEquals(Code.SUCCESS, res.code());
}
@Theory
public void testAgg(ElasticsearchInterpreter interpreter) {
-
final InterpreterContext ctx = buildContext("agg");
// Single-value metric
- InterpreterResult res = interpreter.interpret("search /logs { \"aggs\" : { \"distinct_status_count\" : " +
+ InterpreterResult res = interpreter.interpret("search /logs { \"aggs\" : " +
+ "{ \"distinct_status_count\" : " +
" { \"cardinality\" : { \"field\" : \"status\" } } } }", ctx);
assertEquals(Code.SUCCESS, res.code());
@@ -249,7 +247,8 @@ public class ElasticsearchInterpreterTest {
// Single bucket
res = interpreter.interpret("search /logs { \"aggs\" : { " +
" \"200_OK\" : { \"filter\" : { \"term\": { \"status\": \"200\" } }, " +
- " \"aggs\" : { \"avg_length\" : { \"avg\" : { \"field\" : \"content_length\" } } } } } }", ctx);
+ " \"aggs\" : { \"avg_length\" : { \"avg\" : " +
+ "{ \"field\" : \"content_length\" } } } } } }", ctx);
assertEquals(Code.SUCCESS, res.code());
// Multi-buckets
@@ -259,29 +258,31 @@ public class ElasticsearchInterpreterTest {
res = interpreter.interpret("search /logs { \"aggs\" : { " +
" \"length\" : { \"terms\": { \"field\": \"status\" }, " +
- " \"aggs\" : { \"sum_length\" : { \"sum\" : { \"field\" : \"content_length\" } }, \"sum_status\" : { \"sum\" : { \"field\" : \"status\" } } } } } }", ctx);
+ " \"aggs\" : { \"sum_length\" : { \"sum\" : { \"field\" : \"content_length\" } }, " +
+ "\"sum_status\" : { \"sum\" : { \"field\" : \"status\" } } } } } }", ctx);
assertEquals(Code.SUCCESS, res.code());
}
@Theory
public void testIndex(ElasticsearchInterpreter interpreter) {
-
- InterpreterResult res = interpreter.interpret("index /logs { \"date\": \"" + new Date() + "\", \"method\": \"PUT\", \"status\": \"500\" }", null);
+ 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);
+ 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);
+ res = interpreter.interpret("index /logs/http/1000 { \"date\": " +
+ "\"2015-12-06T14:54:23.368Z\", \"method\": \"PUT\", \"status\": \"500\" }", null);
assertEquals(Code.SUCCESS, res.code());
}
@Theory
public void testDelete(ElasticsearchInterpreter interpreter) {
-
InterpreterResult res = interpreter.interpret("delete /logs/http/unknown", null);
assertEquals(Code.ERROR, res.code());
@@ -296,7 +297,6 @@ public class ElasticsearchInterpreterTest {
@Theory
public void testMisc(ElasticsearchInterpreter interpreter) {
-
InterpreterResult res = interpreter.interpret(null, null);
assertEquals(Code.SUCCESS, res.code());
@@ -306,8 +306,10 @@ public class ElasticsearchInterpreterTest {
@Theory
public void testCompletion(ElasticsearchInterpreter interpreter) {
- final List<InterpreterCompletion> expectedResultOne = Arrays.asList(new InterpreterCompletion("count", "count", CompletionType.command.name()));
- final List<InterpreterCompletion> expectedResultTwo = Arrays.asList(new InterpreterCompletion("help", "help", CompletionType.command.name()));
+ final List<InterpreterCompletion> expectedResultOne = Arrays.asList(
+ new InterpreterCompletion("count", "count", CompletionType.command.name()));
+ final List<InterpreterCompletion> expectedResultTwo = Arrays.asList(
+ new InterpreterCompletion("help", "help", CompletionType.command.name()));
final List<InterpreterCompletion> resultOne = interpreter.completion("co", 0, null);
final List<InterpreterCompletion> resultTwo = interpreter.completion("he", 0, null);
@@ -322,5 +324,4 @@ public class ElasticsearchInterpreterTest {
}
Assert.assertEquals(ElasticsearchInterpreter.COMMANDS, allCompletionList);
}
-
}