2bethere commented on code in PR #13343: URL: https://github.com/apache/druid/pull/13343#discussion_r1039035923
########## docs/querying/sql-jdbc.md: ########## @@ -82,3 +90,112 @@ statement.setString(1, "abc"); statement.setString(2, "def"); final ResultSet resultSet = statement.executeQuery(); ``` + +## Examples + +<!-- docs/tutorial-jdbc.md redirects here --> + +The following section contains two complete samples that use the JDBC connector: + +- [Get the metadata for a datasource](#get-the-metadata-for-a-datasource) shows you how to query the `INFORMATION_SCHEMA` to get metadata like column names. +- [Query data](#query-data) runs a select query against the datasource. + +You can try out these examples after verifying that you meet the [prerequisites](#prerequisites). + +For more information about the connection options, see [Client Reference](https://calcite.apache.org/avatica/docs/client_reference.html). + +### Prerequisites + +Make sure you meet the following requirements before trying these examples: + +- A supported Java version, such as Java 8 + +- [Avatica JDBC driver](https://calcite.apache.org/avatica/downloads/). You can add the JAR to your `CLASSPATH` directly or manage it externally, such as through Maven and a `pom.xml` file. + +- An available Druid instance. You can use the `micro-quickstart` configuration described in [Quickstart (local)](../tutorials/index.md). The examples assume that you are using the quickstart, so no authentication or authorization is expected unless explicitly mentioned. + +- The example `wikipedia` datasource from the quickstart is loaded on your Druid instance. If you have a different datasource loaded, you can still try these examples. You'll have to update the table name and column names to match your datasource. + +### Get the metadata for a datasource + +Metadata, such as column names, is available either through the [`INFORMATION_SCHEMA`](../querying/sql-metadata-tables.md) table or through `connect.getMetaData()`. The following example uses the `INFORMATION_SCHEMA` table to retrieve and print the list of column names for the `wikipedia` datasource that you loaded during a previous tutorial. + +```java +import java.sql.*; +import java.util.Properties; + +public class JdbcListColumns { + + public static void main(String args[]) throws SQLException + { + // Connect to /druid/v2/sql/avatica/ on your Router. + // You can connect to a Broker but must configure connection stickiness if you do. + String url = "jdbc:avatica:remote:url=http://localhost:8888/druid/v2/sql/avatica/"; + + String query = "SELECT COLUMN_NAME,* FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'wikipedia' and TABLE_SCHEMA='druid'"; + // Set any connection context parameters you need here + // Or leave empty for default behavior. + Properties connectionProperties = new Properties(); + + try (Connection connection = DriverManager.getConnection(url, connectionProperties)) { + try ( + final Statement statement = connection.createStatement(); + final ResultSet rs = statement.executeQuery(query) + ) { + while (rs.next()) { + String columnName = rs.getString("COLUMN_NAME"); + System.out.println(columnName); + } + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} +``` + +### Query data + +Now that you know what columns are available, you can start querying the data. The following example queries the datasource named `wikipedia` for the timestamps and comments from Japan. It also sets the [query context parameter](../querying/sql-query-context.md) `sqlTimeZone` . Optionally, you can also parameterize queries by using [dynamic parameters](#dynamic-parameters). + +```java +import java.sql.*; +import java.util.Properties; + +public class JdbcCountryAndTime { + + public static void main(String args[]) throws SQLException + { + // Connect to /druid/v2/sql/avatica/ on your Router. + // You can connect to a Broker but must configure connection stickiness if you do. + String url = "jdbc:avatica:remote:url=http://localhost:8888/druid/v2/sql/avatica/"; + + //The query you want to run. + String query = "SELECT __time, isRobot, countryName, comment FROM wikipedia WHERE countryName='Japan'"; + // Set any connection context parameters you need here + // Or leave empty for default behavior. + Properties connectionProperties = new Properties(); + connectionProperties.setProperty("sqlTimeZone", "America/Los_Angeles"); Review Comment: I think we can add a comment here that this is optional. Users will get back UTC is this is not set (which is recommended) It's usually preferred to do timezone adjustments on the client side. The browser knows the timezone that the user is in. ########## docs/querying/sql-jdbc.md: ########## @@ -82,3 +90,112 @@ statement.setString(1, "abc"); statement.setString(2, "def"); final ResultSet resultSet = statement.executeQuery(); ``` + +## Examples + +<!-- docs/tutorial-jdbc.md redirects here --> + +The following section contains two complete samples that use the JDBC connector: + +- [Get the metadata for a datasource](#get-the-metadata-for-a-datasource) shows you how to query the `INFORMATION_SCHEMA` to get metadata like column names. +- [Query data](#query-data) runs a select query against the datasource. + +You can try out these examples after verifying that you meet the [prerequisites](#prerequisites). + +For more information about the connection options, see [Client Reference](https://calcite.apache.org/avatica/docs/client_reference.html). + +### Prerequisites + +Make sure you meet the following requirements before trying these examples: + +- A supported Java version, such as Java 8 Review Comment: Suggesting using Java 11. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
