-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
137 additions
and
4 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
16 changes: 16 additions & 0 deletions
16
src/main/scala/uk/co/gresearch/spark/dgraph/connector/TripleBatchWrite.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package uk.co.gresearch.spark.dgraph.connector | ||
|
||
import org.apache.spark.sql.connector.write.{BatchWrite, DataWriterFactory, PhysicalWriteInfo, WriterCommitMessage} | ||
import org.apache.spark.sql.types.StructType | ||
import uk.co.gresearch.spark.dgraph.connector.model.GraphTableModel | ||
|
||
case class TripleBatchWrite(schema: StructType, model: GraphTableModel) extends BatchWrite { | ||
override def createBatchWriterFactory(physicalWriteInfo: PhysicalWriteInfo): DataWriterFactory = | ||
TripleDataWriterFactory(schema, model) | ||
|
||
override def commit(writerCommitMessages: Array[WriterCommitMessage]): Unit = { | ||
writerCommitMessages.foreach(msg => Console.println(s"Committed $msg")) | ||
} | ||
|
||
override def abort(writerCommitMessages: Array[WriterCommitMessage]): Unit = { } | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/scala/uk/co/gresearch/spark/dgraph/connector/TripleDataWriter.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package uk.co.gresearch.spark.dgraph.connector | ||
|
||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.connector.write.{DataWriter, WriterCommitMessage} | ||
import org.apache.spark.sql.types.StructType | ||
import uk.co.gresearch.spark.dgraph.connector.model.GraphTableModel | ||
|
||
case class TripleDataWriter(schema: StructType, model: GraphTableModel) extends DataWriter[InternalRow] { | ||
var triples = 0L | ||
|
||
override def write(row: InternalRow): Unit = { | ||
// Console.println(s"Writing row: $row") | ||
triples = triples + 1 | ||
} | ||
|
||
override def commit(): WriterCommitMessage = { | ||
val msg: WriterCommitMessage = new WriterCommitMessage { | ||
val name: String = s"$triples triples (${Thread.currentThread().getName})" | ||
override def toString: String = name | ||
} | ||
Console.println(s"Committing $msg") | ||
msg | ||
} | ||
|
||
override def abort(): Unit = { } | ||
|
||
override def close(): Unit = { } | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/scala/uk/co/gresearch/spark/dgraph/connector/TripleDataWriterFactory.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package uk.co.gresearch.spark.dgraph.connector | ||
|
||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.connector.write.{DataWriter, DataWriterFactory} | ||
import org.apache.spark.sql.types.StructType | ||
import uk.co.gresearch.spark.dgraph.connector.model.GraphTableModel | ||
|
||
case class TripleDataWriterFactory(schema: StructType, model: GraphTableModel) extends DataWriterFactory { | ||
override def createWriter(partitionId: Int, taskId: Long): DataWriter[InternalRow] = TripleDataWriter(schema, model) | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/scala/uk/co/gresearch/spark/dgraph/connector/TripleWriteBuilder.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package uk.co.gresearch.spark.dgraph.connector | ||
|
||
import org.apache.spark.sql.connector.write.{BatchWrite, WriteBuilder} | ||
import org.apache.spark.sql.types.StructType | ||
import uk.co.gresearch.spark.dgraph.connector.model.GraphTableModel | ||
|
||
case class TripleWriteBuilder(schema: StructType, model: GraphTableModel) | ||
extends WriteBuilder { | ||
override def buildForBatch(): BatchWrite = TripleBatchWrite(schema, model) | ||
} |
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
48 changes: 48 additions & 0 deletions
48
src/test/scala/uk/co/gresearch/spark/dgraph/connector/TestWriter.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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2020 G-Research | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.co.gresearch.spark.dgraph.connector | ||
|
||
import org.apache.spark.sql.SaveMode | ||
import org.apache.spark.sql.types.{DoubleType, StringType} | ||
import org.scalatest.funspec.AnyFunSpec | ||
import uk.co.gresearch.spark.dgraph.DgraphTestCluster | ||
|
||
class TestWriter extends AnyFunSpec with ConnectorSparkTestSession with DgraphTestCluster { | ||
|
||
import spark.implicits._ | ||
|
||
// we want a fresh cluster that we can mutate, definitively not one that is always running and used by all tests | ||
override val clusterAlwaysStartUp: Boolean = true | ||
|
||
describe("Connector") { | ||
it("should write") { | ||
spark.range(0, 1000000, 1, 10) | ||
.select( | ||
$"id".as("subject"), | ||
$"id".cast(StringType).as("str"), | ||
$"id".cast(DoubleType).as("dbl") | ||
) | ||
.repartition($"subject") | ||
.sortWithinPartitions($"subject") | ||
.write | ||
.mode(SaveMode.Append) | ||
.option("dgraph.nodes.mode", "wide") | ||
.format("uk.co.gresearch.spark.dgraph.nodes") | ||
.save(dgraph.target) | ||
} | ||
} | ||
} |