17
17
package za .co .absa .cobrix .spark .cobol .source
18
18
19
19
import org .apache .hadoop .fs .Path
20
- import org .apache .spark .sql .sources .{ BaseRelation , DataSourceRegister , RelationProvider , SchemaRelationProvider }
20
+ import org .apache .spark .sql .sources ._
21
21
import org .apache .spark .sql .types .StructType
22
- import org .apache .spark .sql .{SQLContext , SparkSession }
22
+ import org .apache .spark .sql .{DataFrame , SQLContext , SaveMode , SparkSession }
23
23
import za .co .absa .cobrix .cobol .internal .Logging
24
24
import za .co .absa .cobrix .cobol .reader .parameters .CobolParameters
25
25
import za .co .absa .cobrix .spark .cobol .parameters .CobolParametersParser ._
@@ -35,6 +35,7 @@ import za.co.absa.cobrix.spark.cobol.utils.{BuildProperties, SparkUtils}
35
35
class DefaultSource
36
36
extends RelationProvider
37
37
with SchemaRelationProvider
38
+ with CreatableRelationProvider
38
39
with DataSourceRegister
39
40
with ReaderFactory
40
41
with Logging {
@@ -45,6 +46,7 @@ class DefaultSource
45
46
createRelation(sqlContext, parameters, null )
46
47
}
47
48
49
+ /** Reader relation */
48
50
override def createRelation (sqlContext : SQLContext , parameters : Map [String , String ], schema : StructType ): BaseRelation = {
49
51
CobolParametersValidator .validateOrThrow(parameters, sqlContext.sparkSession.sparkContext.hadoopConfiguration)
50
52
@@ -59,6 +61,36 @@ class DefaultSource
59
61
cobolParameters.debugIgnoreFileSize)(sqlContext)
60
62
}
61
63
64
+ /** Writer relation */
65
+ override def createRelation (sqlContext : SQLContext , mode : SaveMode , parameters : Map [String , String ], data : DataFrame ): BaseRelation = {
66
+ val path = parameters.getOrElse(" path" ,
67
+ throw new IllegalArgumentException (" Path is required for this data source." ))
68
+
69
+ mode match {
70
+ case SaveMode .Overwrite =>
71
+ val outputPath = new Path (path)
72
+ val hadoopConf = sqlContext.sparkContext.hadoopConfiguration
73
+ val fs = outputPath.getFileSystem(hadoopConf)
74
+ if (fs.exists(outputPath)) {
75
+ fs.delete(outputPath, true )
76
+ }
77
+ case SaveMode .Append =>
78
+ case _ =>
79
+ }
80
+
81
+ // Simply save each row as comma-separated values in a text file
82
+ data.rdd
83
+ .map(row => row.mkString(" ," ))
84
+ .saveAsTextFile(path)
85
+
86
+ new BaseRelation {
87
+ override def sqlContext : SQLContext = sqlContext
88
+
89
+ override def schema : StructType = data.schema
90
+ }
91
+ }
92
+
93
+
62
94
// TODO fix with the correct implementation once the correct Reader hierarchy is put in place.
63
95
override def buildReader (spark : SparkSession , parameters : Map [String , String ]): FixedLenReader = null
64
96
0 commit comments