jirislav / scala-protobuf-extractor   1.0.0

GNU General Public License v2.0 only GitHub

Fast Protobuf field extractor written in Scala. Its purpose is to read fast a given tag from serialized message without the need to deserialize whole message.

Scala versions: 2.13 2.12

scala-protobuf-extractor

Fast Protobuf field extractor written in Scala. Its purpose is to read fast a given tag from serialized message without the need to deserialize whole message.

Motivation

It would be nice to have a fast and reliable method for single field extraction from Google Protocol Buffer message, without the need to deserialize whole message into an object in JVM.

It might be used with success in high throughput applications (e.g. metrics extraction using Apache Flink for streamed data).

Implementation Resources

Usage

// First, get the PB field descriptor
val fieldDescriptor = YourGeneratedMessageV3.Builder.getDescriptor.findFieldByName("fieldName")

// And provide the input stream (holding the bytes with the message definition) to method for particular field type extraction
val staticallyExtracted = FieldExtractor.staticExtract(
  codedInputStream,
  fieldDescriptor,
  _.readBool()
)

// With dynamic extraction, you have to provide the expected type, otherwise, AnyRef will be returned by default
val dynamicallyExtracted: Boolean = FieldExtractor.dynamicExtract(
  codedInputStream,
  fieldDescriptor
)

Gradle

dependencies {
    // ...

    // Scala 2.13
    implementation 'cz.jkozlovsky:scala-protobuf-field-extractor_2.13:1.0.0'

    // Scala 2.12
    implementation 'cz.jkozlovsky:scala-protobuf-field-extractor_2.12:1.0.0'

    // ...
}

Sbt

// Scala 2.13
libraryDependencies += cz.jkozlovsky % scala-protobuf-field-extractor_2.13 % 1.0.0

// Scala 2.12
libraryDependencies += cz.jkozlovsky % scala-protobuf-field-extractor_2.12 % 1.0.0