From 73e5a4519cf2c1c2079fa4cee1c9c04e1f56348e Mon Sep 17 00:00:00 2001 From: Liangliang Gu Date: Mon, 19 Apr 2021 14:20:46 +0800 Subject: [PATCH] support pass tidb ip by envirenment variables (#66) --- .ci/test.sh | 5 +- README.md | 5 +- .../io/tidb/bigdata/flink/tidb/FlinkTest.java | 38 ++++++++++++- .../io/tidb/bigdata/flink/tidb/FlinkTest.java | 42 ++++++++++++--- .../io/tidb/bigdata/jdbc/TiDBDriverTest.java | 54 ++++++++++++------- 5 files changed, 116 insertions(+), 28 deletions(-) diff --git a/.ci/test.sh b/.ci/test.sh index bb00433f..ab682315 100755 --- a/.ci/test.sh +++ b/.ci/test.sh @@ -3,7 +3,10 @@ set -x set -euo pipefail -export TIDB_URL="jdbc:tidb://127.0.0.1:4000?user=root&password=" +export TIDB_HOST="127.0.0.1" +export TIDB_PORT="4000" +export TIDB_USER="root" +export TIDB_PASSWORD="" mvn clean test -am -pl jdbc mvn clean test -am -pl ticdc mvn clean test -am -pl flink/flink-1.11 diff --git a/README.md b/README.md index 67d13733..58197663 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ TiBigData project is under the Apache 2.0 license. See the [LICENSE](./LICENSE) Use the following command to run all the tests. ``` -TIDB_URL="jdbc:tidb://172.0.0.1:4000?user=root&password=" +export TIDB_HOST="127.0.0.1" +export TIDB_PORT="4000" +export TIDB_USER="root" +export TIDB_PASSWORD="" mvn test ``` diff --git a/flink/flink-1.11/src/test/java/io/tidb/bigdata/flink/tidb/FlinkTest.java b/flink/flink-1.11/src/test/java/io/tidb/bigdata/flink/tidb/FlinkTest.java index 8c5a57a0..3176dc4f 100644 --- a/flink/flink-1.11/src/test/java/io/tidb/bigdata/flink/tidb/FlinkTest.java +++ b/flink/flink-1.11/src/test/java/io/tidb/bigdata/flink/tidb/FlinkTest.java @@ -3,6 +3,7 @@ import static io.tidb.bigdata.tidb.ClientConfig.DATABASE_URL; import static io.tidb.bigdata.tidb.ClientConfig.MAX_POOL_SIZE; import static io.tidb.bigdata.tidb.ClientConfig.MIN_IDLE_SIZE; +import static io.tidb.bigdata.tidb.ClientConfig.PASSWORD; import static io.tidb.bigdata.tidb.ClientConfig.TIDB_REPLICA_READ; import static io.tidb.bigdata.tidb.ClientConfig.TIDB_WRITE_MODE; import static io.tidb.bigdata.tidb.ClientConfig.USERNAME; @@ -22,6 +23,36 @@ public class FlinkTest { + public static final String TIDB_HOST = "TIDB_HOST"; + + public static final String TIDB_PORT = "TIDB_PORT"; + + public static final String TIDB_USER = "TIDB_USER"; + + public static final String TIDB_PASSWORD = "TIDB_PASSWORD"; + + public static final String tidbHost = getEnvOrDefault(TIDB_HOST, "127.0.0.1"); + + public static final String tidbPort = getEnvOrDefault(TIDB_PORT, "4000"); + + public static final String tidbUser = getEnvOrDefault(TIDB_USER, "root"); + + public static final String tidbPassword = getEnvOrDefault(TIDB_PASSWORD, ""); + + private static String getEnvOrDefault(String key, String default0) { + String tmp = System.getenv(key); + if (tmp != null && !tmp.equals("")) { + return tmp; + } + + tmp = System.getProperty(key); + if (tmp != null && !tmp.equals("")) { + return tmp; + } + + return default0; + } + public static final String CATALOG_NAME = "tidb"; public static final String DATABASE_NAME = "test"; @@ -120,8 +151,11 @@ public static String getRandomTableName() { public Map getDefaultProperties() { Map properties = new HashMap<>(); properties.put(DATABASE_URL, - "jdbc:mysql://127.0.0.1:4000/test?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false"); - properties.put(USERNAME, "root"); + String.format( + "jdbc:mysql://%s:%s/test?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false", + tidbHost, tidbPort)); + properties.put(USERNAME, tidbUser); + properties.put(PASSWORD, tidbPassword); properties.put(MAX_POOL_SIZE, "1"); properties.put(MIN_IDLE_SIZE, "1"); return properties; diff --git a/flink/flink-1.12/src/test/java/io/tidb/bigdata/flink/tidb/FlinkTest.java b/flink/flink-1.12/src/test/java/io/tidb/bigdata/flink/tidb/FlinkTest.java index d05f199e..df93a84f 100644 --- a/flink/flink-1.12/src/test/java/io/tidb/bigdata/flink/tidb/FlinkTest.java +++ b/flink/flink-1.12/src/test/java/io/tidb/bigdata/flink/tidb/FlinkTest.java @@ -3,7 +3,7 @@ import static io.tidb.bigdata.tidb.ClientConfig.DATABASE_URL; import static io.tidb.bigdata.tidb.ClientConfig.MAX_POOL_SIZE; import static io.tidb.bigdata.tidb.ClientConfig.MIN_IDLE_SIZE; -import static io.tidb.bigdata.tidb.ClientConfig.TIDB_FILTER_PUSH_DOWN; +import static io.tidb.bigdata.tidb.ClientConfig.PASSWORD; import static io.tidb.bigdata.tidb.ClientConfig.TIDB_REPLICA_READ; import static io.tidb.bigdata.tidb.ClientConfig.TIDB_WRITE_MODE; import static io.tidb.bigdata.tidb.ClientConfig.USERNAME; @@ -17,17 +17,45 @@ import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.IntStream; -import org.apache.flink.table.api.DataTypes; import org.apache.flink.table.api.EnvironmentSettings; import org.apache.flink.table.api.TableEnvironment; import org.apache.flink.table.api.TableResult; -import org.apache.flink.table.types.DataType; import org.apache.flink.types.Row; import org.junit.Assert; import org.junit.Test; public class FlinkTest { + public static final String TIDB_HOST = "TIDB_HOST"; + + public static final String TIDB_PORT = "TIDB_PORT"; + + public static final String TIDB_USER = "TIDB_USER"; + + public static final String TIDB_PASSWORD = "TIDB_PASSWORD"; + + public static final String tidbHost = getEnvOrDefault(TIDB_HOST, "127.0.0.1"); + + public static final String tidbPort = getEnvOrDefault(TIDB_PORT, "4000"); + + public static final String tidbUser = getEnvOrDefault(TIDB_USER, "root"); + + public static final String tidbPassword = getEnvOrDefault(TIDB_PASSWORD, ""); + + private static String getEnvOrDefault(String key, String default0) { + String tmp = System.getenv(key); + if (tmp != null && !tmp.equals("")) { + return tmp; + } + + tmp = System.getProperty(key); + if (tmp != null && !tmp.equals("")) { + return tmp; + } + + return default0; + } + public static final String CATALOG_NAME = "tidb"; public static final String DATABASE_NAME = "test"; @@ -126,11 +154,13 @@ public static String getRandomTableName() { public Map getDefaultProperties() { Map properties = new HashMap<>(); properties.put(DATABASE_URL, - "jdbc:mysql://127.0.0.1:4000/test?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false"); - properties.put(USERNAME, "root"); + String.format( + "jdbc:mysql://%s:%s/test?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false", + tidbHost, tidbPort)); + properties.put(USERNAME, tidbUser); + properties.put(PASSWORD, tidbPassword); properties.put(MAX_POOL_SIZE, "1"); properties.put(MIN_IDLE_SIZE, "1"); - properties.put(TIDB_FILTER_PUSH_DOWN, "true"); return properties; } diff --git a/jdbc/src/test/java/io/tidb/bigdata/jdbc/TiDBDriverTest.java b/jdbc/src/test/java/io/tidb/bigdata/jdbc/TiDBDriverTest.java index 94335b4e..a63a6afa 100644 --- a/jdbc/src/test/java/io/tidb/bigdata/jdbc/TiDBDriverTest.java +++ b/jdbc/src/test/java/io/tidb/bigdata/jdbc/TiDBDriverTest.java @@ -28,11 +28,43 @@ import org.junit.Test; public class TiDBDriverTest { - static final String JDBC_DRIVER = "io.tidb.bigdata.jdbc.TiDBDriver"; - static final String TIDB_URL_KEY = "TIDB_URL"; - static final String DEFAULT_TIDB_URL = "jdbc:tidb://127.0.0.1:4000?user=root&password="; - private final String tidbUrl = getTiDBUrl(); + public static final String JDBC_DRIVER = "io.tidb.bigdata.jdbc.TiDBDriver"; + + public static final String TIDB_HOST = "TIDB_HOST"; + + public static final String TIDB_PORT = "TIDB_PORT"; + + public static final String TIDB_USER = "TIDB_USER"; + + public static final String TIDB_PASSWORD = "TIDB_PASSWORD"; + + public static final String tidbHost = getEnvOrDefault(TIDB_HOST, "127.0.0.1"); + + public static final String tidbPort = getEnvOrDefault(TIDB_PORT, "4000"); + + public static final String tidbUser = getEnvOrDefault(TIDB_USER, "root"); + + public static final String tidbPassword = getEnvOrDefault(TIDB_PASSWORD, ""); + + private static String getEnvOrDefault(String key, String default0) { + String tmp = System.getenv(key); + if (tmp != null && !tmp.equals("")) { + return tmp; + } + + tmp = System.getProperty(key); + if (tmp != null && !tmp.equals("")) { + return tmp; + } + + return default0; + } + + public static final String tidbUrl = String + .format("jdbc:tidb://%s:%s?user=%s&password=%s", tidbHost, tidbPort, tidbUser, + tidbPassword); + private Connection conn = null; @Test @@ -50,20 +82,6 @@ public void testTiDBDriver() throws ClassNotFoundException, SQLException { conn.close(); } - private String getTiDBUrl() { - String tmp = System.getenv(TIDB_URL_KEY); - if (tmp != null && !tmp.equals("")) { - return tmp; - } - - tmp = System.getProperty(TIDB_URL_KEY); - if (tmp != null && !tmp.equals("")) { - return tmp; - } - - return DEFAULT_TIDB_URL; - } - private void executeUpdate(String sql) throws SQLException { try (Statement tidbStmt = conn.createStatement()) { tidbStmt.executeUpdate(sql);