Apache Spark export PostgreSQL data in Parquet format
up vote
0
down vote
favorite
I have PostgreSQL database with ~1000 different tables. I'd like to export all of these tables and data inside them into Parquet files.
In order to do it, I'm going to read each table into DataFrame and then store this df into Parquet file. Many of the PostgreSQL tables contains user-defined Types.
The biggest issue is - that I can't manually specify the schema for DataFrame. Will Apache Spark, in this case, be able to automatically infer the PostgreSQL table schemas and store them appropriately into Parquet format or this is impossible with Apache Spark and some other technology must be used for this purpose?
UPDATED
I have created the following PostgreSQL user-defined type, table and records:
create type dimensions as (
width integer,
height integer,
depth integer
);
create table moving_boxes (
id serial primary key,
dims dimensions not null
);
insert into moving_boxes (dims) values (row(3,4,5)::dimensions);
insert into moving_boxes (dims) values (row(1,4,2)::dimensions);
insert into moving_boxes (dims) values (row(10,12,777)::dimensions);
Implemented the following Spark application:
// that gives an one-partition Dataset
val opts = Map(
"url" -> "jdbc:postgresql:sparktest",
"dbtable" -> "moving_boxes",
"user" -> "user",
"password" -> "password")
val df = spark.
read.
format("jdbc").
options(opts).
load
println(df.printSchema())
df.write.mode(SaveMode.Overwrite).format("parquet").save("moving_boxes.parquet")
This is df.printSchema
output:
root
|-- id: integer (nullable = true)
|-- dims: string (nullable = true)
As you may see, Spark DataFrame infer dims schema as string
and not as a complex nested type.
This is the log information from ParquetWriteSupport
:
18/11/06 10:08:52 INFO ParquetWriteSupport: Initialized Parquet WriteSupport with Catalyst schema:
{
"type" : "struct",
"fields" : [ {
"name" : "id",
"type" : "integer",
"nullable" : true,
"metadata" : { }
}, {
"name" : "dims",
"type" : "string",
"nullable" : true,
"metadata" : { }
} ]
}
and corresponding Parquet message type:
message spark_schema {
optional int32 id;
optional binary dims (UTF8);
}
Could you please explain, will the original complex dims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?
postgresql apache-spark parquet
add a comment |
up vote
0
down vote
favorite
I have PostgreSQL database with ~1000 different tables. I'd like to export all of these tables and data inside them into Parquet files.
In order to do it, I'm going to read each table into DataFrame and then store this df into Parquet file. Many of the PostgreSQL tables contains user-defined Types.
The biggest issue is - that I can't manually specify the schema for DataFrame. Will Apache Spark, in this case, be able to automatically infer the PostgreSQL table schemas and store them appropriately into Parquet format or this is impossible with Apache Spark and some other technology must be used for this purpose?
UPDATED
I have created the following PostgreSQL user-defined type, table and records:
create type dimensions as (
width integer,
height integer,
depth integer
);
create table moving_boxes (
id serial primary key,
dims dimensions not null
);
insert into moving_boxes (dims) values (row(3,4,5)::dimensions);
insert into moving_boxes (dims) values (row(1,4,2)::dimensions);
insert into moving_boxes (dims) values (row(10,12,777)::dimensions);
Implemented the following Spark application:
// that gives an one-partition Dataset
val opts = Map(
"url" -> "jdbc:postgresql:sparktest",
"dbtable" -> "moving_boxes",
"user" -> "user",
"password" -> "password")
val df = spark.
read.
format("jdbc").
options(opts).
load
println(df.printSchema())
df.write.mode(SaveMode.Overwrite).format("parquet").save("moving_boxes.parquet")
This is df.printSchema
output:
root
|-- id: integer (nullable = true)
|-- dims: string (nullable = true)
As you may see, Spark DataFrame infer dims schema as string
and not as a complex nested type.
This is the log information from ParquetWriteSupport
:
18/11/06 10:08:52 INFO ParquetWriteSupport: Initialized Parquet WriteSupport with Catalyst schema:
{
"type" : "struct",
"fields" : [ {
"name" : "id",
"type" : "integer",
"nullable" : true,
"metadata" : { }
}, {
"name" : "dims",
"type" : "string",
"nullable" : true,
"metadata" : { }
} ]
}
and corresponding Parquet message type:
message spark_schema {
optional int32 id;
optional binary dims (UTF8);
}
Could you please explain, will the original complex dims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?
postgresql apache-spark parquet
Yes Spark will be able to infer schema and save it as a parquet file. Have you tried to do it yet?
– Joe Widen
Nov 6 at 3:12
@JoeWiden thanks for your answer. I have updated the question with more detailed information. Could you please explain, will the original complexdims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?
– alexanoid
Nov 6 at 8:22
I dont believe spark will be able to infer complex types via jdbc connection.
– Joe Widen
Nov 9 at 14:53
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have PostgreSQL database with ~1000 different tables. I'd like to export all of these tables and data inside them into Parquet files.
In order to do it, I'm going to read each table into DataFrame and then store this df into Parquet file. Many of the PostgreSQL tables contains user-defined Types.
The biggest issue is - that I can't manually specify the schema for DataFrame. Will Apache Spark, in this case, be able to automatically infer the PostgreSQL table schemas and store them appropriately into Parquet format or this is impossible with Apache Spark and some other technology must be used for this purpose?
UPDATED
I have created the following PostgreSQL user-defined type, table and records:
create type dimensions as (
width integer,
height integer,
depth integer
);
create table moving_boxes (
id serial primary key,
dims dimensions not null
);
insert into moving_boxes (dims) values (row(3,4,5)::dimensions);
insert into moving_boxes (dims) values (row(1,4,2)::dimensions);
insert into moving_boxes (dims) values (row(10,12,777)::dimensions);
Implemented the following Spark application:
// that gives an one-partition Dataset
val opts = Map(
"url" -> "jdbc:postgresql:sparktest",
"dbtable" -> "moving_boxes",
"user" -> "user",
"password" -> "password")
val df = spark.
read.
format("jdbc").
options(opts).
load
println(df.printSchema())
df.write.mode(SaveMode.Overwrite).format("parquet").save("moving_boxes.parquet")
This is df.printSchema
output:
root
|-- id: integer (nullable = true)
|-- dims: string (nullable = true)
As you may see, Spark DataFrame infer dims schema as string
and not as a complex nested type.
This is the log information from ParquetWriteSupport
:
18/11/06 10:08:52 INFO ParquetWriteSupport: Initialized Parquet WriteSupport with Catalyst schema:
{
"type" : "struct",
"fields" : [ {
"name" : "id",
"type" : "integer",
"nullable" : true,
"metadata" : { }
}, {
"name" : "dims",
"type" : "string",
"nullable" : true,
"metadata" : { }
} ]
}
and corresponding Parquet message type:
message spark_schema {
optional int32 id;
optional binary dims (UTF8);
}
Could you please explain, will the original complex dims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?
postgresql apache-spark parquet
I have PostgreSQL database with ~1000 different tables. I'd like to export all of these tables and data inside them into Parquet files.
In order to do it, I'm going to read each table into DataFrame and then store this df into Parquet file. Many of the PostgreSQL tables contains user-defined Types.
The biggest issue is - that I can't manually specify the schema for DataFrame. Will Apache Spark, in this case, be able to automatically infer the PostgreSQL table schemas and store them appropriately into Parquet format or this is impossible with Apache Spark and some other technology must be used for this purpose?
UPDATED
I have created the following PostgreSQL user-defined type, table and records:
create type dimensions as (
width integer,
height integer,
depth integer
);
create table moving_boxes (
id serial primary key,
dims dimensions not null
);
insert into moving_boxes (dims) values (row(3,4,5)::dimensions);
insert into moving_boxes (dims) values (row(1,4,2)::dimensions);
insert into moving_boxes (dims) values (row(10,12,777)::dimensions);
Implemented the following Spark application:
// that gives an one-partition Dataset
val opts = Map(
"url" -> "jdbc:postgresql:sparktest",
"dbtable" -> "moving_boxes",
"user" -> "user",
"password" -> "password")
val df = spark.
read.
format("jdbc").
options(opts).
load
println(df.printSchema())
df.write.mode(SaveMode.Overwrite).format("parquet").save("moving_boxes.parquet")
This is df.printSchema
output:
root
|-- id: integer (nullable = true)
|-- dims: string (nullable = true)
As you may see, Spark DataFrame infer dims schema as string
and not as a complex nested type.
This is the log information from ParquetWriteSupport
:
18/11/06 10:08:52 INFO ParquetWriteSupport: Initialized Parquet WriteSupport with Catalyst schema:
{
"type" : "struct",
"fields" : [ {
"name" : "id",
"type" : "integer",
"nullable" : true,
"metadata" : { }
}, {
"name" : "dims",
"type" : "string",
"nullable" : true,
"metadata" : { }
} ]
}
and corresponding Parquet message type:
message spark_schema {
optional int32 id;
optional binary dims (UTF8);
}
Could you please explain, will the original complex dims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?
postgresql apache-spark parquet
postgresql apache-spark parquet
edited Nov 6 at 8:28
asked Nov 5 at 19:38
alexanoid
6,7991074164
6,7991074164
Yes Spark will be able to infer schema and save it as a parquet file. Have you tried to do it yet?
– Joe Widen
Nov 6 at 3:12
@JoeWiden thanks for your answer. I have updated the question with more detailed information. Could you please explain, will the original complexdims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?
– alexanoid
Nov 6 at 8:22
I dont believe spark will be able to infer complex types via jdbc connection.
– Joe Widen
Nov 9 at 14:53
add a comment |
Yes Spark will be able to infer schema and save it as a parquet file. Have you tried to do it yet?
– Joe Widen
Nov 6 at 3:12
@JoeWiden thanks for your answer. I have updated the question with more detailed information. Could you please explain, will the original complexdims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?
– alexanoid
Nov 6 at 8:22
I dont believe spark will be able to infer complex types via jdbc connection.
– Joe Widen
Nov 9 at 14:53
Yes Spark will be able to infer schema and save it as a parquet file. Have you tried to do it yet?
– Joe Widen
Nov 6 at 3:12
Yes Spark will be able to infer schema and save it as a parquet file. Have you tried to do it yet?
– Joe Widen
Nov 6 at 3:12
@JoeWiden thanks for your answer. I have updated the question with more detailed information. Could you please explain, will the original complex
dims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?– alexanoid
Nov 6 at 8:22
@JoeWiden thanks for your answer. I have updated the question with more detailed information. Could you please explain, will the original complex
dims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?– alexanoid
Nov 6 at 8:22
I dont believe spark will be able to infer complex types via jdbc connection.
– Joe Widen
Nov 9 at 14:53
I dont believe spark will be able to infer complex types via jdbc connection.
– Joe Widen
Nov 9 at 14:53
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53161094%2fapache-spark-export-postgresql-data-in-parquet-format%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Yes Spark will be able to infer schema and save it as a parquet file. Have you tried to do it yet?
– Joe Widen
Nov 6 at 3:12
@JoeWiden thanks for your answer. I have updated the question with more detailed information. Could you please explain, will the original complex
dims
type(defined in PostgreSQL) be lost in the saved Parquet file or no?– alexanoid
Nov 6 at 8:22
I dont believe spark will be able to infer complex types via jdbc connection.
– Joe Widen
Nov 9 at 14:53