数据湖探索 DLI-java样例代码:完整示例代码

时间:2024-06-20 11:23:10

完整示例代码

通过SQL API访问

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.apache.spark.sql.SparkSession;
 
public class java_rds {
 
    public static void main(String[] args) {
        SparkSession sparkSession = SparkSession.builder().appName("datasource-rds").getOrCreate();
 
        // Create a data table for  DLI -associated RDS
        sparkSession.sql("CREATE TABLE IF NOT EXISTS dli_to_rds USING JDBC OPTIONS ('url'='jdbc:mysql://192.168.6.150:3306','dbtable'='test.customer','user'='root','password'='**','driver'='com.mysql.jdbc.Driver')");
 
        //*****************************SQL model***********************************
        //Insert data into the DLI data table
        sparkSession.sql("insert into dli_to_rds values(3,'Liu',21),(4,'Joey',34)");
 
        //Read data from DLI data table
        sparkSession.sql("select * from dli_to_rds");
 
        //drop table
        sparkSession.sql("drop table dli_to_rds");
 
        sparkSession.close();
    }
}
support.huaweicloud.com/devg-dli/dli_09_0187.html