Connect To Dataphin By Using JDBC

Updated at:

This topic shows you how to connect to Dataphin by using a JDBC driver.

Prerequisites

The Dataphin JDBC driver uses Dataphin OpenAPI to provide SQL query capabilities. Before you use the Dataphin JDBC driver, you must enable the Dataphin OpenAPI feature.

Features

  • Two authentication modes: The Dataphin JDBC driver supports two authentication modes: simple mode and proxy mode. For more information, see Authentication modes.

  • Executing SQL statements with the Dataphin JDBC driver produces the same results as executing them in the Dataphin console. Configurations in Dataphin, such as permissions, desensitization rules, and security settings, also apply to queries made through the driver.

Limitations

  • You can use JDBC to connect to all compute sources and some data sources in Dataphin. Supported data sources include MySQL, Oracle, MaxCompute, Hive, AnalyticDB for MySQL 3.0, StarRocks, AnalyticDB for PostgreSQL, Doris, and PostgreSQL.

  • When you execute an SQL statement by using the Dataphin JDBC driver, the statement is preprocessed by Dataphin for tasks like SQL translation and desensitization. The result data is also transmitted and forwarded through Dataphin. This process adds overhead and may increase query times compared to querying the compute engine directly.

  • SQL statements executed via the Dataphin JDBC driver are submitted to Dataphin through OpenAPI and then forwarded to the underlying compute engine. Before using the Dataphin JDBC driver, estimate your expected call volume to determine if you need to scale out your Dataphin cluster and compute engine.

    Note

    If you cannot estimate your capacity needs, you can submit a capacity evaluation request for your Dataphin cluster to the Dataphin O&M team. This evaluation does not cover the compute engine.

  • Dataphin does not currently support traffic throttling or concurrency control. Evaluate the impact carefully.

  • Only compute engines such as MaxCompute support acceleration sources. You can specify an acceleration source and an acceleration source resource group in the Dataphin JDBC driver only if an acceleration source has been configured in Dataphin. For more information, see Acceleration source.

JDBC driver version and download link

Click here to download the driver JAR file.

Note

The current JAR file supports only Dataphin V4.4 and later. You cannot use a later version of the driver client to connect to an earlier version of Dataphin.

Connection parameters

Note

When the Dataphin JDBC driver uses the platform's AccessKey ID and AccessKey Secret (in proxy mode), you can use the set syntax to specify the user for the connection.

  • set dp_delegation_uid = 'target_source_user_uid';

  • set dp_delegation_name = 'target_user_name';

JDBC URL format: jdbc:dataphin://host:port/catalog?[tenant_id=TenantID][&ssl=true][&log_level=Log_Level][&user=UserName][&password=PassWord][&delegation_uid=DelegationUid][&account_type=AccountType][&connect_timeout=ConnectTimeout][&engine=engine_type][compute_project=ProjectName]

Important
  • Parameter names are case-sensitive.

  • The square brackets ([]) indicate optional parameters. Do not include them in your connection string.

Parameter

Required

Description

Example

host

Yes

The Dataphin OpenAPI endpoint. The endpoint varies by region. Select the appropriate endpoint based on the region where your Dataphin instance resides.

  • China (Beijing): dataphin-public.cn-beijing.aliyuncs.com

  • China (Hangzhou): dataphin-public.cn-hangzhou.aliyuncs.com

  • China (Shanghai): dataphin-public.cn-shanghai.aliyuncs.com

  • China (Shenzhen): dataphin-public.cn-shenzhen.aliyuncs.com

dataphin-public.cn-hangzhou.aliyuncs.com

port

No

The port depends on whether HTTPS is enabled for Dataphin OpenAPI. If HTTPS is enabled, use port 443. Otherwise, use port 80. The default is 80.

80

catalog

Yes

The default query scope.

  • To query Dataphin physical tables, enter the English name of the Dataphin project (project_name).

  • To query Dataphin logical tables, enter the English name of the Dataphin data plate, which starts with LD_.

  • To query a table from a data source managed by Dataphin, enter the code of the data source configured in Dataphin, which starts with ds_.

Note
  • If the default query scope is a data plate, the compute_project parameter is required.

  • If the query scope is a table in a managed data source, the compute_project parameter is not required.

Exprojectname

tenant_id

Yes

The tenant ID to query.

111***111

ssl

No

Specifies whether to use HTTPS.

  • True: Use an HTTPS domain name.

  • False: Use an HTTP domain name. This is the default value.

False

currentschema

No

The schema of the data source.

  • If the catalog uses Dataphin physical or logical tables, you do not need to specify this parameter.

  • If the catalog uses a data source type that does not support schemas, such as MySQL, you do not need to specify this parameter.

  • If the catalog uses a data source type that supports schemas, such as Oracle, this parameter is optional.

    • If you specify a schema, data within that schema is queried.

    • If you do not specify a schema, the default schema of the database is queried.

information_schema

compute_project

No

The project name in English. Dataphin uses this project as the execution project for the SQL query. The project and its bound compute source must have read permission on the tables.

  • If the query scope is a Dataphin project, this parameter is optional. If you do not specify it, Dataphin uses the project in the default query scope as the SQL execution project.

  • If the query scope is a Dataphin data plate, this parameter is required.

  • If the query scope is a Dataphin data source, you do not need to specify this parameter.

Exprojectname

user

Yes

The user's AccessKey ID or the platform's AccessKey ID. In proxy mode, use the platform's AccessKey ID.

  • To obtain the platform AccessKey ID, contact the Dataphin O&M team.

  • You can obtain your AccessKey ID by navigating to user center > AccessKey Management.

kIB**********PT0

log_level

No

The log level. Valid values:

  • DEBUG

  • INFO

  • WARNING

  • ERROR

DEBUG

password

Yes

The user's AccessKey Secret.

  • To obtain the platform AccessKey Secret, contact the Dataphin O&M team.

  • You can obtain your AccessKey Secret by navigating to user center > AccessKey Management.

Cy**************r2T

delegation_uid

No

The ID of the user to be proxied. The format must match the specified account_type. Setting this parameter enables proxy mode.

For a Dataphin user accessed by a proxy, you must pass the corresponding account ID based on the selected account_type. Setting this parameter enables authentication in proxy mode.

999***999

account_type

No

In proxy mode, you must specify the account type of the proxied user.

  • ACCOUNT_NAME: The Dataphin username. This mode is recommended when the application username matches the Dataphin username.

  • USER_ID: The unique internal ID in Dataphin. This is generally not recommended.

  • SOURCE_USER_ID: The source system account ID. This is available when Dataphin is configured for Single Sign-On (SSO) authentication, such as with RAM, SAML, or OAuth. It is the user's account in the identity provider (IdP).

Note
  • This parameter is required only when delegation_uid is set.

    If this parameter is not specified, the default type is USER_ID.

  • If duplicate users exist, authentication fails.

USER_ID

connect_timeout

No

The connection timeout in seconds.

  • Greater than 0: The timeout duration. The minimum value is 10.

  • Less than or equal to 0: Wait indefinitely.

10

engine

No

The offline engine for the project specified by project_name in the session or connection string. For a Hadoop compute source, the default engine is Hive, but you can set this parameter to Impala or Spark. The engine type must be pre-configured in the compute source. If the specified engine is not supported by the project, this setting is ignored and a warning is returned. Valid values:

  • MaxCompute

  • Hologres

  • Hive

  • Impala

  • Inceptor

  • ArgoDB

  • Spark

Note

This parameter is ignored when accessing a data source.

MaxCompute

acceleration_source

No

The acceleration source to use. You can select the code of any acceleration source within the tenant.

starrocks_code

acceleration_resource_group

No

The acceleration resource group. You can select a resource group that has been configured for the selected acceleration source.

starrocks_resource_group

Authentication modes

Simple mode

Set the username to the AccessKey ID of a RAM user and the password to the AccessKey Secret of the RAM user. The current connection is then identified as being from that user. For more information about how to create a RAM user, see Prepare a RAM user.

When you connect to Dataphin by using the JDBC driver, Dataphin authenticates the AccessKey and authorizes the user for access to resources or SQL execution. Dataphin then executes the SQL statement as that user.

image

Proxy mode

Important

Before you use proxy mode, contact the Dataphin O&M team to enable and configure this feature.

Proxy mode is ideal for system-level integrations because it avoids the need to distribute or configure an AccessKey for each user. Instead, your application uses a single platform-level AccessKey and specifies the user to proxy in the connection string. For example, if you set delegation_uid=userA, the request is made on behalf of userA. Dataphin then validates the request based on userA's feature and data permissions.

image

Dataphin driver reference

com.aliyun.dataphin.jdbc.DataphinDriver

API

Description

Signature

connect

Establishes a database connection.

Connection connect
(String url, Properties
info) throws 
SQLException;  

acceptsURL

Checks whether the URL is supported.

boolean acceptsURL(String url) 
throws SQLException;

com.aliyun.dataphin.jdbc.DataphinConnection

API

Description

Signature

createStatement

Creates a Statement object.

Statement createStatement
(int resultSetType, 
int resultSetConcurrency)
throws SQLException;

prepareStatement

Creates a PreparedStatement object.

PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency)throws SQLException;
PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException;

com.aliyun.dataphin.jdbc.DataphinStatement

API

Description

Signature

executeQuery

Executes an SQL statement and returns a ResultSet object.

ResultSet executeQuery
(String sql) throws 
SQLException;

setFetchSize

Fetches result data in batches based on the specified number of rows. If this is not set or is set to 0, the default value of 1000 is used.

void setFetchSize(int rows) 
throws SQLException

cancel

Cancels the execution of the Statement object.

void cancel() 
throws SQLException;

com.aliyun.dataphin.jdbc.DataphinPreparedStatement

API

Description

Signature

executeQuery

Executes an SQL statement and returns a ResultSet object.

ResultSet executeQuery
(String sql) 
throws SQLException;

com.aliyun.dataphin.jdbc.DataphinResultSetMetaData

API

Description

Signature

getColumnCount

Gets the number of columns in the result schema.

int getColumnCount() 
throws SQLException;

getColumnName

Gets the name of a column in the result schema.

String getColumnName(int column) 
throws SQLException;

com.aliyun.dataphin.jdbc.ResultSet

API

Description

Signature

next

Retrieves the results of an SQL execution row by row.

boolean next() 
throws SQLException;

com.aliyun.dataphin.jdbc.DatabaseMetaData

API

Description

Signature

getTables

Gets information about tables.

  • Parameters:

    • catalog: The default value is default.

    • schemaPattern: The project name or data plate name.

    • tableNamePattern: The table name. Wildcard matching is supported. Regular expressions are not supported.

    • types: This parameter is not supported.

  • Return value:

    • Returns a ResultSet object.

    • Use the next method to retrieve the execution results row by row. Each row contains the metadata of a single table. Only table names can be retrieved.

  • Get table name:

    • resultSet.getString("table_name")

getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException 

getColumns

Gets information about the columns of a table.

  • Parameters:

    • catalog: The project name or data plate name.

    • schemaPattern: This parameter is not supported.

    • tableNamePattern: The full name of the table. Wildcard matching and regular expressions are not supported.

    • columnNamePattern: This parameter is not supported.

  • Return value:

    • Returns a ResultSet object.

    • Use the next method to iterate through the results. Each row contains metadata for a single column, limited to its name and data type.

  • Get the column name: resultSet.getString("column_name").

  • Obtain the data type of the column: resultSet.getString("data_type").

ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)

Examples

You can use the Dataphin JDBC driver to retrieve catalog information.

1. Get a list of tables

Returns a list of physical tables and physical views in a project.

Syntax

SHOW TABLES
    [FROM db_name]
    [LIKE 'pattern']

Parameters

db_name:

  • Dataphin project name: Shows physical tables in the project.

  • You must explicitly add _Dev to development projects and data plates.

  • If db_name is not specified, tables in the project specified by project_name in the URL are displayed by default.

Result

Parameter

Type

Description

dim_user

logical table

User table.

ods_user

physical table

User source table.

ods_user_logical_view

logical view

A logical view.

ods_user_physical_view2

physical view

A physical view.

2. Get the schema of a table

Gets details about the columns of a physical table or physical view.

Syntax

{DESCRIBE | DESC} table_name;
Note

This command supports only physical tables and physical views.

Parameters

table_name: The name of the physical table or physical view.

Result

Parameter

Type

Description

ID

BigInt

User ID.

Name

String

Username.

DS

String

Partition time.

Server-side connection control

  • Maximum connections: The default value is 100.

  • Connection timeout: The default is 7,200s (2 hours). If a connection is idle for this period, the server automatically disconnects it. Attempting to perform an operation over this connection will result in an error or cause the client to close unexpectedly.

Task information submitted to MaxCompute

When a Dataphin JDBC task is submitted to a MaxCompute execution instance, Dataphin also submits the following information:

Parameter

Description

logical_project

The name of the Dataphin project in which the JDBC task is executed.

EXT_JDBC_TASKRUN_ID

The ID of the JDBC task.

EXT_DPN_TENANT_ID

The ID of the Dataphin tenant in which the JDBC task is executed.

EXT_PLATFORM_ID

The ID of the upper-layer platform that submits the task to MaxCompute. The default value is Dataphin.

biz_id

The Dataphin member ID of the user who executes the JDBC task.

odps.idata.userenv

The user environment information. This includes the Java SDK version, Java version, IP address, and MAC address. Example:

JavaSDK Revision:fcedc4d,Version:0.37.6,JavaVersion:1.8.0_152,IP:11.**.***.**,MAC:00-**-**-**-**-25.

You can use this information to analyze MaxCompute bills and view job execution times. For more information, see Collect statistics on top N accounts by cost and on time-consuming jobs.