Connect to Dataphin by using JDBC
This topic explains how to connect to Dataphin by using a jdbc driver.
Prerequisites
The Dataphin jdbc driver uses Dataphin OpenAPI to provide SQL query capabilities. You must enable the Dataphin OpenAPI feature before you use the Dataphin jdbc driver.
Overview
Two authentication modes: The Dataphin jdbc driver supports two authentication modes: simple mode and proxy mode. For more information, see Authentication modes.
Consistent execution results: SQL queries executed with the Dataphin jdbc driver produce the same results as queries run in the Dataphin console. Dataphin configurations, such as permissions, desensitization rules, security settings, and code specifications, also apply to queries executed through the jdbc driver.
Limitations
You can connect to Dataphin by using JDBC for all compute sources and some data sources. Supported data sources include MySQL, Oracle, MaxCompute, Hive, AnalyticDB for MySQL 3.0, StarRocks, AnalyticDB for PostgreSQL, and Doris.
The Dataphin jdbc driver adds performance overhead. The driver sends queries to Dataphin for preprocessing tasks like SQL translation and data desensitization. Dataphin then transfers and forwards the result sets. This process can increase query latency compared to querying a compute engine directly.
The Dataphin jdbc driver submits SQL queries to Dataphin through OpenAPI, and the queries are then executed by the underlying compute engine. Before you use the jdbc driver, evaluate your expected call volume to determine if you need to scale up your Dataphin cluster or compute engine.
NoteIf you cannot perform this evaluation, contact the Dataphin operations team to request a capacity assessment for your Dataphin cluster. This assessment does not cover the capacity of your compute engine.
Dataphin does not currently support traffic control or concurrency control. Carefully assess the potential impact before you proceed.
Only the following compute engines support acceleration sources: MaxCompute. You can specify an acceleration source and its resource group in the Dataphin JDBC connection only if the acceleration source is already configured in Dataphin. For more information, see Acceleration source.
JDBC driver version and download link
Click here to download the driver JAR package.
The current JAR package supports only Dataphin V4.4 and later. Later driver versions are not compatible with earlier Dataphin versions.
Connection parameters
When Dataphin JDBC uses a platform AccessKey ID and AccessKey Secret (in proxy mode), you can use the set syntax to set the access user.
set dp_delegation_uid = 'target_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 brackets are for illustrative purposes only. Remove them when you construct the URL.
Parameter | Required | Description | Example |
host | Yes | The endpoint for Dataphin OpenAPI. Each region has a different endpoint. Select the endpoint that corresponds to the region where your Dataphin instance is located.
| dataphin-public.cn-hangzhou.aliyuncs.com |
port | No | The port is determined by whether HTTPS is enabled for Dataphin OpenAPI. The port is | 80 |
catalog | Yes | The default scope for data queries.
Note
| Exprojectname |
tenant_id | Yes | The ID of the tenant to be queried. | 111***111 |
ssl | No | Indicates whether to use HTTPS. Valid values:
| False |
currentschema | No | The schema of the data source.
| information_schema |
compute_project | No | The English name of the project that executes the SQL query. The project and its associated compute source must have read permissions on the table.
| 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 AccessKey Secret of the user.
| Cy**************r2T |
delegation_uid | No | When using a platform AccessKey, you can connect to Dataphin on behalf of another user. This parameter specifies the Dataphin user to impersonate. Set this parameter to the account ID of the user to be impersonated, based on the selected account_type. Setting this parameter enables proxy mode authentication. | 999***999 |
account_type | No | When you use proxy mode authentication, you must specify the account type of the delegated user. Valid values:
Note
| USER_ID |
connect_timeout | No | The maximum time to wait for a connection, in seconds.
| 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 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, the setting is ignored and a warning is issued. Valid values:
Note This parameter is ignored when you access a data source. | MaxCompute |
acceleration_source | No | Specifies the acceleration source. You can use the code of any acceleration source within the tenant. | starrocks_code |
acceleration_resource_group | No | The acceleration resource group. You can select any resource group configured for the selected acceleration source. | starrocks_resource_group |
Authentication modes
Simple mode
Use the RAM user's AccessKey ID as the username and the RAM user's AccessKey Secret as the password. The connection is then authenticated as 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 corresponding user for the requested resources or SQL queries. Dataphin then executes the SQL query as that user.
Proxy mode
Before you can use proxy mode, you must contact the Dataphin operations team to enable and configure this feature.
System-level integrations with Dataphin typically use proxy mode. This method avoids the need to distribute or configure an AccessKey for each user. You can specify the user to impersonate in the JDBC connection string (JDBC URL) to authorize operations based on that user's permissions. The platform-level AccessKey has high privileges and can impersonate any user. As shown in the following figure, the client is configured with the platform AccessKey. When userA accesses the system through the client, delegation_uid=userA must be specified to indicate access on behalf of userA. Dataphin then validates the request based on userA's functional and data permissions.
Dataphin driver
com.aliyun.dataphin.jdbc.DataphinDriver
API | Description | API definition |
connect | Establishes a database connection. | |
acceptsURL | Checks if the driver supports the URL. | |
com.aliyun.dataphin.jdbc.DataphinConnection
API | Description | API definition |
createStatement | Creates a Statement object. | |
prepareStatement | Creates a PreparedStatement object. | |
com.aliyun.dataphin.jdbc.DataphinStatement
API | Description | API definition |
executeQuery | Executes an SQL query and returns a ResultSet. | |
setFetchSize | Defines the number of rows to fetch per batch. If not set or set to 0, the default is 1000. | |
cancel | Cancels the execution of the Statement. | |
com.aliyun.dataphin.jdbc.DataphinPrepareStatement
API | Description | API definition |
executeQuery | Executes an SQL query and returns a ResultSet. | |
com.aliyun.dataphin.jdbc.DataphinResultSetMetaData
API | Description | API definition |
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 | API definition |
next | Moves the cursor to the next row of the result set. | |
com.aliyun.dataphin.jdbc.DatabaseMetaData
API | Description | API definition |
getTables | Retrieves a description of the tables available in the given catalog.
| |
getColumns | Retrieves a description of the table's columns.
| |
Examples
You can retrieve catalog information by using the Dataphin jdbc driver.
1. Get table list
Gets 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: Displays the physical tables in the project.
For a development project and a data board, you must explicitly append _Dev.
If db_name is not specified, Dataphin lists tables under the project_name set in the URL.
Response
Name | Type | Description |
dim_user | Logical table | User table. |
ods_user | Physical table | User source table. |
ods_user_logical_view | Logical view | Logical view. |
ods_user_physical_view2 | Physical view | Physical view. |
2. Get table schema
Gets the column details of a physical table or physical view.
Syntax
{DESCRIBE | DESC} table_name;Only physical tables and physical views are supported.
Parameters
table_name: The name of the physical table or physical view.
Response
Name | Type | Description |
ID | BigInt | User ID. |
Name | String | User name. |
DS | String | The partition time. |
Server-side connection control
Maximum number of connections: The default is 100.
Connection timeout: The default is 288000s (2 hours). The system automatically disconnects a database connection that is idle for a specified period. If you try to perform an operation on this connection, the service reports an error or terminates unexpectedly. This corresponds to the
connect_timeoutparameter in the connection URL.
Dataphin JDBC task information in MaxCompute
When a Dataphin JDBC task is submitted to MaxCompute for execution, the system includes additional Dataphin-related information. The details are as follows:
Parameter | Description |
logical_project | The name of the Dataphin project where 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 where the JDBC task is executed. |
EXT_PLATFORM_ID | The ID of the platform that submitted the task to MaxCompute. The default is Dataphin. |
biz_id | The Dataphin member ID of the user who executed the JDBC task. |
odps.idata.userenv | User environment information. This includes the Java SDK version, Java version, IP address, and device MAC address. Example:
|
You can use this information to analyze MaxCompute bills and track job execution times. For more information, see Collect statistics on accounts of top N costs and time-consuming jobs of MaxCompute projects.