-
Notifications
You must be signed in to change notification settings - Fork 4
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
Transactional writer support #6
Changes from 1 commit
8395e56
1cb610f
66e88cc
59f0d48
f95476a
6ab1838
b567132
deb95ad
2fd0b86
902ba07
9807c59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.github.anicolaspp.spark.sql.writing | ||
|
||
import org.apache.spark.sql.sources.v2.writer.WriterCommitMessage | ||
|
||
case class CommittedIds(partitionId: Int, ids: Set[String]) extends WriterCommitMessage |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.anicolaspp.spark.sql.writing | ||
|
||
import org.ojai.store.{DocumentStore, DriverManager} | ||
|
||
object MapRDBCleaner { | ||
|
||
def clean(ids: Set[String], table: String): Unit = { | ||
|
||
val connection = DriverManager.getConnection("ojai:mapr:") | ||
|
||
val store: DocumentStore = connection.getStore(table) | ||
|
||
ids.foreach(store.delete) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a normal rollback this should be fine since the only the confirmed written ids are in the |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.github.anicolaspp.spark.sql.writing | ||
|
||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.sources.v2.writer.{DataSourceWriter, DataWriterFactory, WriterCommitMessage} | ||
import org.apache.spark.sql.types.StructType | ||
|
||
class MapRDBDataSourceWriter(table: String, schema: StructType) extends DataSourceWriter with Logging { | ||
|
||
private var globallyCommittedIds = List.empty[String] | ||
|
||
override def createWriterFactory(): DataWriterFactory[Row] = new MapRDBDataWriterFactory(table, schema) | ||
|
||
override def commit(messages: Array[WriterCommitMessage]): Unit = { | ||
|
||
val ids = messages.foldLeft(Set.empty[String]) { case (acc, CommittedIds(partitionId, partitionIds)) => | ||
log.info(s"PARTITION $partitionId HAS BEEN CONFIRMED BY DRIVER") | ||
|
||
acc ++ partitionIds | ||
} | ||
|
||
// Let's make sure this is thread-safe | ||
globallyCommittedIds = this.synchronized { | ||
globallyCommittedIds ++ ids | ||
} | ||
|
||
} | ||
|
||
override def abort(messages: Array[WriterCommitMessage]): Unit = { | ||
log.info("JOB BEING ABORTED") | ||
log.info("JOB CLEANING UP") | ||
|
||
MapRDBCleaner.clean(globallyCommittedIds.toSet, table) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.github.anicolaspp.spark.sql.writing | ||
|
||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.sources.v2.writer.{DataWriter, WriterCommitMessage} | ||
|
||
class MapRDBDataWriter extends DataWriter[Row] with Logging { | ||
override def write(record: Row): Unit = ??? | ||
|
||
override def commit(): WriterCommitMessage = ??? | ||
|
||
override def abort(): Unit = ??? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.github.anicolaspp.spark.sql.writing | ||
|
||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.sources.v2.writer.{DataWriter, DataWriterFactory, WriterCommitMessage} | ||
import org.apache.spark.sql.types.StructType | ||
import org.ojai.store.{DocumentStore, DriverManager} | ||
|
||
class MapRDBDataWriterFactory(table: String, schema: StructType) extends DataWriterFactory[Row] { | ||
|
||
@transient private lazy val connection = DriverManager.getConnection("ojai:mapr:") | ||
|
||
@transient private lazy val store: DocumentStore = connection.getStore(table) | ||
|
||
private val writtenIds = scala.collection.mutable.ListBuffer.empty[String] | ||
|
||
override def createDataWriter(partitionId: Int, attemptNumber: Int): DataWriter[Row] = new DataWriter[Row] with Logging { | ||
|
||
log.info(s"PROCESSING PARTITION ID: $partitionId ; ATTEMPT: $attemptNumber") | ||
|
||
override def write(record: Row): Unit = { | ||
|
||
val doc = schema | ||
.fields | ||
.map(field => (field.name, schema.fieldIndex(field.name))) | ||
.foldLeft(connection.newDocumentBuilder()) { case (acc, (name, idx)) => acc.put(name, record.getString(idx)) } | ||
.getDocument | ||
|
||
this.synchronized { | ||
if (!writtenIds.contains(doc.getIdString)) { | ||
store.insert(doc) | ||
writtenIds.append(doc.getIdString) | ||
} | ||
} | ||
} | ||
|
||
override def commit(): WriterCommitMessage = { | ||
log.info(s"PARTITION $partitionId COMMITTED AFTER ATTEMPT $attemptNumber") | ||
|
||
CommittedIds(partitionId, writtenIds.toSet) | ||
} | ||
|
||
override def abort(): Unit = { | ||
log.info(s"PARTITION $partitionId ABORTED AFTER ATTEMPT $attemptNumber") | ||
|
||
MapRDBCleaner.clean(writtenIds.toSet, table) | ||
|
||
log.info(s"PARTITION $partitionId CLEANED UP") | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want the connector to replace the official connector at some point then the
createTable
andbulkInsert
options should be exposed to the caller. Either through default parameters or through the .option method. Similar to how the current connector allowsdf.write.option("Operation","Insert").saveToMapRDB("path")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is fine. I will create an issue to track that.
GH-9