From f896ea8cb0651c64e09401e0b0187a98f6ca628e Mon Sep 17 00:00:00 2001 From: Tinkoff DWH Date: Fri, 10 Mar 2017 21:01:06 +0500 Subject: [PATCH 1/3] [ZEPPELIN-2245] separate precode into JDBCInterpreter --- docs/interpreter/jdbc.md | 9 +++--- .../apache/zeppelin/jdbc/JDBCInterpreter.java | 10 ++++--- .../main/resources/interpreter-setting.json | 12 ++++---- .../zeppelin/jdbc/JDBCInterpreterTest.java | 30 +++++++++++++++---- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/docs/interpreter/jdbc.md b/docs/interpreter/jdbc.md index 75da51fd1a9..aa95037cc9e 100644 --- a/docs/interpreter/jdbc.md +++ b/docs/interpreter/jdbc.md @@ -118,6 +118,11 @@ The JDBC interpreter properties are defined by default like below. gpadmin The JDBC user name + + default.precode + + Some SQL which executes while opening connection + If you want to connect other databases such as `Mysql`, `Redshift` and `Hive`, you need to edit the property values. @@ -167,10 +172,6 @@ There are more JDBC interpreter properties you can specify like below. default.jceks.credentialKey jceks credential key - - zeppelin.jdbc.precode - Some SQL which executes while opening connection - You can also add more properties by using this [method](http://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html#getConnection%28java.lang.String,%20java.util.Properties%29). diff --git a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java index 0b25a23fcd3..f83caf9fec6 100644 --- a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java +++ b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java @@ -101,9 +101,10 @@ public class JDBCInterpreter extends Interpreter { static final String URL_KEY = "url"; static final String USER_KEY = "user"; static final String PASSWORD_KEY = "password"; + static final String PRECODE_KEY = "precode"; static final String JDBC_JCEKS_FILE = "jceks.file"; static final String JDBC_JCEKS_CREDENTIAL_KEY = "jceks.credentialKey"; - static final String ZEPPELIN_JDBC_PRECODE_KEY = "zeppelin.jdbc.precode"; + static final String PRECODE_KEY_TEMPLATE = "%s.precode"; static final String DOT = "."; private static final char WHITESPACE = ' '; @@ -118,6 +119,7 @@ public class JDBCInterpreter extends Interpreter { static final String DEFAULT_URL = DEFAULT_KEY + DOT + URL_KEY; static final String DEFAULT_USER = DEFAULT_KEY + DOT + USER_KEY; static final String DEFAULT_PASSWORD = DEFAULT_KEY + DOT + PASSWORD_KEY; + static final String DEFAULT_PRECODE = DEFAULT_KEY + DOT + PRECODE_KEY; static final String EMPTY_COLUMN_VALUE = ""; @@ -342,7 +344,7 @@ private Connection getConnectionFromPool(String url, String user, String propert if (!getJDBCConfiguration(user).isConnectionInDBDriverPool(propertyKey)) { createConnectionPool(url, user, propertyKey, properties); try (Connection connection = DriverManager.getConnection(jdbcDriver)) { - executePrecode(connection); + executePrecode(connection, propertyKey); } } return DriverManager.getConnection(jdbcDriver); @@ -548,8 +550,8 @@ protected ArrayList splitSqlQueries(String sql) { return queries; } - private void executePrecode(Connection connection) throws SQLException { - String precode = getProperty(ZEPPELIN_JDBC_PRECODE_KEY); + private void executePrecode(Connection connection, String propertyKey) throws SQLException { + String precode = getProperty(String.format(PRECODE_KEY_TEMPLATE, propertyKey)); if (StringUtils.isNotBlank(precode)) { precode = StringUtils.trim(precode); logger.info("Run SQL precode '{}'", precode); diff --git a/jdbc/src/main/resources/interpreter-setting.json b/jdbc/src/main/resources/interpreter-setting.json index 6134243502e..322ea5ac791 100644 --- a/jdbc/src/main/resources/interpreter-setting.json +++ b/jdbc/src/main/resources/interpreter-setting.json @@ -28,6 +28,12 @@ "defaultValue": "org.postgresql.Driver", "description": "JDBC Driver Name" }, + "default.precode": { + "envName": null, + "propertyName": "zeppelin.jdbc.precode", + "defaultValue": "", + "description": "SQL which executes while opening connection" + }, "common.max_count": { "envName": null, "propertyName": "common.max_count", @@ -63,12 +69,6 @@ "propertyName": "zeppelin.jdbc.principal", "defaultValue": "", "description": "Kerberos principal" - }, - "zeppelin.jdbc.precode": { - "envName": null, - "propertyName": "zeppelin.jdbc.precode", - "defaultValue": "", - "description": "SQL which executes while opening connection" } }, "editor": { diff --git a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java index 197c368154c..2e7e1a5116e 100644 --- a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java +++ b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java @@ -19,6 +19,8 @@ import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_PASSWORD; import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_USER; import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_URL; +import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_PRECODE; +import static org.apache.zeppelin.jdbc.JDBCInterpreter.PRECODE_KEY_TEMPLATE; import static org.apache.zeppelin.jdbc.JDBCInterpreter.COMMON_MAX_LINE; import static org.junit.Assert.*; @@ -44,8 +46,6 @@ import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter; -import static org.apache.zeppelin.jdbc.JDBCInterpreter.ZEPPELIN_JDBC_PRECODE_KEY; - /** * JDBC interpreter unit tests */ @@ -397,7 +397,7 @@ public void testPrecode() throws SQLException, IOException { properties.setProperty("default.url", getJdbcConnection()); properties.setProperty("default.user", ""); properties.setProperty("default.password", ""); - properties.setProperty(ZEPPELIN_JDBC_PRECODE_KEY, "SET @testVariable=1"); + properties.setProperty(DEFAULT_PRECODE, "SET @testVariable=1"); JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties); jdbcInterpreter.open(); @@ -417,7 +417,7 @@ public void testIncorrectPrecode() throws SQLException, IOException { properties.setProperty("default.url", getJdbcConnection()); properties.setProperty("default.user", ""); properties.setProperty("default.password", ""); - properties.setProperty(ZEPPELIN_JDBC_PRECODE_KEY, "incorrect command"); + properties.setProperty(DEFAULT_PRECODE, "incorrect command"); JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties); jdbcInterpreter.open(); @@ -428,4 +428,24 @@ public void testIncorrectPrecode() throws SQLException, IOException { assertEquals(InterpreterResult.Code.ERROR, interpreterResult.code()); assertEquals(InterpreterResult.Type.TEXT, interpreterResult.message().get(0).getType()); } - } + + @Test + public void testPrecodeWithAnotherPrefix() throws SQLException, IOException { + Properties properties = new Properties(); + properties.setProperty("anotherPrefix.driver", "org.h2.Driver"); + properties.setProperty("anotherPrefix.url", getJdbcConnection()); + properties.setProperty("anotherPrefix.user", ""); + properties.setProperty("anotherPrefix.password", ""); + properties.setProperty(String.format(PRECODE_KEY_TEMPLATE, "anotherPrefix"), "SET @testVariable=2"); + JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties); + jdbcInterpreter.open(); + + String sqlQuery = "(anotherPrefix) select @testVariable"; + + InterpreterResult interpreterResult = jdbcInterpreter.interpret(sqlQuery, interpreterContext); + + assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); + assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); + assertEquals("@TESTVARIABLE\n2\n", interpreterResult.message().get(0).getData()); + } +} From a136a0e283e892eba5f9a054fd6a13ca9ff9a6cc Mon Sep 17 00:00:00 2001 From: Tinkoff DWH Date: Sun, 12 Mar 2017 23:59:32 +0500 Subject: [PATCH 2/3] [ZEPPELIN-2245] documentation for usage of precode --- docs/interpreter/jdbc.md | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/docs/interpreter/jdbc.md b/docs/interpreter/jdbc.md index aa95037cc9e..bd097e47107 100644 --- a/docs/interpreter/jdbc.md +++ b/docs/interpreter/jdbc.md @@ -222,6 +222,67 @@ SELECT name, country, performer FROM demo.performers WHERE name='{{"{{performer=Sheryl Crow|Doof|Fanfarlo|Los Paranoia"}}}}' ``` +### Usage `precode` +You can set `precode` for each data source. Code runs once while opening the connection. + +##### Properties + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Property NameValue
default.driverorg.postgresql.Driver
default.password1
default.urljdbc:postgresql://localhost:5432/
default.userpostgres
default.precodeset search_path='test_path'
mysql.drivercom.mysql.jdbc.Driver
mysql.password1
mysql.urljdbc:mysql://localhost:3306/
mysql.userroot
mysql.precodeset @v=12
+ +##### Usage +```sql +%jdbc +show search_path +``` + +```sql +%jdbc(mysql) +select @v +``` ## Examples Here are some examples you can refer to. Including the below connectors, you can connect every databases as long as it can be configured with it's JDBC driver. From 970c064ddd6f3534f5b2a33803f40fab71187d6e Mon Sep 17 00:00:00 2001 From: Tinkoff DWH Date: Tue, 14 Mar 2017 12:56:15 +0500 Subject: [PATCH 3/3] [ZEPPELIN-2245] editing documentation --- docs/interpreter/jdbc.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/interpreter/jdbc.md b/docs/interpreter/jdbc.md index bd097e47107..28403fc92fd 100644 --- a/docs/interpreter/jdbc.md +++ b/docs/interpreter/jdbc.md @@ -222,10 +222,12 @@ SELECT name, country, performer FROM demo.performers WHERE name='{{"{{performer=Sheryl Crow|Doof|Fanfarlo|Los Paranoia"}}}}' ``` -### Usage `precode` -You can set `precode` for each data source. Code runs once while opening the connection. +### Usage *precode* +You can set *precode* for each data source. Code runs once while opening the connection. ##### Properties +An example settings of interpreter for the two data sources, each of which has its *precode* parameter. + @@ -274,15 +276,21 @@ You can set `precode` for each data source. Code runs once while opening the con
Property Name
##### Usage +Test of execution *precode* for each data source. + ```sql %jdbc show search_path ``` +Returns value of `search_path` which is set in the *default.precode*. + ```sql %jdbc(mysql) select @v ``` +Returns value of `v` which is set in the *mysql.precode*. + ## Examples Here are some examples you can refer to. Including the below connectors, you can connect every databases as long as it can be configured with it's JDBC driver.