-
Notifications
You must be signed in to change notification settings - Fork 29k
SPARK-22833 [Improvement] in SparkHive Scala Examples #20018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
9d9b42b
0bbad8c
ee53208
2f98a3c
69a4145
b95587d
9b8d188
c3dda1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,8 +19,7 @@ package org.apache.spark.examples.sql.hive | |
| // $example on:spark_hive$ | ||
| import java.io.File | ||
|
|
||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.{Row, SaveMode, SparkSession} | ||
| // $example off:spark_hive$ | ||
|
|
||
| object SparkHiveExample { | ||
|
|
@@ -102,8 +101,41 @@ object SparkHiveExample { | |
| // | 4| val_4| 4| val_4| | ||
| // | 5| val_5| 5| val_5| | ||
| // ... | ||
| // $example off:spark_hive$ | ||
|
|
||
| // Create Hive managed table with parquet | ||
| sql("CREATE TABLE records(key int, value string) STORED AS PARQUET") | ||
| // Save DataFrame to Hive Managed table as Parquet format | ||
|
||
| val hiveTableDF = sql("SELECT * FROM records") | ||
| hiveTableDF.write.mode(SaveMode.Overwrite).saveAsTable("database_name.records") | ||
| // Create External Hive table with parquet | ||
| sql("CREATE EXTERNAL TABLE records(key int, value string) " + | ||
| "STORED AS PARQUET LOCATION '/user/hive/warehouse/'") | ||
| // to make Hive parquet format compatible with spark parquet format | ||
|
||
| spark.sqlContext.setConf("spark.sql.parquet.writeLegacyFormat", "true") | ||
|
|
||
| // Multiple parquet files could be created accordingly to volume of data under directory given. | ||
|
||
| val hiveExternalTableLocation = "/user/hive/warehouse/database_name.db/records" | ||
|
|
||
| // Save DataFrame to Hive External table as compatible parquet format | ||
|
||
| hiveTableDF.write.mode(SaveMode.Overwrite).parquet(hiveExternalTableLocation) | ||
|
|
||
| // turn on flag for Dynamic Partitioning | ||
|
||
| spark.sqlContext.setConf("hive.exec.dynamic.partition", "true") | ||
| spark.sqlContext.setConf("hive.exec.dynamic.partition.mode", "nonstrict") | ||
|
|
||
| // You can create partitions in Hive table, so downstream queries run much faster. | ||
| hiveTableDF.write.mode(SaveMode.Overwrite).partitionBy("key") | ||
| .parquet(hiveExternalTableLocation) | ||
|
|
||
| // reduce number of files for each partition by repartition | ||
|
||
| hiveTableDF.repartition($"key").write.mode(SaveMode.Overwrite) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a standard usage, let's not put it in the example.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan removed all comments , as discussed with @srowen it does really make sense to have at docs with removed inconsitency. |
||
| .partitionBy("key").parquet(hiveExternalTableLocation) | ||
|
|
||
| // Control number of files in each partition by coalesce | ||
|
||
| hiveTableDF.coalesce(10).write.mode(SaveMode.Overwrite) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| .partitionBy("key").parquet(hiveExternalTableLocation) | ||
| // $example off:spark_hive$ | ||
|
|
||
| spark.stop() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parquet->ParquetThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HyukjinKwon Thanks for highlight, improved the same.