Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.druid.query.movingaverage;
import org.apache.druid.data.input.MapBasedRow;
import org.apache.druid.data.input.Row;
import org.apache.druid.java.util.common.guava.Sequence;
import org.apache.druid.java.util.common.guava.Yielder;
import org.apache.druid.java.util.common.guava.Yielders;
import org.apache.druid.query.aggregation.Aggregator;
import org.apache.druid.query.aggregation.AggregatorFactory;
import org.apache.druid.query.aggregation.PostAggregator;
import org.apache.druid.query.dimension.DimensionSpec;
import org.apache.druid.query.movingaverage.averagers.Averager;
import org.apache.druid.query.movingaverage.averagers.AveragerFactory;
import org.apache.druid.segment.ColumnSelectorFactory;
import org.apache.druid.segment.ColumnValueSelector;
import org.apache.druid.segment.DimensionSelector;
import org.apache.druid.segment.NilColumnValueSelector;
import org.apache.druid.segment.column.ColumnCapabilities;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;
/**
* {@link MovingAverageIterable} iterates over days {@link RowBucket}, producing rows for each dimension combination,
* filling in missing entries with "empty" rows so that the averaging buckets have enough data to operate on.
* It then computes the moving average on the buckets and returns the row.
* See computeMovingAverage for more details.
*/
public class MovingAverageIterable implements Iterable
{
private final Sequence seq;
private final List dims;
private final List> factories;
private final Map postAggMap;
private final Map aggMap;
private final Map emptyEvents;
public MovingAverageIterable(
Sequence buckets,
List dims,
List> factories,
List postAggList,
List aggList
)
{
this.dims = dims;
this.factories = factories;
this.seq = buckets;
postAggMap = postAggList.stream().collect(Collectors.toMap(postAgg -> postAgg.getName(), postAgg -> postAgg));
aggMap = aggList.stream().collect(Collectors.toMap(agg -> agg.getName(), agg -> agg));
emptyEvents = generateEmptyEventsFromAggregators(aggMap, postAggMap);
}
// Build a list of empty events from Aggregators/PostAggregators to be used by Iterator to build fake rows.
// These fake rows will be used by computeMovingAverage() in skip=true mode.
// See emptyEventsCopy in internalNext() and computeMovingAverage() documentation.
private Map generateEmptyEventsFromAggregators(Map aggMap,
Map postAggMap)
{
Map emptyEvents = new LinkedHashMap<>();
aggMap.values().forEach(agg -> {
Aggregator aggFactorized = agg.factorize(getEmptyColumnSelectorFactory());
emptyEvents.put(agg.getName(), aggFactorized.get());
});
postAggMap.values().forEach(postAgg -> emptyEvents.put(postAgg.getName(), postAgg.compute(emptyEvents)));
return emptyEvents;
}
@Nonnull
private ColumnSelectorFactory getEmptyColumnSelectorFactory()
{
return new ColumnSelectorFactory()
{
@Override
public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec)
{
// Generating empty records while aggregating on Filtered aggregators requires a dimension selector
// for initialization. This dimension selector is not actually used for generating values
return DimensionSelector.constant(null);
}
@Override
public ColumnValueSelector makeColumnValueSelector(String s)
{
return NilColumnValueSelector.instance();
}
@Override
public ColumnCapabilities getColumnCapabilities(String s)
{
return null;
}
};
}
/* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator iterator()
{
return new MovingAverageIterator(seq, dims, factories, emptyEvents, aggMap);
}
static class MovingAverageIterator implements Iterator
{
private final List dims;
// Key: Row's dimension set. Value: Averager. See MovingAverageIterator#computeMovingAverage for more details.
private final Map