Skip to content

Commit

Permalink
Change the default response format to JDBC. (#334)
Browse files Browse the repository at this point in the history
* change the default response fromat to jdbc
* update doc
  • Loading branch information
penghuo authored Jan 6, 2020
1 parent 580a5d3 commit f67d701
Show file tree
Hide file tree
Showing 15 changed files with 498 additions and 121 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ plugins {
id 'nebula.ospackage' version "5.3.0"
id 'java-library'
id 'checkstyle'
id "io.freefair.lombok" version "4.1.2"
}

/*
Expand Down Expand Up @@ -99,6 +100,7 @@ esplugin {
// TODO: fix compiler warnings
compileJava.options.warnings = false
compileJava {
options.compilerArgs.addAll(["-processor", 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor'])
doFirst {
// TODO: do not fail build on warnings, need to fix all compiler warnings
options.compilerArgs.remove('-Werror')
Expand Down
115 changes: 110 additions & 5 deletions docs/user/admin/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
"transient" : {
"opendistro.sql.enabled" : false
"opendistro.sql.enabled" : "false"
}
}'

Expand Down Expand Up @@ -101,7 +101,7 @@ SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
"transient" : {
"opendistro.sql.query.slowlog" : 10
"opendistro.sql.query.slowlog" : "10"
}
}'

Expand Down Expand Up @@ -143,7 +143,7 @@ SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
"transient" : {
"opendistro.sql.query.analysis.enabled" : false
"opendistro.sql.query.analysis.enabled" : "false"
}
}'

Expand Down Expand Up @@ -187,7 +187,7 @@ SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
"transient" : {
"opendistro.sql.query.analysis.semantic.suggestion" : true
"opendistro.sql.query.analysis.semantic.suggestion" : "true"
}
}'

Expand Down Expand Up @@ -255,7 +255,7 @@ SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
"transient" : {
"opendistro.sql.query.analysis.semantic.threshold" : 50
"opendistro.sql.query.analysis.semantic.threshold" : "50"
}
}'

Expand All @@ -279,3 +279,108 @@ Result set::
}
}

opendistro.sql.query.response.format
====================================

Description
-----------

User can set default response format of the query. The supported format includes: jdbc,json,csv,raw,table.

1. The default value is jdbc.
2. This setting is node scope.
3. This setting can be updated dynamically.


Example 1
---------

You can update the setting with a new value like this.

SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
"transient" : {
"opendistro.sql.query.response.format" : "json"
}
}'

Result set::

{
"acknowledged" : true,
"persistent" : { },
"transient" : {
"opendistro" : {
"sql" : {
"query" : {
"response" : {
"format" : "json"
}
}
}
}
}
}

Example 2
---------

Query result after the setting updated is like:

SQL query::

>> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{
"query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age LIMIT 2"
}'

Result set::

{
"_shards" : {
"total" : 5,
"failed" : 0,
"successful" : 5,
"skipped" : 0
},
"hits" : {
"hits" : [
{
"_index" : "accounts",
"_type" : "account",
"_source" : {
"firstname" : "Nanette",
"age" : 28,
"lastname" : "Bates"
},
"_id" : "13",
"sort" : [
28
],
"_score" : null
},
{
"_index" : "accounts",
"_type" : "account",
"_source" : {
"firstname" : "Amber",
"age" : 32,
"lastname" : "Duke"
},
"_id" : "1",
"sort" : [
32
],
"_score" : null
}
],
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null
},
"took" : 100,
"timed_out" : false
}

138 changes: 69 additions & 69 deletions docs/user/interfaces/protocol.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,80 +133,13 @@ Explain::
}
}

Elasticsearch DSL
=================

Description
-----------

By default the plugin returns original response from Elasticsearch in JSON. Because this is the native response from Elasticsearch, extra efforts are needed to parse and interpret it.

Example
-------

SQL query::

>> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{
"query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age LIMIT 2"
}'

Result set::

{
"_shards" : {
"total" : 5,
"failed" : 0,
"successful" : 5,
"skipped" : 0
},
"hits" : {
"hits" : [
{
"_index" : "accounts",
"_type" : "account",
"_source" : {
"firstname" : "Nanette",
"age" : 28,
"lastname" : "Bates"
},
"_id" : "13",
"sort" : [
28
],
"_score" : null
},
{
"_index" : "accounts",
"_type" : "account",
"_source" : {
"firstname" : "Amber",
"age" : 32,
"lastname" : "Duke"
},
"_id" : "1",
"sort" : [
32
],
"_score" : null
}
],
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null
},
"took" : 100,
"timed_out" : false
}

JDBC Format
===========

Description
-----------

JDBC format is provided for JDBC driver and client side that needs both schema and result set well formatted.
By default the plugin return JDBC format. JDBC format is provided for JDBC driver and client side that needs both schema and result set well formatted.

Example 1
---------
Expand All @@ -215,7 +148,7 @@ Here is an example for normal response. The `schema` includes field name and its

SQL query::

>> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql?format=jdbc -d '{
>> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{
"query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age LIMIT 2"
}'

Expand Down Expand Up @@ -275,6 +208,73 @@ Result set::
"status" : 400
}

Elasticsearch DSL
=================

Description
-----------

The plugin returns original response from Elasticsearch in JSON. Because this is the native response from Elasticsearch, extra efforts are needed to parse and interpret it.

Example
-------

SQL query::

>> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql?format=json -d '{
"query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age LIMIT 2"
}'

Result set::

{
"_shards" : {
"total" : 5,
"failed" : 0,
"successful" : 5,
"skipped" : 0
},
"hits" : {
"hits" : [
{
"_index" : "accounts",
"_type" : "account",
"_source" : {
"firstname" : "Nanette",
"age" : 28,
"lastname" : "Bates"
},
"_id" : "13",
"sort" : [
28
],
"_score" : null
},
{
"_index" : "accounts",
"_type" : "account",
"_source" : {
"firstname" : "Amber",
"age" : 32,
"lastname" : "Duke"
},
"_id" : "1",
"sort" : [
32
],
"_score" : null
}
],
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null
},
"took" : 100,
"timed_out" : false
}

CSV Format
==========

Expand Down
3 changes: 3 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file is generated by the 'io.freefair.lombok' Gradle plugin
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
import com.amazon.opendistroforelasticsearch.sql.query.join.ESJoinQueryAction;
import com.amazon.opendistroforelasticsearch.sql.query.multi.MultiQueryAction;

import java.util.stream.Stream;

/**
* Created by Eliran on 26/12/2015.
*/
public class ActionRequestRestExecutorFactory {

/**
* Create executor based on the format and wrap with AsyncRestExecutor
* to async blocking execute() call if necessary.
Expand All @@ -36,23 +33,21 @@ public class ActionRequestRestExecutorFactory {
* @param queryAction query action
* @return executor
*/
public static RestExecutor createExecutor(String format, QueryAction queryAction) {
if (format == null || format.equals("")) {
return new AsyncRestExecutor(
new ElasticDefaultRestExecutor(queryAction),
action -> isJoin(action) || isUnionMinus(action)
);
}

if (format.equalsIgnoreCase("csv")) {
return new AsyncRestExecutor(new CSVResultRestExecutor());
public static RestExecutor createExecutor(Format format, QueryAction queryAction) {
switch (format) {
case CSV:
return new AsyncRestExecutor(new CSVResultRestExecutor());
case JSON:
return new AsyncRestExecutor(
new ElasticDefaultRestExecutor(queryAction),
action -> isJoin(action) || isUnionMinus(action)
);
case JDBC:
case RAW:
case TABLE:
default:
return new AsyncRestExecutor(new PrettyFormatRestExecutor(format.getFormatName()));
}

if (Stream.of("jdbc", "table", "raw").anyMatch(format::equalsIgnoreCase)) {
return new AsyncRestExecutor(new PrettyFormatRestExecutor(format));
}

throw new IllegalArgumentException("Failed to create executor due to unknown response format: " + format);
}

private static boolean isJoin(QueryAction queryAction) {
Expand Down
Loading

0 comments on commit f67d701

Please sign in to comment.