Riak Java client questions

Oved Machlev omachlev at interwise.com
Sun Aug 5 10:25:31 EDT 2012



Thanks Brian, that really helped!

      Regarding #4: it's within question 2 below... I have encountered additional issues, would be great if you could assist:

1.      I have a BaseEntity class and a derived class (ServiceProvider).  BaseEntity has an Id which is annotated with @RiakKey. But it seems that I also have to have this field with this annotation in the derived class. If I don't have this duplication I get this exception:

      com.basho.riak.client.convert.NoKeySpecifedException
          at com.basho.riak.client.bucket.DefaultBucket.fetch(DefaultBucket.java:535)
          at com.att.cso.omss.datastore.riak.controllers.RiakBaseController.isEntityExist(RiakBaseController.java:130)
          at com.att.cso.omss.datastore.riak.controllers.RiakBaseController.createEntity(RiakBaseController.java:94)
          at com.att.cso.omss.datastore.riak.controllers.RiakBaseController.createServiceProvider(RiakBaseController.java:234)
          at com.att.cso.omss.datastore.riak.App.serviceProviderTests(App.java:62)
          at com.att.cso.omss.datastore.riak.App.main(App.java:38)

      2. what is the recommended way to run a filtered query?  I'm getting really frustrated trying to perform queries with some sort of filter. I have tried many methods but always reached a dead end.

      a. I have tried indexes as follows (I enabled the indexing (storage_backend in config file)):

              IndexQuery iq = new BinValueQuery(BinIndex.named("name"),"serviceProvider", "oved1");
              MapReduceResult result = riakClient.mapReduce(iq).addReducePhase(NamedErlangFunction.REDUCE_IDENTITY).execute();

      but getting empty result (although the record exists).

      b. I have tried SearchMapReduce but getting an exception (I have enabled search on the bucket before storing objects to it):

        MapReduceResult result = riakClient.
                        mapReduce("serviceProvider", "name:oved1").
                        addMapPhase(new NamedJSFunction("Riak.mapValuesJson"), true).execute();

      Exception:

      com.basho.riak.client.RiakException: java.io.IOException: {"error":"map_reduce_error"}
          at com.basho.riak.client.query.MapReduce.execute(MapReduce.java:80)
          at com.att.cso.omss.datastore.riak.controllers.RiakBaseController.getAllServiceProvider(RiakBaseController.java:339)
          at com.att.cso.omss.datastore.riak.App.serviceProviderTests(App.java:64)
          at com.att.cso.omss.datastore.riak.App.main(App.java:38)
      Caused by: java.io.IOException: {"error":"map_reduce_error"}
          at com.basho.riak.client.raw.http.ConversionUtil.convert(ConversionUtil.java:588)
          at com.basho.riak.client.raw.http.HTTPClientAdapter.mapReduce(HTTPClientAdapter.java:386)
          at com.basho.riak.client.query.MapReduce.execute(MapReduce.java:78)
          ... 3 more


      c. This actually seems to work, but I am not exactly sure on how to use it (setting the field to sort_by, calling other functions (JS/Erlang) to perform filtering, etc...):

                MapReduceResult result = riakClient.
                                mapReduce("serviceProvider").
                                addMapPhase(new NamedJSFunction("Riak.mapValuesJson"), false).
                                addReducePhase(new NamedErlangFunction("riak_kv_mapreduce", "reduce_sort"), true).
                                execute();


      d. when trying to linkWalk I get no result, although the link exist (see below):

                MapReduceResult result = riakClient.
                                mapReduce("serviceProvider").
                                addMapPhase(new NamedJSFunction("Riak.mapValuesJson"), false).
                                addReducePhase(new NamedErlangFunction("riak_kv_mapreduce", "reduce_sort"), false).
                                addLinkPhase(("service, "tag_1", true).
                                execute();



      [root at localhost riak]# curl -v http://127.0.0.1:8098/riak/serviceProvider/1xxxx
      ...
      < Link: </riak/service/3>; riaktag="tag_1", </riak/serviceProvider>; rel="up"
      ...
      {"name":"oved1","LastUpdate":"1344173461469","Data":null,"Id":"1xxxx"}[root at localhost riak]#


      [root at localhost riak]# curl -v http://127.0.0.1:8098/riak/service/3
      ...
      < Link: </riak/serviceProvider/1xxxx>; riaktag="tag_1", </riak/service>; rel="up"
      ...
      {"name":"firewall111","serviceCustomers":[],"LastUpdate":"1344174190964","Data":null,"Active":true}[root at localhost riak]#



      I have attached the entities classes and the controller class.

      Thanks you very much!

      Oved Machlev



Thanks,
Oved Machlev
____________________________
Get AT&T Connect for Mobile now!
Android | iPhone | iPad | BB

-----Original Message-----
From: Brian Roach [mailto:roach at basho.com]
Sent: Thursday, August 02, 2012 20:47
To: Oved Machlev
Cc: riak-users at lists.basho.com
Subject: Re: Riak Java client questions

Hi Oved - replies inlined below.


On Aug 2, 2012, at 11:04 AM, Oved Machlev wrote:

> Hi,
>
> I have been working with Riak in past 3 weeks, using the java client.
> I have encountered a few issues, and could not find solutions so far.
> This is the first time I write an email to this mailing list - Would
> be great if it reaches the right destination J
>
> 1.       Riak supports auto generated keys when storing an object - http://wiki.basho.com/Basic-Riak-API-Operations.html:
> Store a new object and assign random key #
>
> If your application would rather leave key-generation up to Riak, issue a POST request to the bucket URL instead of a PUT to a bucket/key pair: POST /riak/bucket If you don't pass Riak a "key" name after the bucket, it will know to create one for you.
>
> Is it possible to do the same when using the java client? it seems that key must be provided when storing an object.
>

Right now the Java client doesn't support this. These is an issue open for this on github and I agree it needs to be added.

After the 1.0.6 release (along with Riak 1.2) I hope to work on this and get it added.

> 2.       I have a POJO which I store. It contains a set of objects which I do not wish to persist, I mark it as transient:
>
> transient private Set<Service> services; but still, this set is being
> persisted - when I fetch my POJO (curl and java) I can see this:
>
>
> {"name":"oved","lastUpdate":"1343923201735","data":null,"services":[]}
>
> Is there something else I need to do to tell Riak to ignore it?
>

The default Converter<T> used if you do not specify your own is the JSONConverter. This uses the Jackson JSON library.

To exclude a field from serialization you would need to use the (org.codehaus.jackson.annotate) @JsonIgnore annotation.

> 3.       This is more basic question - I keep on thinking that my stored objects should implement the IRIAKObject, but I avoided doing that so far, because in all the examples in the cookbook, it is never being done. So in theoretical level - should the objects that are stored in riak database implement this interface or not? Is there any value in doing that?
>

Not particularly. That's what the Converter<T> is doing behind the scenes; converting back and forth between your POJO and a IRiakObject


> 4.       MapReduce - If I perform the following without the line in bold (link) I get the collection of ServiceProviders without the Services that are linked.
> When adding the LinkPhase, I am getting a JsonMappingException (see below). Any idea what is causing that? What am I missing? Both objects (service and serviceProvider) are stored as JsonObjects.
>
>     public Collection<ServiceProvider> getAllServiceProvider() throws
> Exception{
>
>         BucketMapReduce m = riakClient.mapReduce("SERVICE_PROVIDER");
>         m.addMapPhase(new NamedJSFunction("Riak.mapValuesJson"), true);
>         m.addLinkPhase("SERVICE", "_");
>         MapReduceResult result = m.execute();
>         return result.getResult(ServiceProvider.class);
>     }
>
>
> The exception:
> com.basho.riak.client.convert.ConversionException:
> org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.att.cso.omss.datastore.riak.entities.ServiceProvider out of START_ARRAY token  at [Source: java.io.StringReader at 31958905<mailto:java.io.StringReader at 31958905>; line: 1, column: 2]
>     at
> com.basho.riak.client.raw.http.ConversionUtil$1.getResult(ConversionUt
> il.java:601)
>
>

Jackson is having some issue trying to map the JSON returned to your ServiceProvider class. Can you check the JSON stored in Riak and make sure it's what you think it is? If I could see the class and the JSON I might be able to offer more insight.


> Sorry for the long email, and thanks in advance...

No Worries!

Thanks,
Brian Roach


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20120805/86331e61/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BaseEntity.java
Type: application/octet-stream
Size: 4387 bytes
Desc: BaseEntity.java
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20120805/86331e61/attachment.java>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Service.java
Type: application/octet-stream
Size: 4635 bytes
Desc: Service.java
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20120805/86331e61/attachment-0001.java>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ServiceProvider.java
Type: application/octet-stream
Size: 3898 bytes
Desc: ServiceProvider.java
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20120805/86331e61/attachment-0002.java>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: RiakBaseController.java
Type: application/octet-stream
Size: 25946 bytes
Desc: RiakBaseController.java
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20120805/86331e61/attachment-0003.java>


More information about the riak-users mailing list