|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 QAware GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package de.qaware.chronix.examples.server; |
| 17 | + |
| 18 | +import de.qaware.chronix.ChronixClient; |
| 19 | +import de.qaware.chronix.converter.KassiopeiaConverter; |
| 20 | +import de.qaware.chronix.solr.client.ChronixSolrStorage; |
| 21 | +import de.qaware.chronix.timeseries.TimeSeries; |
| 22 | +import org.apache.solr.client.solrj.SolrClient; |
| 23 | +import org.apache.solr.client.solrj.SolrQuery; |
| 24 | +import org.apache.solr.client.solrj.impl.HttpSolrClient; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | +import java.util.function.BinaryOperator; |
| 30 | +import java.util.function.Function; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +import static de.qaware.chronix.timeseries.TimeSeries.merge; |
| 34 | + |
| 35 | +/** |
| 36 | + * An example showcase of how to integrate chronix into your application. |
| 37 | + * Works with the release 0.1.1 of the chronix-server |
| 38 | + * Download at <a href="https://github.com/ChronixDB/chronix.server/releases/download/v0.1/chronix-0.1.zip">chronix-server-0.1.1</a> |
| 39 | + * Note: The example data stored in the release |
| 40 | + * |
| 41 | + * @author f.lautenschlager |
| 42 | + */ |
| 43 | +public class ChronixClientExampleWithKassiopeia { |
| 44 | + |
| 45 | + private static final Logger LOGGER = LoggerFactory.getLogger(ChronixClientExampleWithKassiopeia.class); |
| 46 | + |
| 47 | + public static void main(String[] args) { |
| 48 | + SolrClient solr = new HttpSolrClient("http://localhost:8983/solr/chronix/"); |
| 49 | + |
| 50 | + //Define a group by function for the time series records |
| 51 | + Function<TimeSeries<Long, Double>, String> groupBy = ts -> ts.getAttribute("metric") + "-" + ts.getAttribute("host"); |
| 52 | + |
| 53 | + //Define a reduce function for the grouped time series records. We use the average. |
| 54 | + BinaryOperator<TimeSeries<Long, Double>> reduce = (ts1, ts2) -> merge(ts1, ts2, (y1, y2) -> (y1 + y2) / 2); |
| 55 | + |
| 56 | + //Instantiate a Chronix Client |
| 57 | + ChronixClient<TimeSeries<Long, Double>, SolrClient, SolrQuery> chronix = new ChronixClient<>( |
| 58 | + new KassiopeiaConverter(), new ChronixSolrStorage<>(200, groupBy, reduce)); |
| 59 | + |
| 60 | + //We want the maximum of all time series that metric matches *load*. |
| 61 | + SolrQuery query = new SolrQuery("metric:*Load*"); |
| 62 | + query.addFilterQuery("ag=max"); |
| 63 | + |
| 64 | + //The result is a Java Stream. We simply collect the result into a list. |
| 65 | + List<TimeSeries<Long, Double>> maxTS = chronix.stream(solr, query).collect(Collectors.toList()); |
| 66 | + |
| 67 | + //Just print it out. |
| 68 | + LOGGER.info("Result for query {} is: {}", query, maxTS); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +} |
0 commit comments