使用 spark-redshift 插入 Redshift
Inserts into Redshift using spark-redshift
我正在尝试从 S3(镶木地板文件)插入 Redshift 数据。通过 SQLWorkbench 完成 600 万行需要 46 秒。但是通过连接器 spark-redshift 完成它大约需要 7 分钟。
我正在尝试使用更多节点并获得相同的结果。
有什么改进使用 spark-redshift 的时间的建议吗?
Spark中的代码:
val df = spark.read.option("basePath", "s3a://parquet/items").parquet("s3a://parquet/items/Year=2017/Month=7/Day=15")
df.write
.format("com.databricks.spark.redshift")
.option("url", "jdbc:....")
.option("dbtable", "items")
.option("tempdir", "s3a://parquet/temp")
.option("aws_iam_role", "...")
.option("sortkeyspec", "SORTKEY(id)")
.mode(SaveMode.Append)
.save()
SQLWorkbench中的代码(Redshift SQL):
CREATE EXTERNAL TABLE items_schema.parquet_items("id type, column2 type....")
ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
STORED AS PARQUET
LOCATION 's3://parquet/items/Year=2017/Month=7/Day=15';
CREATE TABLE items ("id type, column2 type....");
INSERT INTO items (SELECT * FROM items_schema.parquet_items);
我会说你的片段标签错误:
- 这是 Spark 代码
val df = spark.read…
- 这是 Redshift SQL
CREATE EXTERNAL TABLE…
当您使用外部 table (Redshift Spectrum) 时,它会执行以下操作:
- 读取定义位置的parquet数据。
- 将数据插入正常的 Redshift table,如图所示。
当您使用 Spark 代码将数据写入 Redshift 时,使用 spark-redshift
,它会执行以下操作:
- Spark 将 parquet 文件从 S3 读取到 Spark 集群中。
- Spark 将 parquet 数据转换为 Avro 格式并将其写入 S3。
- Spark 向 Redshift 发出
COPY
SQL 查询以加载数据。
- Redshift 将 Avro 数据从 S3 加载到最终 table。
基本上 Spark 代码做了更多的工作,两次读取数据并以不同的格式写入两次。 Redshift Spectrum SQL 一次读取数据并将其写入 Redshift 本身(比通过网络发送到 S3 快得多)。
此外,尝试使用 CSV 而不是 Avro(默认)应该更快:
Redshift is significantly faster when loading CSV than when loading
Avro files, so using that tempformat may provide a large performance
boost when writing to Redshift.
https://docs.databricks.com/spark/latest/data-sources/aws/amazon-redshift.html
我正在尝试从 S3(镶木地板文件)插入 Redshift 数据。通过 SQLWorkbench 完成 600 万行需要 46 秒。但是通过连接器 spark-redshift 完成它大约需要 7 分钟。
我正在尝试使用更多节点并获得相同的结果。
有什么改进使用 spark-redshift 的时间的建议吗?
Spark中的代码:
val df = spark.read.option("basePath", "s3a://parquet/items").parquet("s3a://parquet/items/Year=2017/Month=7/Day=15")
df.write
.format("com.databricks.spark.redshift")
.option("url", "jdbc:....")
.option("dbtable", "items")
.option("tempdir", "s3a://parquet/temp")
.option("aws_iam_role", "...")
.option("sortkeyspec", "SORTKEY(id)")
.mode(SaveMode.Append)
.save()
SQLWorkbench中的代码(Redshift SQL):
CREATE EXTERNAL TABLE items_schema.parquet_items("id type, column2 type....")
ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
STORED AS PARQUET
LOCATION 's3://parquet/items/Year=2017/Month=7/Day=15';
CREATE TABLE items ("id type, column2 type....");
INSERT INTO items (SELECT * FROM items_schema.parquet_items);
我会说你的片段标签错误:
- 这是 Spark 代码
val df = spark.read…
- 这是 Redshift SQL
CREATE EXTERNAL TABLE…
当您使用外部 table (Redshift Spectrum) 时,它会执行以下操作:
- 读取定义位置的parquet数据。
- 将数据插入正常的 Redshift table,如图所示。
当您使用 Spark 代码将数据写入 Redshift 时,使用 spark-redshift
,它会执行以下操作:
- Spark 将 parquet 文件从 S3 读取到 Spark 集群中。
- Spark 将 parquet 数据转换为 Avro 格式并将其写入 S3。
- Spark 向 Redshift 发出
COPY
SQL 查询以加载数据。 - Redshift 将 Avro 数据从 S3 加载到最终 table。
基本上 Spark 代码做了更多的工作,两次读取数据并以不同的格式写入两次。 Redshift Spectrum SQL 一次读取数据并将其写入 Redshift 本身(比通过网络发送到 S3 快得多)。
此外,尝试使用 CSV 而不是 Avro(默认)应该更快:
Redshift is significantly faster when loading CSV than when loading Avro files, so using that tempformat may provide a large performance boost when writing to Redshift.
https://docs.databricks.com/spark/latest/data-sources/aws/amazon-redshift.html