| |
| """pyspark_001 database initialization: create tables + load seed data""" |
| from pyspark.sql import SparkSession |
|
|
| spark = SparkSession.builder \ |
| .appName('dataclaw_eval_init_pyspark_001') \ |
| .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.caseR1_dwd_ww_kf_session_satify_attrib_v3 ( |
| `reason` STRING, `reason_detail` STRING, `session_cnt` BIGINT, |
| `satify_score` DOUBLE, `session_rate` DOUBLE, `all_session_cnt` BIGINT, |
| `all_session_satify_score` DOUBLE, `satify_attrib` DOUBLE, |
| `session_type` BIGINT, `total_session_cnt` BIGINT, |
| `total_all_session_cnt` BIGINT, `total_session_rate` DOUBLE, |
| `imp_date` STRING |
| ) STORED AS ORC |
| ''') |
|
|
| |
| from pyspark.sql.types import StructType, StructField, StringType, LongType, DoubleType |
|
|
| schema = StructType([ |
| StructField("reason", StringType(), True), |
| StructField("reason_detail", StringType(), True), |
| StructField("session_cnt", LongType(), True), |
| StructField("satify_score", DoubleType(), True), |
| StructField("session_rate", DoubleType(), True), |
| StructField("all_session_cnt", LongType(), True), |
| StructField("all_session_satify_score", DoubleType(), True), |
| StructField("satify_attrib", DoubleType(), True), |
| StructField("session_type", LongType(), True), |
| StructField("total_session_cnt", LongType(), True), |
| StructField("total_all_session_cnt", LongType(), True), |
| StructField("total_session_rate", DoubleType(), True), |
| StructField("imp_date", StringType(), True), |
| ]) |
|
|
| print('Loading seed data...') |
| df = spark.read.csv( |
| "/tmp_workspace/seed_data/caseR1_dwd_ww_kf_session_satify_attrib_v3.csv", |
| header=True, schema=schema, |
| ) |
| df.write.mode("overwrite").insertInto("internal_platform_db.caseR1_dwd_ww_kf_session_satify_attrib_v3") |
|
|
| print('Database initialization complete') |
| spark.stop() |
|
|