Skip to content
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

[REST-155] Refactoring type class hierarchy #7

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ lazy val `data-scalaxy-reader` = (project in file("."))

lazy val `reader-text` = (project in file("text"))
.settings(
version := "1.1.0",
version := "2.0.0",
libraryDependencies ++= textDependencies
)
183 changes: 100 additions & 83 deletions text/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.clairvoyant.data.scalaxy.reader.text

import com.clairvoyant.data.scalaxy.reader.text.formats.TextFormat
import com.clairvoyant.data.scalaxy.reader.text.instances.TextFormatToDataFrameReader
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{DataFrame, SparkSession}

object TextToDataFrameReader {
trait TextToDataFrameReader[T]:

def read[T <: TextFormat](
def read(
text: Seq[String],
textFormat: T,
originalSchema: Option[StructType],
adaptSchemaColumns: StructType => StructType
)(using sparkSession: SparkSession): DataFrame

def read(
text: String,
textFormat: T,
originalSchema: Option[StructType] = None,
adaptSchemaColumns: StructType => StructType = identity
)(using textFormatToDataFrameReader: TextFormatToDataFrameReader[T], sparkSession: SparkSession): DataFrame =
read(Seq(text), textFormat, originalSchema, adaptSchemaColumns)
)(using sparkSession: SparkSession): DataFrame = read(Seq(text), textFormat, originalSchema, adaptSchemaColumns)

def read[T <: TextFormat](
text: Seq[String],
textFormat: T,
originalSchema: Option[StructType],
adaptSchemaColumns: StructType => StructType
)(using textFormatToDataFrameReader: TextFormatToDataFrameReader[T], sparkSession: SparkSession): DataFrame =
textFormatToDataFrameReader.read(text, textFormat, originalSchema, adaptSchemaColumns)
object TextToDataFrameReader:

}
def apply[T <: TextFormat](using textToDataFrameReader: TextToDataFrameReader[T]): TextToDataFrameReader[T] =
textToDataFrameReader
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.clairvoyant.data.scalaxy.reader.text.instances

import com.clairvoyant.data.scalaxy.reader.text.TextToDataFrameReader
import com.clairvoyant.data.scalaxy.reader.text.formats.CSVTextFormat
import org.apache.spark.sql.catalyst.csv.CSVOptions.*
import org.apache.spark.sql.types.{StringType, StructType}
import org.apache.spark.sql.{DataFrame, SparkSession}

implicit object CSVTextToDataFrameReader extends TextFormatToDataFrameReader[CSVTextFormat] {
implicit object CSVTextToDataFrameReader extends TextToDataFrameReader[CSVTextFormat] {

override def read(
text: Seq[String],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import org.jsoup.select.Elements
import scala.collection.convert.ImplicitConversions.`collection AsScalaIterable`
import scala.util.Try

implicit object HTMLTableTextToDataFrameReader extends TextFormatToDataFrameReader[HTMLTableTextFormat] {
implicit object HTMLTableTextToDataFrameReader extends TextToDataFrameReader[HTMLTableTextFormat] {

val VALUE_SEPARATOR = "~"
private val VALUE_SEPARATOR = "~"

override def read(
text: Seq[String],
textFormat: HTMLTableTextFormat,
originalSchema: Option[StructType],
adaptSchemaColumns: StructType => StructType
)(using sparkSession: SparkSession): DataFrame =
TextToDataFrameReader.read(
TextToDataFrameReader[CSVTextFormat].read(
text = text.map(htmlText => convertHTMLTableToCSV(htmlText, textFormat.tableName)),
textFormat = CSVTextFormat(
sep = VALUE_SEPARATOR
Expand All @@ -30,7 +30,7 @@ implicit object HTMLTableTextToDataFrameReader extends TextFormatToDataFrameRead
adaptSchemaColumns = adaptSchemaColumns
)

def convertHTMLTableToCSV(htmlText: String, tableName: Option[String] = None): String = {
private def convertHTMLTableToCSV(htmlText: String, tableName: Option[String] = None): String = {
Try {
val parsedDocument = Jsoup.parse(htmlText)

Expand All @@ -47,7 +47,7 @@ implicit object HTMLTableTextToDataFrameReader extends TextFormatToDataFrameRead
}.get
}

def getTableFromParsedDocument(parsedDocument: Document, tableName: Option[String]): Element =
private def getTableFromParsedDocument(parsedDocument: Document, tableName: Option[String]): Element =
tableName
.map { tblName =>
val tables = parsedDocument.select(tblName)
Expand All @@ -58,12 +58,12 @@ implicit object HTMLTableTextToDataFrameReader extends TextFormatToDataFrameRead
}
.getOrElse(parsedDocument.getElementsByTag("table").first())

def getTableHeader(rows: Elements): String =
private def getTableHeader(rows: Elements): String =
Seq(rows.select("th").map(_.text)).flatten
.mkString(VALUE_SEPARATOR)
.concat("\n")

def getTableRows(rows: Elements): String =
private def getTableRows(rows: Elements): String =
Seq(
rows.map { row =>
val flattenRows = Seq(row.select("td").map(_.text())).flatten
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.clairvoyant.data.scalaxy.reader.text.instances

import com.clairvoyant.data.scalaxy.reader.text.TextToDataFrameReader
import com.clairvoyant.data.scalaxy.reader.text.formats.JSONTextFormat
import org.apache.spark.sql.catalyst.json.JSONOptions.*
import org.apache.spark.sql.functions.{col, explode}
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{DataFrame, SparkSession}

implicit object JSONTextToDataFrameReader extends TextFormatToDataFrameReader[JSONTextFormat] {
implicit object JSONTextToDataFrameReader extends TextToDataFrameReader[JSONTextFormat] {

override def read(
text: Seq[String],
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.clairvoyant.data.scalaxy.reader.text.instances

import com.clairvoyant.data.scalaxy.reader.text.TextToDataFrameReader
import com.clairvoyant.data.scalaxy.reader.text.formats.XMLTextFormat
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{DataFrame, SparkSession}

import java.io.{File, PrintWriter}
import java.util.concurrent.Semaphore

implicit object XMLTextToDataFrameReader extends TextFormatToDataFrameReader[XMLTextFormat] {
implicit object XMLTextToDataFrameReader extends TextToDataFrameReader[XMLTextFormat] {

override def read(
text: Seq[String],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.clairvoyant.data.scalaxy.reader.text

import com.clairvoyant.data.scalaxy.reader.text.TextToDataFrameReader
import com.clairvoyant.data.scalaxy.reader.text.formats.CSVTextFormat
import com.clairvoyant.data.scalaxy.reader.text.instances.CSVTextToDataFrameReader
import com.clairvoyant.data.scalaxy.test.util.SparkUtil
Expand All @@ -9,6 +8,8 @@ import org.apache.spark.sql.types.*

class CSVTextToDataFrameReaderSpec extends SparkUtil {

val csvTextToDataFrameReader = TextToDataFrameReader[CSVTextFormat]

"read() - with csv text and headers" should "return a dataframe with correct count and schema" in {
val csvText =
"""|col1,col2
Expand All @@ -18,7 +19,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {

val csvTextFormat = CSVTextFormat()

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -38,7 +39,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
inferSchema = false
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -57,7 +58,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
header = false
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -77,7 +78,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
sep = ";"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -94,7 +95,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
recordSep = "#"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -112,7 +113,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
recordSep = "#"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -132,7 +133,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
timestampFormat = "dd-MM-yyyy HH:mm:ss"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -153,7 +154,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
locale = "fr-FR"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -175,7 +176,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
mode = "PERMISSIVE"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand Down Expand Up @@ -208,7 +209,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
escape = "#"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -235,7 +236,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
nullValue = "Invalid"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand Down Expand Up @@ -263,7 +264,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
quote = "~"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -285,7 +286,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
enforceSchema = true
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat,
originalSchema = Some(
Expand Down Expand Up @@ -315,7 +316,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
ignoreLeadingWhiteSpace = true
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand Down Expand Up @@ -343,7 +344,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
ignoreTrailingWhiteSpace = true
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand Down Expand Up @@ -372,7 +373,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
nanValue = "NA"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -392,7 +393,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
positiveInf = "PositiveInfiniteValue"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -417,7 +418,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
negativeInf = "NegativeInfiniteValue"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -444,7 +445,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {
comment = "*"
)

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat
)
Expand All @@ -462,7 +463,7 @@ class CSVTextToDataFrameReaderSpec extends SparkUtil {

val csvTextFormat = CSVTextFormat()

val df = TextToDataFrameReader.read(
val df = csvTextToDataFrameReader.read(
text = csvText,
textFormat = csvTextFormat,
adaptSchemaColumns =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.clairvoyant.data.scalaxy.reader.text

import com.clairvoyant.data.scalaxy.reader.text.TextToDataFrameReader
import com.clairvoyant.data.scalaxy.reader.text.formats.{CSVTextFormat, HTMLTableTextFormat}
import com.clairvoyant.data.scalaxy.reader.text.formats.HTMLTableTextFormat
import com.clairvoyant.data.scalaxy.reader.text.instances.HTMLTableTextToDataFrameReader
import com.clairvoyant.data.scalaxy.test.util.SparkUtil
import com.clairvoyant.data.scalaxy.test.util.matchers.DataFrameMatcher
import com.clairvoyant.data.scalaxy.test.util.readers.DataFrameReader
import org.apache.spark.sql.functions.col
import org.apache.spark.sql.types.*

class HTMLTableTextToDataFrameReaderSpec extends DataFrameReader with DataFrameMatcher {

val htmlTableTextToDataFrameReader = TextToDataFrameReader[HTMLTableTextFormat]

"read() - with html text" should "return a dataframe with correct count and schema" in {
val htmlText =
"""
Expand Down Expand Up @@ -98,7 +96,7 @@ class HTMLTableTextToDataFrameReaderSpec extends DataFrameReader with DataFrameM

val htmlTableTextFormat = HTMLTableTextFormat()

val actualDF = TextToDataFrameReader.read(
val actualDF = htmlTableTextToDataFrameReader.read(
text = htmlText,
textFormat = htmlTableTextFormat
)
Expand Down
Loading