Converting scala self describing contexts to java

I’m currently converting our google cloud dataflow to read a globally defined context which as been set on our events. The scala sdk shows this context as being available on the event class in the form of an immutable list (https://github.com/snowplow/snowplow-scala-analytics-sdk/blob/master/src/main/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/Event.scala#L91):

final List<SelfDescribingData<Json>> contexts

Does anyone have any pointers on how to properly cast / convert this to a Java list?

For reference I’m converting from the enriched pubsub topic to an event like so:

PubsubMessage message = context.element();
Validated<ParsingError, Event> event = Event.parse(new String(message.getPayload(), StandardCharsets.UTF_8));
Either<ParsingError, Event> condition = event.toEither();
if (condition.isLeft()) {
    ParsingError err = condition.left().get();
    LOG.error("Event parsing error: " + err.toString() + " for message: " + new String(message.getPayload(), StandardCharsets.UTF_8));
    throw new RuntimeException(err.toString());
} else {
    Event e = condition.right().get();
}

I was expecting to be able to iterate the contexts like so:

for (SelfDescribingData<Json> t : e.contexts()) {
    LOG.info(t.toString());
}

Answering my own question here, you need to use the json converters package:

        List<SelfDescribingData<Json>> list = JavaConverters.seqAsJavaList(e.contexts());
        for(SelfDescribingData<Json> i: list) {
            // extract desired schema
        }
1 Like

The JavaConverters library is indeed the correct way to do this :slight_smile:

1 Like