Skip to content

Commit

Permalink
support pass tidb ip by envirenment variables (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
marsishandsome authored Apr 19, 2021
1 parent 9c06f70 commit 73e5a45
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 28 deletions.
5 changes: 4 additions & 1 deletion .ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -120,8 +151,11 @@ public static String getRandomTableName() {
public Map<String, String> getDefaultProperties() {
Map<String, String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -126,11 +154,13 @@ public static String getRandomTableName() {
public Map<String, String> getDefaultProperties() {
Map<String, String> 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;
}

Expand Down
54 changes: 36 additions & 18 deletions jdbc/src/test/java/io/tidb/bigdata/jdbc/TiDBDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 73e5a45

Please sign in to comment.