In Linux or Windows, you can connect to the DLI server using JDBC.
DLI Data Type |
JDBC Type |
Java Type |
---|---|---|
INT |
INTEGER |
java.lang.Integer |
STRING |
VARCHAR |
java.lang.String |
FLOAT |
FLOAT |
java.lang.Float |
DOUBLE |
DOUBLE |
java.lang.Double |
DECIMAL |
DECIMAL |
java.math.BigDecimal |
BOOLEAN |
BOOLEAN |
java.lang.Boolean |
SMALLINT/SHORT |
SMALLINT |
java.lang.Short |
TINYINT |
TINYINT |
java.lang.Short |
BIGINT/LONG |
BIGINT |
java.lang.Long |
TIMESTAMP |
TIMESTAMP |
java.sql.Timestamp |
CHAR |
CHAR |
Java.lang.Character |
VARCHAR |
VARCHAR |
java.lang.String |
DATE |
DATE |
java.sql.Date |
Before using JDBC, perform the following operations:
DLI uses the Identity and Access Management (IAM) to implement fine-grained permissions for your enterprise-level tenants. IAM provides identity authentication, permissions management, and access control, helping you securely access your resources.
With IAM, you can use your account to create IAM users for your employees, and assign permissions to the users to control their access to specific resource types.
Currently, roles (coarse-grained authorization) and policies (fine-grained authorization) are supported.
If the user who creates the queue is not an administrator, the queue can be used only after being authorized by the administrator. For details about how to assign permissions, see .
Class.forName("com.dli.jdbc.DliDriver");
Connection conn = DriverManager.getConnection(String url, Properties info);
Parameter |
Description |
---|---|
url |
The URL format is as follows: jdbc:dli://<endPoint>/projectId? <key1>=<val1>;<key2>=<val2>...
|
Info |
The Info object passes user-defined configuration items. If Info does not pass any attribute item, you can set it to null. The format is as follows: info.setProperty ("Attribute item", "Attribute value"). |
Item |
Mandatory |
Default Value |
Description |
---|---|---|---|
queuename |
Yes |
- |
Queue name of DLI. |
databasename |
No |
- |
Name of a database. |
authenticationmode |
No |
token |
Authentication mode. Currently, token- and AK/SK-based authentication modes are supported. |
accesskey |
Yes |
- |
AK/SK authentication key. For details about how to obtain the key, see Performing Authentication. |
secretkey |
Yes |
- |
AK/SK authentication key. For details about how to obtain the key, see Performing Authentication. |
servicename |
This parameter must be configured if authenticationmode is set to aksk. |
- |
Indicates the service name, that is, dli. |
token |
This parameter must be configured if authenticationmode is set to token. |
- |
Token authentication. For details about the authentication mode, see Performing Authentication. |
charset |
No |
UTF-8 |
JDBC encoding mode. |
usehttpproxy |
No |
false |
Whether to use the access proxy. |
proxyhost |
This parameter must be configured if usehttpproxy is set to true. |
- |
Access proxy host. |
proxyport |
This parameter must be configured if usehttpproxy is set to true. |
- |
Access proxy port. |
dli.sql.checkNoResultQuery |
No |
false |
Whether to allow invoking the executeQuery API to execute statements (for example, DDL) that do not return results.
|
jobtimeout |
No |
300 |
End time of the job submission. Unit: second |
iam.endpoint |
No. By default, the value is automatically combined based on regionName. |
- |
- |
obs.endpoint |
No. By default, the value is automatically combined based on regionName. |
- |
- |
directfetchthreshold |
No |
1000 |
Check whether the number of returned results exceeds the threshold based on service requirements. The default threshold is 1000. |
Statement statement = conn.createStatement();
statement.execute("SET dli.sql.spark.sql.forcePartitionPredicatesOnPartitionedTable.enabled=true");
statement.execute("select * from tb1");
ResultSet rs = statement.getResultSet();
while (rs.next()) { int a = rs.getInt(1); int b = rs.getInt(2); }
conn.close();
import java.sql.*; import java.util.Properties; public class DLIJdbcDriverExample { public static void main(String[] args) throws ClassNotFoundException, SQLException { Connection conn = null; try { Class.forName("com.dli.jdbc.DliDriver"); String url = "jdbc:dli://<endpoint>/<projectId>?databasename=db1;queuename=testqueue"; Properties info = new Properties(); info.setProperty("authenticationmode", "aksk"); info.setProperty("regionname", "<real region name>"); info.setProperty("accesskey", "<System.getenv("AK")>"); info.setProperty("secretkey", "<System.getenv("SK")>") conn = DriverManager.getConnection(url, info); Statement statement = conn.createStatement(); statement.execute("select * from tb1"); ResultSet rs = statement.getResultSet(); int line = 0; while (rs.next()) { line ++; int a = rs.getInt(1); int b = rs.getInt(2); System.out.println("Line:" + line + ":" + a + "," + b); } statement.execute("SET dli.sql.spark.sql.forcePartitionPredicatesOnPartitionedTable.enabled=true"); statement.execute("describe tb1"); ResultSet rs1 = statement.getResultSet(); line = 0; while (rs1.next()) { line ++; String a = rs1.getString(1); String b = rs1.getString(2); System.out.println("Line:" + line + ":" + a + "," + b); } } catch (SQLException ex) { } finally { if (conn != null) { conn.close(); } } } }
If the JDBC requery function is enabled, the system automatically requeries when the query operation fails.
To enable the requery function, add the attributes listed in Table 4 to the Info parameter.
Item |
Mandatory |
Default Value |
Description |
---|---|---|---|
USE_RETRY_KEY |
Yes |
false |
Whether to enable the requery function. If this parameter is set to True, the requery function is enabled. |
RETRY_TIMES_KEY |
Yes |
3000 |
Requery interval (milliseconds). Set this parameter to 30000 ms. |
RETRY_INTERVALS_KEY |
Yes |
3 |
Requery times. Set this parameter to a value from 3 to 5. |
Set JDBC parameters, enable the requery function, and create a link. The following is an example:
import com.xxx.dli.jdbs.utils.ConnectionResource;// Introduce "ConnectionResource". Change the class name as needed. import java.sql.*; import java.util.Properties; public class DLIJdbcDriverExample { private static final String X_AUTH_TOKEN_VALUE = "<realtoken>"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Connection conn = null; try { Class.forName("com.dli.jdbc.DliDriver"); String url = "jdbc:dli://<endpoint>/<projectId>?databasename=db1;queuename=testqueue"; Properties info = new Properties(); info.setProperty("token", X_AUTH_TOKEN_VALUE); info.setProperty(ConnectionResource.USE_RETRY_KEY, "true"); // Enable the requery function. info.setProperty(ConnectionResource.RETRY_TIMES_KEY, "30000");// Requery interval (ms) info.setProperty(ConnectionResource.RETRY_INTERVALS_KEY, "5");// Requery Times conn = DriverManager.getConnection(url, info); Statement statement = conn.createStatement(); statement.execute("select * from tb1"); ResultSet rs = statement.getResultSet(); int line = 0; while (rs.next()) { line ++; int a = rs.getInt(1); int b = rs.getInt(2); System.out.println("Line:" + line + ":" + a + "," + b); } statement.execute("describe tb1"); ResultSet rs1 = statement.getResultSet(); line = 0; while (rs1.next()) { line ++; String a = rs1.getString(1); String b = rs1.getString(2); System.out.println("Line:" + line + ":" + a + "," + b); } } catch (SQLException ex) { } finally { if (conn != null) { conn.close(); } } } }