Skip to content
paulcleary edited this page Nov 10, 2015 · 2 revisions

The Wire module is intended to be used for marshalling / unmarshalling data across application boundaries. It provides the ability to convert easily from a Money Span to Avro as well as Json formats.

Why do I need it?

This would be used by Money implementers who want to receive Span data over Kafka or Http, or perhaps develop their own Emitter and Collector for handling the Span data.

Another use case is for users who want to store their Span data in Hadoop. They can encode all data in Avro format.

Adding the dependency

Add a dependency as follows for maven:

    <dependency>
        <groupId>com.comcast.money</groupId>
        <artifactId>money-wire_2.10</artifactId>
        <version>${money.version}</version>
    </dependency>

And here is a dependency for SBT:

    libraryDependencies += "com.comcast.money" %% "money-wire" % "${money.version}"

Usage

The wire module uses Scala implicit types in order to add extensions to existing classes; in particular String, Array[Byte], and Span.

import com.comcast.money.wire.AvroConversions._

class SpanStore extends Storage {

  def save(span:Span) = {
    val bytes = span.convertTo[Array[Byte]]
    store(span.spanId.toString, bytes)
  }

  def get(key:String):Span = {
    val bytes = fetch(key)
    bytes.convertTo[Span]
  }
}

The previous example is a fictitious data store, that supports saving and retrieving Money Span data as Avro encoded bytes with the span id as the key. You can imagine that the underlying storage implementation could be any key-value database.

import com.comcast.money.wire.JsonConversions._

class SpanStore extends Storage {

  def save(span:Span) = {
    val json = span.convertTo[String]
    store(span.spanId.toString, json)
  }

  def get(key:String):Span = {
    val json = fetch(key)
    json.convertTo[Span]
  }
}

The previous example is just a variation of the first example, here we are saving the data encoded as json instead of avro.