| |
| """pyspark_002 database initialization: create tables + load seed data""" |
| from pyspark.sql import SparkSession |
|
|
| spark = SparkSession.builder \ |
| .appName('dataclaw_eval_init_pyspark_002') \ |
| .enableHiveSupport() \ |
| .config('spark.sql.warehouse.dir', '/tmp/hive_warehouse') \ |
| .getOrCreate() |
|
|
| spark.sql('CREATE DATABASE IF NOT EXISTS internal_platform_db') |
|
|
| |
| spark.sql(''' |
| CREATE TABLE IF NOT EXISTS internal_platform_db.case5_ods_code_v2_note_diff_files_diff_v3 ( |
| `databus_imp_date` STRING COMMENT '业务日期,原为分区列', |
| `id` BIGINT COMMENT '-', |
| `diff` STRING COMMENT '-' |
| ) STORED AS ORC |
| ''') |
|
|
| |
| from pyspark.sql.types import StructType, StructField, StringType, LongType |
|
|
| schema = StructType([ |
| StructField("databus_imp_date", StringType(), True), |
| StructField("id", LongType(), True), |
| StructField("diff", StringType(), True), |
| ]) |
|
|
| print('Loading seed data...') |
| df = spark.read.csv( |
| "/tmp_workspace/seed_data/case5_ods_code_v2_note_diff_files_diff_v3.csv", |
| header=True, schema=schema, |
| ) |
| df.write.mode("overwrite").insertInto("internal_platform_db.case5_ods_code_v2_note_diff_files_diff_v3") |
|
|
| print('Database initialization complete') |
| spark.stop() |
|
|