score:1
first problem as noted in the comments is that reduce_identity
returns a list of bucket/key pairs which isn't what you want. second problem is ... you can't go straight to your pojo from mapreduce if you're storing something other than json. third (sorta) problem is ... riak mapreduce is really not made for binary values.
the following uses the kryopersonconverter
and person
class from the example in the cookbook that demonstrates how to use kryo in a custom converter.
i'm going to break to code sample up and inline my comments:
public class app
{
public static void main( string[] args ) throws riakexception
{
list<person> personlist = new arraylist<person>();
person p = new person("bob","111 elm street","555-1212");
personlist.add(p);
p = new person("jenny","122 spruce lane","867-5309");
personlist.add(p);
p = new person("steve","333 oak place","555-1111");
personlist.add(p);
iriakclient client = riakfactory.pbcclient();
bucket b = client.fetchbucket("people").execute();
kryopersonconverter converter = new kryopersonconverter("people");
for (person p2 : personlist)
{
b.store(p2).withconverter(converter).execute();
}
p = new person();
p.setname("jenny");
p = b.fetch(p).withconverter(converter).execute();
assert(p.getphone().equals("867-5309")); // i got your number
everything up to now? a-ok! we've stored a pojo in riak after having used kryo to serialize it, and retrieved it.
mapreduceresult result = client.mapreduce("people")
.addmapphase(namederlangfunction.map_object_value)
.execute();
system.out.println(result.getresultraw());
and here we see the problem, as the output of that println()
is:
["\u0001\u0001\u000e111 elm street\u0001\u0003bob\u0001\b555-1212","\u0001\u0001\r333 oak place\u0001\u0005steve\u0001\b555-1111","\u0001\u0001\u000f122 spruce lane\u0001\u0005jenny\u0001\b867-5309"]
unfortunately, mapreduce in riak is really meant to be used with json data (or just plain strings) when talking about stored data. we have a json array containing json strings of the bytes we stored.
to work with that, you'd have to get the string
s as a collection
, then convert the bytes using kryo.
collection<string> objects = result.getresult(string.class);
kryo kryo = new kryo();
kryo.register(person.class);
objectbuffer buffer = new objectbuffer(kryo);
for (string s : objects)
{
person p3 = buffer.readobject(s.getbytes(), person.class);
system.out.println(p3.getname() + " " + p3.getphone());
}
client.shutdown();
}
}
and you'd get the output:
bob 555-1212
steve 555-1111
jenny 867-5309
Source: stackoverflow.com
Related Query
- Riak-Java cannot deserialize domain objects from MapReduce query in Scala?
- Scala 2.12.4: Cannot access protected static Java method from another package anymore
- How to pass implicit vals defined through package objects from Scala in Java
- Cannot Load Java Resources in Spark from Scala
- Cannot access a Java static method from Scala
- How to convert objects from scala to java correctly?
- How to get Scala List from Java List?
- Compilation failed: error while loading AnnotatedElement, ConcurrentMap, CharSequence from Java 8 under Scala 2.10?
- How to show scala doc from Java Editor in Eclipse?
- How do I abstract the domain layer from the persistence layer in Scala
- Scala inheritance from Java class: select which super constructor to call
- Any way to obtain a Java class from a Scala (2.10) type tag or symbol?
- How to use Scala varargs from Java code
- Can I use a scala class which implements a java interface from Java?
- How do you call Scala objects from Java?
- Nested Scala singletons from Java code
- Calling a Scala from Java Play Framework which takes a ClassTag
- Accessing scala object fields from java
- Package-private scope in Scala visible from Java
- Accessing Scala nested classes from Java
- Getting a Scala Map from a Java Properties
- Create a HashMap in Scala from a list of objects without looping
- How to call main method of a Scala program from the main method of a java program?
- NoClassDefFoundError using Scala class from Java
- How to access a Java static method from Scala given a type alias for that class it resides in
- How to refer to protected inner class in Scala when inheriting from Java (with byte code only)
- Calling Java vararg method from Scala with primitives
- Is there a quick way to convert Java xml objects to Scala xml objects?
- Single iteration => Multiple output collections from Java to Scala
- Using Scala type aliases from Java code
More Query from same tag
- Scala Patch function
- ActiveSlick query compilation error
- ScalaCheck not failing?
- Is it possible to do tail-recursion on a method which returns ZIO?
- Cannot generate indexable for a type containing a Map with a custom Key type
- Scala map cannot resolve mapper function
- JUnit 5: How to assert an exception is thrown in Scala?
- Spray routing filter path parameter
- Why does this Iterable produce a Set after mapping?
- Scala: how to flatten a List of a Set of filepaths
- Play2 - validation of dynamic part of route(s)
- scala iterator hasNext random behaviour
- Scala - error "value map is not a member of"
- Scala Generics Type Constraints
- Is it possible to make a generic training pipeline for Random Forest in Spark ML?
- Get a value from RichPipe
- What is the difference between switchLatest and flatmapLastest in RxJs
- how to make predictionio/flatspec create HTML output if I run the tests in sbt?
- What is the Scala syntax for instantiating using a specific implementation?
- joinLeft error: constructor cannot be instantiated to expected type
- How to select the resultant columns from one of the dataframes while joining with other in Spark?
- scala jackson dealing with any type, possible custom interceptor required
- Compilation issue in spark scala script containing join on RDDs with 2 columns
- Akka Actor Setup: In Main method or in 'Manager' class?
- Hybrid Functional/Object Oriented languages like F#/Scala: Use types with methods or methods and types
- How can I access args from trait?
- Computer Algebra System (CAS) for Scala
- How to create a List of Lists in Scala in functional programming style
- Cannot splat an Array into function's arguments that accepts varargs
- A type constructor IS a monad or HAS a monad?