如何执行列编码从PySpark加载数据到Redshift
问题描述:
我想加载数据是在实木复合地板格式S3直接使用pyspark红移。我能够做到这一点,但是当我在表格定义中看到列的编码时,它是一致的。我想特别让它一致,我希望它们都是lzo。以下是单个表中不一致的数据类型列表。如何执行列编码从PySpark加载数据到Redshift
+-------------------------------+-------------------+
| data_type | encoding |
+-------------------------------+-------------------+
| bigint | delta |
| bigint | delta32k |
| character varying(256) | lzo |
| bigint | runlength |
| bigint | bytedict |
| timestamp without time zone | bytedict |
| integer | runlength |
+-------------------------------+-------------------+
有人可以帮助我如何在pyspark中执行此操作。我没有看到列的编码中的任何选项com.databricks:火花redshift_2.10:1.0.0
x.write.format("com.databricks.spark.redshift")
.option("url","jdbc:redshift://<url>:<port>/<schema>?user=<user>&password=<pass>")
.option("dbtable","<tbl_nm>")
.option("diststyle","KEY").option("distkey","<key>")
.option("sortkeyspec","SORTKEY(<sort1>)")
.option("tempdir","<path>")
.mode("error").save()
答
我发现的相关位在PR 178指定列的编码。
所以你没有通过像.read.option('encoding', 'lzo')
这样的东西来指定编码。您需要创建一个带有元数据的模式对象,以在数据框创建时指定编码。在Python,例如:
%pyspark
from pyspark.sql.types import IntegerType, StringType, StructType, StructField
metadata = {'encoding':'LZO'}
schema = StructType([
StructField("id", IntegerType(), True, metadata),
StructField("name", StringType(), True, metadata)])
df = spark.createDataFrame([(1, 'Alice')], schema)
df.write \
.format("com.databricks.spark.redshift") \
.option("url", "jdbc:redshift://example.com:5439/db_foo?user=user_bar&password=pass_baz") \
.option("dbtable", "foo") \
.option("tempdir", "s3a://foo/bar") \
.mode("error") \
.save()
验证:
select "column", "encoding" from pg_table_def where tablename = 'foo';
column | encoding
--------+----------
id | lzo
name | lzo
(2 rows)