-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Presto container * Add incubating note to module docs Co-authored-by: Richard North <rich.north@gmail.com>
- Loading branch information
Showing
11 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Presto Module | ||
|
||
!!! note | ||
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. | ||
|
||
See [Database containers](./index.md) for documentation and usage that is common to all database container types. | ||
|
||
## Usage example | ||
|
||
Running Presto as a stand-in for in a test: | ||
|
||
```java | ||
public class SomeTest { | ||
|
||
@Rule | ||
public PrestoContainer presto = new PrestoContainer(); | ||
|
||
@Test | ||
public void someTestMethod() { | ||
String url = presto.getJdbcUrl(); | ||
|
||
... create a connection and run test as normal | ||
``` | ||
|
||
Presto comes with several catalogs preconfigured. Most useful ones for testing are | ||
|
||
* `tpch` catalog using the [Presto TPCH Connector](https://prestosql.io/docs/current/connector/tpch.html). | ||
This is a read-only catalog that defines standard TPCH schema, so is available for querying without a need | ||
to create any tables. | ||
* `memory` catalog using the [Presto Memory Connector](https://prestosql.io/docs/current/connector/memory.html). | ||
This catalog can be used for creating schemas and tables and does not require any storage, as everything | ||
is stored fully in-memory. | ||
|
||
Example test using the `tpch` and `memory` catalogs: | ||
|
||
```java | ||
public class SomeTest { | ||
@Rule | ||
public PrestoContainer prestoSql = new PrestoContainer(); | ||
|
||
@Test | ||
public void queryMemoryAndTpchConnectors() throws SQLException { | ||
try (Connection connection = prestoSql.createConnection(); | ||
Statement statement = connection.createStatement()) { | ||
// Prepare data | ||
statement.execute("CREATE TABLE memory.default.table_with_array AS SELECT 1 id, ARRAY[1, 42, 2, 42, 4, 42] my_array"); | ||
|
||
// Query Presto using newly created table and a builtin connector | ||
try (ResultSet resultSet = statement.executeQuery("" + | ||
"SELECT nationkey, element " + | ||
"FROM tpch.tiny.nation " + | ||
"JOIN memory.default.table_with_array twa ON nationkey = twa.id " + | ||
"LEFT JOIN UNNEST(my_array) a(element) ON true " + | ||
"ORDER BY element OFFSET 1 FETCH NEXT 3 ROWS WITH TIES ")) { | ||
List<Integer> actualElements = new ArrayList<>(); | ||
while (resultSet.next()) { | ||
actualElements.add(resultSet.getInt("element")); | ||
} | ||
Assert.assertEquals(Arrays.asList(2, 4, 42, 42, 42), actualElements); | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Adding this module to your project dependencies | ||
|
||
Add the following dependency to your `pom.xml`/`build.gradle` file: | ||
|
||
```groovy tab='Gradle' | ||
testCompile "org.testcontainers:presto:{{latest_version}}" | ||
``` | ||
|
||
```xml tab='Maven' | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>presto</artifactId> | ||
<version>{{latest_version}}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
``` | ||
|
||
!!! hint | ||
Adding this Testcontainers library JAR will not automatically add the Presto JDBC driver JAR to your project. | ||
You should ensure that your project has the Presto JDBC driver as a dependency, if you plan on using it. | ||
Refer to [Presto project download page](https://prestosql.io/download.html) for instructions. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
description = "Testcontainers :: JDBC :: Presto" | ||
|
||
dependencies { | ||
compile project(':jdbc') | ||
|
||
testCompile 'io.prestosql:presto-jdbc:329' | ||
testCompile 'commons-dbutils:commons-dbutils:1.7' | ||
} |
104 changes: 104 additions & 0 deletions
104
modules/presto/src/main/java/org/testcontainers/containers/PrestoContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.time.Duration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static com.google.common.base.Strings.nullToEmpty; | ||
import static java.lang.String.format; | ||
import static java.time.temporal.ChronoUnit.SECONDS; | ||
|
||
public class PrestoContainer<SELF extends PrestoContainer<SELF>> extends JdbcDatabaseContainer<SELF> { | ||
public static final String NAME = "presto"; | ||
public static final String IMAGE = "prestosql/presto"; | ||
public static final String DEFAULT_TAG = "329"; | ||
|
||
public static final Integer PRESTO_PORT = 8080; | ||
|
||
private String username = "test"; | ||
private String catalog = null; | ||
|
||
public PrestoContainer() { | ||
this(IMAGE + ":" + DEFAULT_TAG); | ||
} | ||
|
||
public PrestoContainer(final String dockerImageName) { | ||
super(dockerImageName); | ||
this.waitStrategy = new LogMessageWaitStrategy() | ||
.withRegEx(".*io.prestosql.server.PrestoServer\\s+======== SERVER STARTED ========.*") | ||
.withStartupTimeout(Duration.of(60, SECONDS)); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
protected Set<Integer> getLivenessCheckPorts() { | ||
return new HashSet<>(getMappedPort(PRESTO_PORT)); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
addExposedPort(PRESTO_PORT); | ||
} | ||
|
||
@Override | ||
public String getDriverClassName() { | ||
return "io.prestosql.jdbc.PrestoDriver"; | ||
} | ||
|
||
@Override | ||
public String getJdbcUrl() { | ||
return format("jdbc:presto://%s:%s/%s", getContainerIpAddress(), getMappedPort(PRESTO_PORT), nullToEmpty(catalog)); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String getDatabaseName() { | ||
return catalog; | ||
} | ||
|
||
@Override | ||
public String getTestQueryString() { | ||
return "SELECT count(*) FROM tpch.tiny.nation"; | ||
} | ||
|
||
@Override | ||
public SELF withUsername(final String username) { | ||
this.username = username; | ||
return self(); | ||
} | ||
|
||
/** | ||
* @deprecated This operation is not supported. | ||
*/ | ||
@Override | ||
@Deprecated | ||
public SELF withPassword(final String password) { | ||
// ignored, Presto does not support password authentication without TLS | ||
// TODO: make JDBCDriverTest not pass a password unconditionally and remove this method | ||
return self(); | ||
} | ||
|
||
@Override | ||
public SELF withDatabaseName(String dbName) { | ||
this.catalog = dbName; | ||
return self(); | ||
} | ||
|
||
public Connection createConnection() throws SQLException, NoDriverFoundException { | ||
return createConnection(""); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
modules/presto/src/main/java/org/testcontainers/containers/PrestoContainerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.testcontainers.jdbc.ConnectionUrl; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Factory for Presto containers. | ||
*/ | ||
public class PrestoContainerProvider extends JdbcDatabaseContainerProvider { | ||
|
||
public static final String USER_PARAM = "user"; | ||
public static final String PASSWORD_PARAM = "password"; | ||
|
||
@Override | ||
public boolean supports(String databaseType) { | ||
return databaseType.equals(PrestoContainer.NAME); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance() { | ||
return newInstance(PrestoContainer.DEFAULT_TAG); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance(String tag) { | ||
return new PrestoContainer(PrestoContainer.IMAGE + ":" + tag); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance(ConnectionUrl connectionUrl) { | ||
return newInstanceFromConnectionUrl(connectionUrl, USER_PARAM, PASSWORD_PARAM); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...n/resources/META-INF/services/org.testcontainers.containers.JdbcDatabaseContainerProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.testcontainers.containers.PrestoContainerProvider |
Oops, something went wrong.