Connect To Dataphin By Using JDBC
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.
NoteIf 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.
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
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]
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.
| 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 | 80 |
catalog | Yes | The default query scope.
Note
| Exprojectname |
tenant_id | Yes | The tenant ID to query. | 111***111 |
ssl | No | Specifies whether to use HTTPS.
| False |
currentschema | No | The schema of the data source.
| 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.
| Exprojectname |
user | Yes | The user's AccessKey ID or the platform's AccessKey ID. In proxy mode, use the platform's AccessKey ID.
| kIB**********PT0 |
log_level | No | The log level. Valid values:
| DEBUG |
password | Yes | The user's AccessKey Secret.
| 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.
Note
| USER_ID |
connect_timeout | No | The connection timeout in seconds.
| 10 |
engine | No | The offline engine for the project specified by
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.
Proxy mode
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.
Dataphin driver reference
com.aliyun.dataphin.jdbc.DataphinDriver
API | Description | Signature |
connect | Establishes a database connection. | |
acceptsURL | Checks whether the URL is supported. | |
com.aliyun.dataphin.jdbc.DataphinConnection
API | Description | Signature |
createStatement | Creates a Statement object. | |
prepareStatement | Creates a PreparedStatement object. | |
com.aliyun.dataphin.jdbc.DataphinStatement
API | Description | Signature |
executeQuery | Executes an SQL statement and returns a ResultSet object. | |
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. | |
cancel | Cancels the execution of the Statement object. | |
com.aliyun.dataphin.jdbc.DataphinPreparedStatement
API | Description | Signature |
executeQuery | Executes an SQL statement and returns a ResultSet object. | |
com.aliyun.dataphin.jdbc.DataphinResultSetMetaData
API | Description | Signature |
getColumnCount | Gets the number of columns in the result schema. | |
getColumnName | Gets the name of a column in the result schema. | |
com.aliyun.dataphin.jdbc.ResultSet
API | Description | Signature |
next | Retrieves the results of an SQL execution row by row. | |
com.aliyun.dataphin.jdbc.DatabaseMetaData
API | Description | Signature |
getTables | Gets information about tables.
| |
getColumns | Gets information about the columns of a table.
| |
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;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:
|
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.