forked from music-of-the-ainur/almaren-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request music-of-the-ainur#59 from badrinathpatchikolla/sp…
…ark-3.1 Added CSV Deserializer for Target File
- Loading branch information
Showing
27 changed files
with
273 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 87 additions & 25 deletions
112
src/main/scala/com/github/music/of/the/ainur/almaren/state/core/Deserializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,123 @@ | ||
package com.github.music.of.the.ainur.almaren.state.core | ||
|
||
import com.github.music.of.the.ainur.almaren.State | ||
import org.apache.spark.sql.DataFrame | ||
import com.github.music.of.the.ainur.almaren.{Almaren, SchemaRequired, State} | ||
import org.apache.spark.sql.{DataFrame, DataFrameReader, Dataset} | ||
import org.apache.spark.sql.types.{DataType, StructType} | ||
|
||
import scala.language.implicitConversions | ||
import com.github.music.of.the.ainur.almaren.Almaren | ||
import com.github.music.of.the.ainur.almaren.util.Constants | ||
import org.apache.spark.sql.Dataset | ||
import org.apache.spark.sql.functions.col | ||
|
||
import javax.xml.crypto.Data | ||
|
||
trait Deserializer extends State { | ||
|
||
def columnName: String | ||
def schema: Option[String] | ||
def options: Map[String, String] | ||
def autoFlatten: Boolean | ||
|
||
override def executor(df: DataFrame): DataFrame = { | ||
val newDf = deserializer(df) | ||
if(autoFlatten) | ||
autoFlatten(newDf,columnName) | ||
else | ||
newDf | ||
} | ||
|
||
abstract class Deserializer() extends State { | ||
override def executor(df: DataFrame): DataFrame = deserializer(df) | ||
def deserializer(df: DataFrame): DataFrame | ||
implicit def string2Schema(schema: String): DataType = | ||
|
||
implicit def string2Schema(schema: String): StructType = | ||
StructType.fromDDL(schema) | ||
|
||
def autoFlatten(df: DataFrame, columnName: String): DataFrame = | ||
df. | ||
select("*", columnName.concat(".*")). | ||
drop(columnName) | ||
|
||
def sampleData[T](df: Dataset[T]): Dataset[T] = { | ||
df.sample( | ||
options.getOrElse("samplingRatio","1.0").toDouble | ||
).limit( | ||
options.getOrElse("samplingMaxLines","10000").toInt | ||
) | ||
} | ||
|
||
def getReadWithOptions: DataFrameReader = | ||
Almaren.spark.getOrCreate().read.options(options) | ||
|
||
def getDDL(df:DataFrame): String = | ||
df.schema.toDDL | ||
} | ||
|
||
case class AvroDeserializer(columnName: String,schema: String) extends Deserializer { | ||
import org.apache.spark.sql.avro._ | ||
case class AvroDeserializer(columnName: String, schema: Option[String] = None, options: Map[String, String], autoFlatten: Boolean, mandatorySchema: String) extends Deserializer { | ||
|
||
import org.apache.spark.sql.avro.functions.from_avro | ||
import org.apache.spark.sql.functions._ | ||
import collection.JavaConversions._ | ||
|
||
schema.map(_ => throw SchemaRequired(s"AvroDeserializer, don't use 'schema' it must be None, use 'mandatorySchema' ")) | ||
|
||
override def deserializer(df: DataFrame): DataFrame = { | ||
logger.info(s"columnName:{$columnName}, schema:{$schema}") | ||
df.withColumn(columnName,from_avro(col(columnName),schema)) | ||
.select("*",columnName.concat(".*")).drop(columnName) | ||
logger.info(s"columnName:{$columnName}, schema:{$mandatorySchema}, options:{$options}, autoFlatten:{$autoFlatten}") | ||
df.withColumn(columnName, from_avro(col(columnName), mandatorySchema, options)) | ||
} | ||
} | ||
|
||
case class JsonDeserializer(columnName: String,schema: Option[String]) extends Deserializer { | ||
case class JsonDeserializer(columnName: String, schema: Option[String], options: Map[String, String], autoFlatten: Boolean) extends Deserializer { | ||
|
||
import org.apache.spark.sql.functions._ | ||
import collection.JavaConversions._ | ||
|
||
override def deserializer(df: DataFrame): DataFrame = { | ||
import df.sparkSession.implicits._ | ||
logger.info(s"columnName:{$columnName}, schema:{$schema}") | ||
logger.info(s"columnName:{$columnName}, schema:{$schema}, options:{$options}, autoFlatten:{$autoFlatten}") | ||
df.withColumn(columnName, | ||
from_json(col(columnName), | ||
schema.getOrElse(getSchemaDDL(df.selectExpr(columnName).as[(String)])))) | ||
.select("*",columnName.concat(".*")) | ||
.drop(columnName) | ||
from_json( | ||
col(columnName), | ||
schema.getOrElse(getSchemaDDL(df.selectExpr(columnName).as[(String)])), | ||
options | ||
)) | ||
} | ||
|
||
private def getSchemaDDL(df: Dataset[String]): String = | ||
Almaren.spark.getOrCreate().read.json(df.sample(Constants.sampleDeserializer)).schema.toDDL | ||
getDDL(getReadWithOptions.json(sampleData(df))) | ||
} | ||
|
||
case class XMLDeserializer(columnName: String, schema: Option[String]) extends Deserializer { | ||
case class XMLDeserializer(columnName: String, schema: Option[String], options: Map[String, String], autoFlatten: Boolean) extends Deserializer { | ||
|
||
import com.databricks.spark.xml.functions.from_xml | ||
import com.databricks.spark.xml.schema_of_xml | ||
import org.apache.spark.sql.functions._ | ||
|
||
override def deserializer(df: DataFrame): DataFrame = { | ||
logger.info(s"columnName:{$columnName}") | ||
logger.info(s"columnName:{$columnName}, schema:{$schema}, options:{$options}, autoFlatten:{$autoFlatten}") | ||
import df.sparkSession.implicits._ | ||
val xmlSchema = schema match { | ||
case Some(s) => StructType.fromDDL(s) | ||
case None => schema_of_xml(df.select(columnName).as[String]) | ||
case None => schema_of_xml(sampleData(df.select(columnName).as[String]), options = options) | ||
} | ||
df | ||
.withColumn(columnName, from_xml(col(columnName), xmlSchema)) | ||
.select("*",columnName.concat(".*")) | ||
.drop(columnName) | ||
.withColumn(columnName, from_xml(col(columnName), xmlSchema, options)) | ||
} | ||
} | ||
|
||
case class CSVDeserializer(columnName: String, schema: Option[String], options: Map[String, String], autoFlatten: Boolean) extends Deserializer { | ||
|
||
import org.apache.spark.sql.functions._ | ||
import collection.JavaConversions._ | ||
|
||
override def deserializer(df: DataFrame): DataFrame = { | ||
import df.sparkSession.implicits._ | ||
logger.info(s"columnName:{$columnName}, schema:{$schema}, options:{$options}, autoFlatten:{$autoFlatten}") | ||
df.withColumn(columnName, | ||
from_csv( | ||
col(columnName), | ||
schema.getOrElse(getSchemaDDL(df.selectExpr(columnName).as[(String)])), | ||
options | ||
)) | ||
} | ||
|
||
private def getSchemaDDL(df: Dataset[String]): String = | ||
getDDL(getReadWithOptions.csv(sampleData(df))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file added
BIN
+20 Bytes
...rializer.parquet/.part-00000-481960de-7c18-4a69-a9f9-1c122d6c7cf2-c000.snappy.parquet.crc
Binary file not shown.
Binary file added
BIN
+20 Bytes
...rializer.parquet/.part-00001-481960de-7c18-4a69-a9f9-1c122d6c7cf2-c000.snappy.parquet.crc
Binary file not shown.
Binary file added
BIN
+20 Bytes
...rializer.parquet/.part-00002-481960de-7c18-4a69-a9f9-1c122d6c7cf2-c000.snappy.parquet.crc
Binary file not shown.
Binary file added
BIN
+20 Bytes
...rializer.parquet/.part-00003-481960de-7c18-4a69-a9f9-1c122d6c7cf2-c000.snappy.parquet.crc
Binary file not shown.
Empty file.
Binary file added
BIN
+1.09 KB
...vDeserializer.parquet/part-00000-481960de-7c18-4a69-a9f9-1c122d6c7cf2-c000.snappy.parquet
Binary file not shown.
Binary file added
BIN
+1.11 KB
...vDeserializer.parquet/part-00001-481960de-7c18-4a69-a9f9-1c122d6c7cf2-c000.snappy.parquet
Binary file not shown.
Binary file added
BIN
+1.08 KB
...vDeserializer.parquet/part-00002-481960de-7c18-4a69-a9f9-1c122d6c7cf2-c000.snappy.parquet
Binary file not shown.
Binary file added
BIN
+1.12 KB
...vDeserializer.parquet/part-00003-481960de-7c18-4a69-a9f9-1c122d6c7cf2-c000.snappy.parquet
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+20 Bytes
...erSchema.parquet/.part-00000-83307a04-3932-47db-bdfa-c1d0e4dcfb49-c000.snappy.parquet.crc
Binary file not shown.
Binary file added
BIN
+20 Bytes
...erSchema.parquet/.part-00001-83307a04-3932-47db-bdfa-c1d0e4dcfb49-c000.snappy.parquet.crc
Binary file not shown.
Binary file added
BIN
+20 Bytes
...erSchema.parquet/.part-00002-83307a04-3932-47db-bdfa-c1d0e4dcfb49-c000.snappy.parquet.crc
Binary file not shown.
Binary file added
BIN
+20 Bytes
...erSchema.parquet/.part-00003-83307a04-3932-47db-bdfa-c1d0e4dcfb49-c000.snappy.parquet.crc
Binary file not shown.
Empty file.
Binary file added
BIN
+1.15 KB
...ializerSchema.parquet/part-00000-83307a04-3932-47db-bdfa-c1d0e4dcfb49-c000.snappy.parquet
Binary file not shown.
Binary file added
BIN
+1.17 KB
...ializerSchema.parquet/part-00001-83307a04-3932-47db-bdfa-c1d0e4dcfb49-c000.snappy.parquet
Binary file not shown.
Binary file added
BIN
+1.13 KB
...ializerSchema.parquet/part-00002-83307a04-3932-47db-bdfa-c1d0e4dcfb49-c000.snappy.parquet
Binary file not shown.
Binary file added
BIN
+1.17 KB
...ializerSchema.parquet/part-00003-83307a04-3932-47db-bdfa-c1d0e4dcfb49-c000.snappy.parquet
Binary file not shown.
Oops, something went wrong.