use of org.apache.solr.client.solrj.io.Tuple in project lucene-solr by apache.
the class StreamExpressionTest method testTimeSeriesStream.
@Test
public void testTimeSeriesStream() throws Exception {
UpdateRequest updateRequest = new UpdateRequest();
int i = 0;
while (i < 50) {
updateRequest.add(id, "id_" + (++i), "test_dt", getDateString("2016", "5", "1"), "price_f", "400.00");
}
while (i < 100) {
updateRequest.add(id, "id_" + (++i), "test_dt", getDateString("2015", "5", "1"), "price_f", "300.0");
}
while (i < 150) {
updateRequest.add(id, "id_" + (++i), "test_dt", getDateString("2014", "5", "1"), "price_f", "500.0");
}
while (i < 250) {
updateRequest.add(id, "id_" + (++i), "test_dt", getDateString("2013", "5", "1"), "price_f", "100.00");
}
updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
String expr = "timeseries(" + COLLECTIONORALIAS + ", q=\"*:*\", start=\"2013-01-01T01:00:00.000Z\", " + "end=\"2016-12-01T01:00:00.000Z\", " + "gap=\"+1YEAR\", " + "field=\"test_dt\", " + "count(*), sum(price_f), max(price_f), min(price_f))";
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString() + "/" + COLLECTIONORALIAS;
TupleStream solrStream = new SolrStream(url, paramsLoc);
StreamContext context = new StreamContext();
solrStream.setStreamContext(context);
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 4);
assertTrue(tuples.get(0).get("test_dt").equals("2013-01-01T01:00:00Z"));
assertTrue(tuples.get(0).getLong("count(*)").equals(100L));
assertTrue(tuples.get(0).getDouble("sum(price_f)").equals(10000D));
assertTrue(tuples.get(0).getDouble("max(price_f)").equals(100D));
assertTrue(tuples.get(0).getDouble("min(price_f)").equals(100D));
assertTrue(tuples.get(1).get("test_dt").equals("2014-01-01T01:00:00Z"));
assertTrue(tuples.get(1).getLong("count(*)").equals(50L));
assertTrue(tuples.get(1).getDouble("sum(price_f)").equals(25000D));
assertTrue(tuples.get(1).getDouble("max(price_f)").equals(500D));
assertTrue(tuples.get(1).getDouble("min(price_f)").equals(500D));
assertTrue(tuples.get(2).get("test_dt").equals("2015-01-01T01:00:00Z"));
assertTrue(tuples.get(2).getLong("count(*)").equals(50L));
assertTrue(tuples.get(2).getDouble("sum(price_f)").equals(15000D));
assertTrue(tuples.get(2).getDouble("max(price_f)").equals(300D));
assertTrue(tuples.get(2).getDouble("min(price_f)").equals(300D));
assertTrue(tuples.get(3).get("test_dt").equals("2016-01-01T01:00:00Z"));
assertTrue(tuples.get(3).getLong("count(*)").equals(50L));
assertTrue(tuples.get(3).getDouble("sum(price_f)").equals(20000D));
assertTrue(tuples.get(3).getDouble("max(price_f)").equals(400D));
assertTrue(tuples.get(3).getDouble("min(price_f)").equals(400D));
expr = "timeseries(" + COLLECTIONORALIAS + ", q=\"*:*\", start=\"2013-01-01T01:00:00.000Z\", " + "end=\"2016-12-01T01:00:00.000Z\", " + "gap=\"+1YEAR\", " + "field=\"test_dt\", " + "format=\"yyyy\", " + "count(*), sum(price_f), max(price_f), min(price_f))";
paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
solrStream = new SolrStream(url, paramsLoc);
solrStream.setStreamContext(context);
tuples = getTuples(solrStream);
assertTrue(tuples.size() == 4);
assertTrue(tuples.get(0).get("test_dt").equals("2013"));
assertTrue(tuples.get(0).getLong("count(*)").equals(100L));
assertTrue(tuples.get(0).getDouble("sum(price_f)").equals(10000D));
assertTrue(tuples.get(0).getDouble("max(price_f)").equals(100D));
assertTrue(tuples.get(0).getDouble("min(price_f)").equals(100D));
assertTrue(tuples.get(1).get("test_dt").equals("2014"));
assertTrue(tuples.get(1).getLong("count(*)").equals(50L));
assertTrue(tuples.get(1).getDouble("sum(price_f)").equals(25000D));
assertTrue(tuples.get(1).getDouble("max(price_f)").equals(500D));
assertTrue(tuples.get(1).getDouble("min(price_f)").equals(500D));
assertTrue(tuples.get(2).get("test_dt").equals("2015"));
assertTrue(tuples.get(2).getLong("count(*)").equals(50L));
assertTrue(tuples.get(2).getDouble("sum(price_f)").equals(15000D));
assertTrue(tuples.get(2).getDouble("max(price_f)").equals(300D));
assertTrue(tuples.get(2).getDouble("min(price_f)").equals(300D));
assertTrue(tuples.get(3).get("test_dt").equals("2016"));
assertTrue(tuples.get(3).getLong("count(*)").equals(50L));
assertTrue(tuples.get(3).getDouble("sum(price_f)").equals(20000D));
assertTrue(tuples.get(3).getDouble("max(price_f)").equals(400D));
assertTrue(tuples.get(3).getDouble("min(price_f)").equals(400D));
expr = "timeseries(" + COLLECTIONORALIAS + ", q=\"*:*\", start=\"2013-01-01T01:00:00.000Z\", " + "end=\"2016-12-01T01:00:00.000Z\", " + "gap=\"+1YEAR\", " + "field=\"test_dt\", " + "format=\"yyyy-MM\", " + "count(*), sum(price_f), max(price_f), min(price_f))";
paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
solrStream = new SolrStream(url, paramsLoc);
solrStream.setStreamContext(context);
tuples = getTuples(solrStream);
assertTrue(tuples.size() == 4);
assertTrue(tuples.get(0).get("test_dt").equals("2013-01"));
assertTrue(tuples.get(0).getLong("count(*)").equals(100L));
assertTrue(tuples.get(0).getDouble("sum(price_f)").equals(10000D));
assertTrue(tuples.get(0).getDouble("max(price_f)").equals(100D));
assertTrue(tuples.get(0).getDouble("min(price_f)").equals(100D));
assertTrue(tuples.get(1).get("test_dt").equals("2014-01"));
assertTrue(tuples.get(1).getLong("count(*)").equals(50L));
assertTrue(tuples.get(1).getDouble("sum(price_f)").equals(25000D));
assertTrue(tuples.get(1).getDouble("max(price_f)").equals(500D));
assertTrue(tuples.get(1).getDouble("min(price_f)").equals(500D));
assertTrue(tuples.get(2).get("test_dt").equals("2015-01"));
assertTrue(tuples.get(2).getLong("count(*)").equals(50L));
assertTrue(tuples.get(2).getDouble("sum(price_f)").equals(15000D));
assertTrue(tuples.get(2).getDouble("max(price_f)").equals(300D));
assertTrue(tuples.get(2).getDouble("min(price_f)").equals(300D));
assertTrue(tuples.get(3).get("test_dt").equals("2016-01"));
assertTrue(tuples.get(3).getLong("count(*)").equals(50L));
assertTrue(tuples.get(3).getDouble("sum(price_f)").equals(20000D));
assertTrue(tuples.get(3).getDouble("max(price_f)").equals(400D));
assertTrue(tuples.get(3).getDouble("min(price_f)").equals(400D));
}
use of org.apache.solr.client.solrj.io.Tuple in project lucene-solr by apache.
the class StreamExpressionTest method assertTopicRun.
private void assertTopicRun(TupleStream stream, String... idArray) throws Exception {
long version = -1;
int count = 0;
List<String> ids = new ArrayList();
for (String id : idArray) {
ids.add(id);
}
try {
stream.open();
while (true) {
Tuple tuple = stream.read();
if (tuple.EOF) {
break;
} else {
++count;
String id = tuple.getString("id");
if (!ids.contains(id)) {
throw new Exception("Expecting id in topic run not found:" + id);
}
long v = tuple.getLong("_version_");
if (v < version) {
throw new Exception("Out of order version in topic run:" + v);
}
}
}
} finally {
stream.close();
}
if (count != ids.size()) {
throw new Exception("Wrong count in topic run:" + count);
}
}
use of org.apache.solr.client.solrj.io.Tuple in project lucene-solr by apache.
the class StreamExpressionTest method testCopyOfRange.
public void testCopyOfRange() throws Exception {
UpdateRequest updateRequest = new UpdateRequest();
int i = 0;
while (i < 50) {
updateRequest.add(id, "id_" + (++i), "test_dt", getDateString("2016", "5", "1"), "price_f", "400.00");
}
while (i < 100) {
updateRequest.add(id, "id_" + (++i), "test_dt", getDateString("2015", "5", "1"), "price_f", "300.0");
}
while (i < 150) {
updateRequest.add(id, "id_" + (++i), "test_dt", getDateString("2014", "5", "1"), "price_f", "500.0");
}
while (i < 250) {
updateRequest.add(id, "id_" + (++i), "test_dt", getDateString("2013", "5", "1"), "price_f", "100.00");
}
updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
String expr = "timeseries(" + COLLECTIONORALIAS + ", q=\"*:*\", start=\"2013-01-01T01:00:00.000Z\", " + "end=\"2016-12-01T01:00:00.000Z\", " + "gap=\"+1YEAR\", " + "field=\"test_dt\", " + "count(*), sum(price_f), max(price_f), min(price_f))";
String cexpr = "let(a=" + expr + ", c=col(a, max(price_f)), tuple(copy=copyOfRange(c, 1, 3)))";
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", cexpr);
paramsLoc.set("qt", "/stream");
String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString() + "/" + COLLECTIONORALIAS;
TupleStream solrStream = new SolrStream(url, paramsLoc);
StreamContext context = new StreamContext();
solrStream.setStreamContext(context);
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
List<Number> copy1 = (List<Number>) tuples.get(0).get("copy");
assertTrue(copy1.size() == 2);
assertTrue(copy1.get(0).doubleValue() == 500D);
assertTrue(copy1.get(1).doubleValue() == 300D);
}
use of org.apache.solr.client.solrj.io.Tuple in project lucene-solr by apache.
the class StreamExpressionTest method testConvertEvaluator.
@Test
public void testConvertEvaluator() throws Exception {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.add(id, "1", "miles_i", "50");
updateRequest.add(id, "2", "miles_i", "70");
updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
//Test annotating tuple
String expr = "select(calc(), convert(miles, kilometers, 10) as kilometers)";
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString() + "/" + COLLECTIONORALIAS;
TupleStream solrStream = new SolrStream(url, paramsLoc);
StreamContext context = new StreamContext();
solrStream.setStreamContext(context);
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
double d = (double) tuples.get(0).get("kilometers");
assertTrue(d == (double) (10 * 1.61));
expr = "select(search(" + COLLECTIONORALIAS + ", q=\"*:*\", sort=\"miles_i asc\", fl=\"miles_i\"), convert(miles, kilometers, miles_i) as kilometers)";
paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
solrStream = new SolrStream(url, paramsLoc);
context = new StreamContext();
solrStream.setStreamContext(context);
tuples = getTuples(solrStream);
assertTrue(tuples.size() == 2);
d = (double) tuples.get(0).get("kilometers");
assertTrue(d == (double) (50 * 1.61));
d = (double) tuples.get(1).get("kilometers");
assertTrue(d == (double) (70 * 1.61));
expr = "parallel(" + COLLECTIONORALIAS + ", workers=2, sort=\"miles_i asc\", select(search(" + COLLECTIONORALIAS + ", q=\"*:*\", partitionKeys=miles_i, sort=\"miles_i asc\", fl=\"miles_i\"), convert(miles, kilometers, miles_i) as kilometers))";
paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
solrStream = new SolrStream(url, paramsLoc);
context = new StreamContext();
solrStream.setStreamContext(context);
tuples = getTuples(solrStream);
assertTrue(tuples.size() == 2);
d = (double) tuples.get(0).get("kilometers");
assertTrue(d == (double) (50 * 1.61));
d = (double) tuples.get(1).get("kilometers");
assertTrue(d == (double) (70 * 1.61));
expr = "select(stats(" + COLLECTIONORALIAS + ", q=\"*:*\", sum(miles_i)), convert(miles, kilometers, sum(miles_i)) as kilometers)";
paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", expr);
paramsLoc.set("qt", "/stream");
solrStream = new SolrStream(url, paramsLoc);
context = new StreamContext();
solrStream.setStreamContext(context);
tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
d = (double) tuples.get(0).get("kilometers");
assertTrue(d == (double) (120 * 1.61));
}
use of org.apache.solr.client.solrj.io.Tuple in project lucene-solr by apache.
the class StreamExpressionTest method testParallelShuffleStream.
@Test
public void testParallelShuffleStream() throws Exception {
new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "0").add(id, "2", "a_s", "hello2", "a_i", "2", "a_f", "0").add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3").add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4").add(id, "1", "a_s", "hello1", "a_i", "1", "a_f", "1").add(id, "5", "a_s", "hello1", "a_i", "10", "a_f", "1").add(id, "6", "a_s", "hello1", "a_i", "11", "a_f", "5").add(id, "7", "a_s", "hello1", "a_i", "12", "a_f", "5").add(id, "8", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "9", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "10", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "11", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "12", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "13", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "14", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "15", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "16", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "17", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "18", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "19", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "20", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "21", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "22", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "23", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "24", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "25", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "26", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "27", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "28", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "29", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "30", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "31", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "32", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "33", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "34", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "35", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "36", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "37", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "38", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "39", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "40", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "41", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "42", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "43", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "44", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "45", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "46", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "47", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "48", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "49", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "50", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "51", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "52", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "53", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "54", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "55", "a_s", "hello1", "a_i", "13", "a_f", "4").add(id, "56", "a_s", "hello1", "a_i", "13", "a_f", "1000").commit(cluster.getSolrClient(), COLLECTIONORALIAS);
StreamContext streamContext = new StreamContext();
SolrClientCache solrClientCache = new SolrClientCache();
streamContext.setSolrClientCache(solrClientCache);
String zkHost = cluster.getZkServer().getZkAddress();
StreamFactory streamFactory = new StreamFactory().withCollectionZkHost(COLLECTIONORALIAS, zkHost).withFunctionName("shuffle", ShuffleStream.class).withFunctionName("unique", UniqueStream.class).withFunctionName("parallel", ParallelStream.class);
try {
ParallelStream pstream = (ParallelStream) streamFactory.constructStream("parallel(" + COLLECTIONORALIAS + ", unique(shuffle(collection1, q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc, a_i asc\", partitionKeys=\"a_f\"), over=\"a_f\"), workers=\"2\", zkHost=\"" + zkHost + "\", sort=\"a_f asc\")");
pstream.setStreamFactory(streamFactory);
pstream.setStreamContext(streamContext);
List<Tuple> tuples = getTuples(pstream);
assert (tuples.size() == 6);
assertOrder(tuples, 0, 1, 3, 4, 6, 56);
//Test the eofTuples
Map<String, Tuple> eofTuples = pstream.getEofTuples();
//There should be an EOF tuple for each worker.
assert (eofTuples.size() == 2);
assert (pstream.toExpression(streamFactory).toString().contains("shuffle"));
} finally {
solrClientCache.close();
}
}
Aggregations