{"name":"Michael"}{"name":"Andy", "age":30}{"name":"Justin", "age":3}{"name":"WangHua", "age":19}{"name":"ZhangSan", "age":10}{"name":"LiSi", "age":33}{"name":"ZhaoWu", "age":37}{"name":"MengXiao", "age":68}{"name":"KaiDa", "age":89}
<dependency><groupId>org.apache.spark</groupId><artifactId>spark-core_2.12</artifactId><version>3.2.1</version><scope>provided</scope></dependency><dependency><groupId>org.apache.spark</groupId><artifactId>spark-sql_2.12</artifactId><version>3.2.1</version><scope>provided</scope></dependency>
package com.tencent.dlc;import org.apache.spark.sql.Dataset;import org.apache.spark.sql.Row;import org.apache.spark.sql.SaveMode;import org.apache.spark.sql.SparkSession;public class CosService {public static void main( String[] args ){//1.创建SparkSessionSparkSession spark = SparkSession.builder().appName("Operate data on cos").config("spark.some.config.option", "some-value").getOrCreate();//2.读取cos上的json文件生成数据集,支持多种类型的文件,如 json,csv,parquet,orc,textString readPath = "cosn://dlc-demo-1305424723/people.json";Dataset<Row> readData = spark.read().json(readPath);//3.对数据集做业务计算操作生成结果数据,计算支持API和SQL形式,这里生成临时表用sql读数据readData.createOrReplaceTempView("people");Dataset<Row> result = spark.sql("SELECT * FROM people where age > 3");//4.结果数据保存到cosString writePath = "cosn://dlc-demo-1305424723/people_output";//写入支持多种类型的文件,如 json,csv,parquet,orc,textresult.write().mode(SaveMode.Append).json(writePath);spark.read().json(writePath).show();//5.关闭sessionspark.stop();}}
package com.tencent.dlc;import org.apache.spark.sql.SparkSession;public class DbService {public static void main(String[] args) {//1.初始化SparkSessionSparkSession spark = SparkSession.builder().appName("Operate DB Example").getOrCreate();//2.建数据库String dbName = " `DataLakeCatalog`.`dlc_db_test` ";String dbSql = "CREATE DATABASE IF NOT EXISTS" + dbName + " COMMENT 'demo test'";spark.sql(dbSql);//3.建内表String tableName = "`test`";String tableSql = "CREATE TABLE IF NOT EXISTS " + dbName + "." + tableName+ "(`id` int,`name` string, `age` int)";spark.sql(tableSql);//4.写数据spark.sql("INSERT INTO " + dbName + "." + tableName + "VALUES (1,'Andy',12),(2,'Justin',3) ");//5.查询数据spark.sql(" SELECT * FROM " + dbName + "." + tableName).show();//6.建外表String extTableName = "`ext_test`";spark.sql("CREATE EXTERNAL TABLE IF NOT EXISTS " + dbName + "." + extTableName + ""+ " (`id` int, `name` string, `age` int) "+ "ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe' "+ "STORED AS TEXTFILE LOCATION 'cosn://dlc-demo-1305424723/ext_test '");//7.写外表数据spark.sql("INSERT INTO " + dbName + "." + extTableName + "VALUES (1,'LiLy',12),(2,'Lucy',3) ");//8.查询外表数据spark.sql(" SELECT * FROM " + dbName + "." + extTableName).show();//9.关闭Sessionspark.stop();}}
配置参数 | 说明 |
作业名称 | 自定义 Spark Jar 作业名称,例如:cosn-demo |
作业类型 | 选择 批处理类型 |
数据引擎 | 选择 创建资源 步骤创建的 dlc-demo 计算引擎 |
程序包 | 选择 COS,在 上传 JAR 包到 COS 步骤上传的 JAR 包 demo-1.0-SNAPSHOT.jar |
主类(Main Class) | 根据程序代码填写,如: 从 COS 上读写数据填: com.tencent.dlc.CosService 在 DLC 上建库、建表等填:com.tencent.dlc.DbService |
数据访问策略 | 选择该步骤前创建的策略 qcs::cam::uin/100018379117:roleName/dlc-demo |
本页内容是否解决了您的问题?