Chris Halcrow
Chris Halcrow

Reputation: 31960

Apache beam with MessagePack - how do I get values from Map<Value, Value>?

Inside an Apache Beam transform I can successfully transform pub/sub values (in MessagePack format) that I read from Google Cloud Pub/Sub into a map of MessagePack Value objects like this:

    @ProcessElement
    public void processElement(ProcessContext c) 
    {
        Map<Value, Value> map = MessagePack.newDefaultUnpacker(c.element().getPayload()).unpackValue().asMapValue().map();

When I inspect map I can see the following:

enter image description here

If I then try to get a value like this it always returns null:

map.get("Tz")

How do I get a value? Do I need to transform the values in a different way, or do I need a different way of retrieving them?

Upvotes: 1

Views: 251

Answers (2)

Chris Halcrow
Chris Halcrow

Reputation: 31960

The map keys are MessagePack Value objects, so I needed to do this to reference the key, and return the values as strings:

import org.msgpack.value.ValueFactory;

map.get(ValueFactory.newString("Tz")).toString()

See also In messagepack, error while getting value from MapValue.. Please help me (the solution didn't work in my case, but there are some suggestions there for dealing with different types that can be used in a Map)

Upvotes: 2

Justin
Justin

Reputation: 1356

The map is Map, so the key is a value object, but you are using a String s the key when you do map.get("Tz")

Can you create a Value object using Tz and attempt to get based on that Value object?

Upvotes: 2

Related Questions