-
Notifications
You must be signed in to change notification settings - Fork 17
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
9 changed files
with
297 additions
and
17 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
98 changes: 98 additions & 0 deletions
98
parquet/src/main/scala/monix/connect/parquet/ParquetSubscriberCoeval.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,98 @@ | ||
/* | ||
* Copyright (c) 2020-2020 by The Monix Connect Project Developers. | ||
* See the project homepage at: https://connect.monix.io | ||
* | ||
* 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 monix.connect.parquet | ||
|
||
import monix.eval.Coeval | ||
import monix.execution.cancelables.AssignableCancelable | ||
import monix.execution.internal.{InternalApi, Platform} | ||
import monix.execution.{Ack, Callback, Scheduler} | ||
import monix.reactive.Consumer | ||
import monix.reactive.observers.Subscriber | ||
import org.apache.parquet.hadoop.ParquetWriter | ||
|
||
import scala.concurrent.Future | ||
import scala.util.control.NonFatal | ||
|
||
/** | ||
* A sink that writes each element of [[T]] passed, into a single parquet file. | ||
* | ||
* @param parquetWriter The apache hadoop generic implementation of a parquet writer. | ||
* @tparam T Represents the type of the elements that will be written into the parquet file. | ||
*/ | ||
@InternalApi | ||
private[parquet] class ParquetSubscriberCoeval[T](parquetWriter: Coeval[ParquetWriter[T]]) extends Consumer[T, Long] { | ||
|
||
def createSubscriber(callback: Callback[Throwable, Long], s: Scheduler): (Subscriber[T], AssignableCancelable) = { | ||
val out = new Subscriber[T] { | ||
override implicit val scheduler: Scheduler = s | ||
|
||
//the number of parquet files that has been written that is returned as materialized value | ||
private[this] var nElements: Long = 0 | ||
private[this] val memoizedWriter = parquetWriter | ||
|
||
// Protects from the situation where last onNext throws, we call onError and then | ||
// upstream calls onError or onComplete again | ||
private[this] var isDone = false | ||
|
||
override def onNext(record: T): Future[Ack] = { | ||
memoizedWriter.map { parquetWriter => | ||
try { | ||
parquetWriter.write(record) | ||
nElements = nElements + 1 | ||
Ack.Continue | ||
} catch { | ||
case ex if NonFatal(ex) => | ||
onError(ex) | ||
Ack.Stop | ||
} | ||
}.value() | ||
} | ||
|
||
override def onComplete(): Unit = | ||
if (!isDone) { | ||
isDone = true | ||
memoizedWriter.map { writer => | ||
try { | ||
writer.close() | ||
callback.onSuccess(nElements) | ||
} catch { | ||
case NonFatal(ex) => | ||
callback.onError(ex) | ||
} | ||
}.value() | ||
} | ||
|
||
override def onError(ex: Throwable): Unit = | ||
if (!isDone) { | ||
isDone = true | ||
memoizedWriter.map { writer => | ||
try { | ||
writer.close() | ||
callback.onError(ex) | ||
} catch { | ||
case NonFatal(ex2) => | ||
callback.onError(Platform.composeErrors(ex, ex2)) | ||
} | ||
}.value() | ||
} | ||
} | ||
|
||
(out, AssignableCancelable.multi()) | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.