Skip to content

Commit eeb7eb2

Browse files
authored
Add TrinoContainer (#3668)
1 parent 8965712 commit eeb7eb2

File tree

13 files changed

+313
-3
lines changed

13 files changed

+313
-3
lines changed

docs/modules/databases/jdbc.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ Insert `tc:` after `jdbc:` as follows. Note that the hostname, port and database
3939

4040
`jdbc:tc:postgis:9.6-2.5:///databasename`
4141

42-
#### Using Presto
42+
#### Using Trino
4343

44-
`jdbc:tc:presto:344://localhost/memory/default`
44+
`jdbc:tc:trino:352://localhost/memory/default`
4545

4646
### Using a classpath init script
4747

docs/modules/databases/presto.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Presto Module
22

33
!!! note
4-
This module is INCUBATING. While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. See [our contributing guidelines](/contributing/#incubating-modules) for more information on our incubating modules policy.
4+
This module is deprecated, use Trino module.
55

66
See [Database containers](./index.md) for documentation and usage that is common to all database container types.
77

docs/modules/databases/trino.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Trino Module
2+
3+
!!! note
4+
This module is INCUBATING. While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. See [our contributing guidelines](/contributing/#incubating-modules) for more information on our incubating modules policy.
5+
6+
See [Database containers](./index.md) for documentation and usage that is common to all database container types.
7+
8+
## Usage example
9+
10+
Running Trino as a stand-in for in a test:
11+
12+
```java
13+
public class SomeTest {
14+
15+
@Rule
16+
public TrinoContainer trino = new TrinoContainer();
17+
18+
@Test
19+
public void someTestMethod() {
20+
String url = trino.getJdbcUrl();
21+
22+
... create a connection and run test as normal
23+
```
24+
25+
Trino comes with several catalogs preconfigured. Most useful ones for testing are
26+
27+
* `tpch` catalog using the [Trino TPCH Connector](https://trino.io/docs/current/connector/tpch.html).
28+
This is a read-only catalog that defines standard TPCH schema, so is available for querying without a need
29+
to create any tables.
30+
* `memory` catalog using the [Trino Memory Connector](https://trino.io/docs/current/connector/memory.html).
31+
This catalog can be used for creating schemas and tables and does not require any storage, as everything
32+
is stored fully in-memory.
33+
34+
Example test using the `tpch` and `memory` catalogs:
35+
36+
```java
37+
public class SomeTest {
38+
@Rule
39+
public TrinoContainer trino = new TrinoContainer();
40+
41+
@Test
42+
public void queryMemoryAndTpchConnectors() throws SQLException {
43+
try (Connection connection = trino.createConnection();
44+
Statement statement = connection.createStatement()) {
45+
// Prepare data
46+
statement.execute("CREATE TABLE memory.default.table_with_array AS SELECT 1 id, ARRAY[1, 42, 2, 42, 4, 42] my_array");
47+
48+
// Query Trino using newly created table and a builtin connector
49+
try (ResultSet resultSet = statement.executeQuery("" +
50+
"SELECT nationkey, element " +
51+
"FROM tpch.tiny.nation " +
52+
"JOIN memory.default.table_with_array twa ON nationkey = twa.id " +
53+
"LEFT JOIN UNNEST(my_array) a(element) ON true " +
54+
"ORDER BY element OFFSET 1 FETCH NEXT 3 ROWS WITH TIES ")) {
55+
List<Integer> actualElements = new ArrayList<>();
56+
while (resultSet.next()) {
57+
actualElements.add(resultSet.getInt("element"));
58+
}
59+
Assert.assertEquals(Arrays.asList(2, 4, 42, 42, 42), actualElements);
60+
}
61+
}
62+
}
63+
}
64+
```
65+
66+
## Adding this module to your project dependencies
67+
68+
Add the following dependency to your `pom.xml`/`build.gradle` file:
69+
70+
```groovy tab='Gradle'
71+
testCompile "org.testcontainers:trino:{{latest_version}}"
72+
```
73+
74+
```xml tab='Maven'
75+
<dependency>
76+
<groupId>org.testcontainers</groupId>
77+
<artifactId>trino</artifactId>
78+
<version>{{latest_version}}</version>
79+
<scope>test</scope>
80+
</dependency>
81+
```
82+
83+
!!! hint
84+
Adding this Testcontainers library JAR will not automatically add the Trino JDBC driver JAR to your project.
85+
You should ensure that your project has the Trino JDBC driver as a dependency, if you plan on using it.
86+
Refer to [Trino project download page](https://trino.io/download.html) for instructions.
87+
88+

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ nav:
6060
- modules/databases/orientdb.md
6161
- modules/databases/postgres.md
6262
- modules/databases/presto.md
63+
- modules/databases/trino.md
6364
- modules/docker_compose.md
6465
- modules/elasticsearch.md
6566
- modules/gcloud.md

modules/presto/src/main/java/org/testcontainers/containers/PrestoContainer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import static java.lang.String.format;
1515
import static java.time.temporal.ChronoUnit.SECONDS;
1616

17+
/**
18+
* @deprecated Use {@code TrinoContainer} instead.
19+
*/
20+
@Deprecated
1721
public class PrestoContainer<SELF extends PrestoContainer<SELF>> extends JdbcDatabaseContainer<SELF> {
1822
public static final String NAME = "presto";
1923
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("ghcr.io/trinodb/presto");

modules/trino/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
description = "Testcontainers :: JDBC :: Trino"
2+
3+
dependencies {
4+
compile project(':jdbc')
5+
6+
testCompile project(':jdbc-test')
7+
testCompile 'io.trino:trino-jdbc:352'
8+
compileOnly 'org.jetbrains:annotations:20.0.0'
9+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.testcontainers.containers;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.VisibleForTesting;
5+
import org.testcontainers.utility.DockerImageName;
6+
7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
12+
import static com.google.common.base.Strings.nullToEmpty;
13+
import static java.lang.String.format;
14+
15+
public class TrinoContainer extends JdbcDatabaseContainer<TrinoContainer> {
16+
static final String NAME = "trino";
17+
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("trinodb/trino");
18+
static final String IMAGE = "trinodb/trino";
19+
@VisibleForTesting
20+
static final String DEFAULT_TAG = "352";
21+
22+
private static final int TRINO_PORT = 8080;
23+
24+
private String username = "test";
25+
private String catalog = null;
26+
27+
public TrinoContainer(final String dockerImageName) {
28+
this(DockerImageName.parse(dockerImageName));
29+
}
30+
31+
public TrinoContainer(final DockerImageName dockerImageName) {
32+
super(dockerImageName);
33+
34+
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
35+
addExposedPort(TRINO_PORT);
36+
}
37+
38+
@NotNull
39+
@Override
40+
protected Set<Integer> getLivenessCheckPorts() {
41+
return new HashSet<>(getMappedPort(TRINO_PORT));
42+
}
43+
44+
@Override
45+
public String getDriverClassName() {
46+
return "io.trino.jdbc.TrinoDriver";
47+
}
48+
49+
@Override
50+
public String getJdbcUrl() {
51+
return format("jdbc:trino://%s:%s/%s", getHost(), getMappedPort(TRINO_PORT), nullToEmpty(catalog));
52+
}
53+
54+
@Override
55+
public String getUsername() {
56+
return username;
57+
}
58+
59+
@Override
60+
public String getPassword() {
61+
return "";
62+
}
63+
64+
@Override
65+
public String getDatabaseName() {
66+
return catalog;
67+
}
68+
69+
@Override
70+
public String getTestQueryString() {
71+
return "SELECT count(*) FROM tpch.tiny.nation";
72+
}
73+
74+
@Override
75+
public TrinoContainer withUsername(final String username) {
76+
this.username = username;
77+
return this;
78+
}
79+
80+
@Override
81+
public TrinoContainer withDatabaseName(String dbName) {
82+
this.catalog = dbName;
83+
return this;
84+
}
85+
86+
public Connection createConnection() throws SQLException, NoDriverFoundException {
87+
return createConnection("");
88+
}
89+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.testcontainers.containers;
2+
3+
import org.testcontainers.utility.DockerImageName;
4+
5+
/**
6+
* Factory for Trino containers.
7+
*/
8+
public class TrinoContainerProvider extends JdbcDatabaseContainerProvider {
9+
@Override
10+
public boolean supports(String databaseType) {
11+
return databaseType.equals(TrinoContainer.NAME);
12+
}
13+
14+
@Override
15+
public JdbcDatabaseContainer newInstance() {
16+
return newInstance(TrinoContainer.DEFAULT_TAG);
17+
}
18+
19+
@Override
20+
public JdbcDatabaseContainer newInstance(String tag) {
21+
return new TrinoContainer(DockerImageName.parse(TrinoContainer.IMAGE).withTag(tag));
22+
}
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.testcontainers.containers.TrinoContainerProvider
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.testcontainers;
2+
3+
import org.testcontainers.utility.DockerImageName;
4+
5+
public interface TrinoTestImages {
6+
DockerImageName TRINO_TEST_IMAGE = DockerImageName.parse("trinodb/trino:352");
7+
DockerImageName TRINO_PREVIOUS_VERSION_TEST_IMAGE = DockerImageName.parse("trinodb/trino:351");
8+
}

0 commit comments

Comments
 (0)