Added the ability to return discreet objects from M/R queries as well as Lists
This commit is contained in:
@@ -548,11 +548,25 @@ public class RiakTemplate extends RestGatewaySupport implements KeyValueStoreOpe
|
||||
|
||||
public <T> T execute(MapReduceJob job, Class<T> targetType) {
|
||||
RestTemplate restTemplate = getRestTemplate();
|
||||
ResponseEntity<T> resp = restTemplate.postForEntity(mapReduceUri,
|
||||
ResponseEntity<List> resp = restTemplate.postForEntity(mapReduceUri,
|
||||
job.toJson(),
|
||||
targetType);
|
||||
List.class);
|
||||
if (resp.hasBody()) {
|
||||
return resp.getBody();
|
||||
if (!targetType.isAssignableFrom(List.class)) {
|
||||
List<?> results = (List<?>) resp.getBody();
|
||||
if (results.size() == 1) {
|
||||
Object obj = results.get(0);
|
||||
if (obj.getClass() != targetType) {
|
||||
ConversionService conv = getConversionService();
|
||||
if (conv.canConvert(obj.getClass(), targetType)) {
|
||||
return conv.convert(obj, targetType);
|
||||
}
|
||||
} else {
|
||||
return (T) obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (T) resp.getBody();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ class RiakTemplateSpec extends Specification {
|
||||
|
||||
}
|
||||
|
||||
def "Test Map/Reduce"() {
|
||||
def "Test Map/Reduce returning Integer"() {
|
||||
|
||||
given:
|
||||
MapReduceJob job = riak.createMapReduceJob()
|
||||
@@ -193,7 +193,29 @@ class RiakTemplateSpec extends Specification {
|
||||
addPhase(reducePhase)
|
||||
|
||||
when:
|
||||
def result = riak.execute(job, List)
|
||||
def result = riak.execute(job, Integer)
|
||||
|
||||
then:
|
||||
1 == result
|
||||
|
||||
}
|
||||
|
||||
def "Test Map/Reduce returning List"() {
|
||||
|
||||
given:
|
||||
MapReduceJob job = riak.createMapReduceJob()
|
||||
def mapJs = new JavascriptMapReduceOperation("function(v){ return [1]; }")
|
||||
def mapPhase = new RiakMapReducePhase("map", "javascript", mapJs)
|
||||
|
||||
def reduceJs = new JavascriptMapReduceOperation("function(v){ return [v.length]; }")
|
||||
def reducePhase = new RiakMapReducePhase("reduce", "javascript", reduceJs)
|
||||
|
||||
job.addInputs(["test"]).
|
||||
addPhase(mapPhase).
|
||||
addPhase(reducePhase)
|
||||
|
||||
when:
|
||||
def result = riak.execute(job)
|
||||
|
||||
then:
|
||||
1 == result.size()
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by J. Brisbin <jon@jbrisbin.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.riak.core;
|
||||
|
||||
/**
|
||||
* @author J. Brisbin <jon@jbrisbin.com>
|
||||
*/
|
||||
public class TestObject {
|
||||
String test = "value";
|
||||
Integer integer = 12;
|
||||
|
||||
public String getTest() {
|
||||
return test;
|
||||
}
|
||||
|
||||
public void setTest(String test) {
|
||||
this.test = test;
|
||||
}
|
||||
|
||||
public Integer getInteger() {
|
||||
return integer;
|
||||
}
|
||||
|
||||
public void setInteger(Integer integer) {
|
||||
this.integer = integer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user