本文整理匯總了Java中org.apache.flink.util.Collector.collect方法的典型用法代碼示例。如果您正苦於以下問題:Java Collector.collect方法的具體用法?Java Collector.collect怎麽用?Java Collector.collect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.flink.util.Collector
的用法示例。
在下文中一共展示了Collector.collect方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: flatMap
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
@Override
public void flatMap(String tweetJsonStr, Collector<Tuple2<String, Integer>> collector) throws Exception {
JsonNode tweetJson = mapper.readTree(tweetJsonStr);
JsonNode entities = tweetJson.get("entities");
if (entities == null) return;
JsonNode hashtags = entities.get("hashtags");
if (hashtags == null) return;
for (Iterator<JsonNode> iter = hashtags.getElements(); iter.hasNext();) {
JsonNode node = iter.next();
String hashtag = node.get("text").getTextValue();
if (hashtag.matches("\\w+")) {
collector.collect(new Tuple2<>(hashtag, 1));
}
}
}
示例2: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
/**
* @param key the key to calculate by (address or id in this section)
* @param window the time window
* @param lamps the Iterable<{@link StreetLamp}>
* @param collector the collector to handle the hand off between streams
* @throws Exception
*/
@Override
public void apply(String key, TimeWindow window, Iterable<StreetLamp> lamps, Collector<LampEMAConsumptionStreet> collector) throws Exception {
// creating lamp consumption
LampEMAConsumptionStreet streetConsumption = new LampEMAConsumptionStreet();
streetConsumption.setAddress(key);
streetConsumption.setComputed(Instant.now().getEpochSecond());
// iterating over lamps in window
for (Object lamp : lamps) {
if (streetConsumption.getConsumption() == 0) {
// initializing average
streetConsumption.setConsumption(((StreetLamp) lamp).getConsumption());
} else {
// using EMA to calculate average on the selected window
// the formula is: S_t = alpha * Y_t + (1 - alpha) * S_t-1
streetConsumption.setConsumption(ALPHA * ((StreetLamp) lamp).getConsumption() + (1 - ALPHA) * streetConsumption.getConsumption());
}
}
// returning the consumption
collector.collect(streetConsumption);
}
示例3: reduce
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
@Override
public void reduce(Iterable<Tuple4<Long, Integer, String, TagEvent>> values, Collector<ChartsResult> out) throws Exception {
int counter = 0;
String group= "";
log.info("Reducing Groups and applyting limit(" + limit + ") by field " + groupPosition);
for (Tuple4<Long, Integer, String, TagEvent> t : values) {
if (!t.getField(groupPosition).equals(group)) {
counter= 0;
group= t.f2;
}
if (counter < limit) {
out.collect(new ChartsResult(t.f0, t.f1, t.f3));
}
counter++;
}
}
示例4: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
/**
* Evaluates the window and outputs none or several elements.
*
* @param window The window that is being evaluated.
* @param values The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(TimeWindow window, Iterable<WindowWordWithCount> values, Collector<WindowWordRanking> out) throws Exception {
this.ranking.setWStart(window.getStart());
this.ranking.setWEnd(window.getEnd());
List<WindowWordWithCount> rank = this.ranking.getRank();
rank.clear();
values.forEach(rank::add);
rank.sort((e1,e2) -> Long.compare(e2.getCount(), e1.getCount()));
int size = rank.size();
rank.subList(Math.min(this.rankSize, size), size).clear();
LOG.debug("WOUT: {}", this.ranking);
out.collect(this.ranking);
}
示例5: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
/**
* Evaluates the window and outputs none or several elements.
*
* @param window The window that is being evaluated.
* @param values The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(TimeWindow window, Iterable<PlayerSpeedStatistics> values, Collector<PlayersSpeedRanking> out) throws Exception {
this.ranking.setTsStart(window.getStart());
this.ranking.setTsStop(window.getEnd());
List<RankingElement> rank = this.ranking.getRank();
rank.clear();
values.forEach(e -> rank.add(new RankingElement(e.getPid(), e.getAverageSpeed())));
rank.sort(Comparator.reverseOrder());
int size = rank.size();
rank.subList(Math.min(this.rankSize, size), size).clear();
//LOG.debug("WOUT: {}", this.ranking);
out.collect(this.ranking);
}
示例6: flatMap
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
@Override
public void flatMap(Event event, Collector<Event.Alert> collector) throws Exception {
EventStateMachine.State value = state.value();
if(value == null) {
value = EventStateMachine.Transitions.initialState;
}
EventStateMachine.State nextValue = value.transition(event.getEvent());
if(nextValue instanceof EventStateMachine.InvalidTransition) {
Event.Alert alert = new Event.Alert(Instant.now(), event, value);
collector.collect(alert);
state.update(null);
} else if (!nextValue.terminal()){
state.update(nextValue);
} else {
state.update(null);
}
}
示例7: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
@Override
public void apply(TimeWindow window, Iterable<Tuple3<Long, String, String>> values, Collector<Tuple3<String, String, Integer>> out) throws Exception {
Map<String, Integer> collected = new HashMap<String, Integer>();
String lastTimestamp = null;
for (Tuple3<Long, String, String> tuple : values) {
String langTag = tuple._2();
lastTimestamp = tuple._3();
if (collected.get(langTag) == null) {
collected.put(langTag, 1);
} else {
collected.put(langTag, collected.get(langTag) + 1);
}
}
if (lastTimestamp != null) {
for (Map.Entry<String, Integer> entry : collected.entrySet()) {
out.collect(new Tuple3<>(lastTimestamp, entry.getKey(), entry.getValue()));
}
}
}
示例8: combine
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
@Override
public void combine(Iterable<Tuple3<Integer, Integer, String>> values,
Collector<Tuple3<Integer, Double, String>> out)
{
int key = 0;
int sum = 0;
String someString = null;
for (Tuple3<Integer, Integer, String> next : values) {
key = next.f0;
sum += next.f1;
someString = next.f2;
}
out.collect(new Tuple3<Integer, Double, String>(key, (double) sum, someString));
}
示例9: reduce
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
@Override
public void reduce(Iterable<Tuple3<T, T, IntValue>> values, Collector<Result<T>> out)
throws Exception {
int count = 0;
Tuple3<T, T, IntValue> edge = null;
for (Tuple3<T, T, IntValue> next : values) {
edge = next;
count += 1;
}
int distinctNeighbors = edge.f2.getValue() - count;
if (unboundedScores ||
(count * minimumScoreDenominator >= distinctNeighbors * minimumScoreNumerator
&& count * maximumScoreDenominator <= distinctNeighbors * maximumScoreNumerator)) {
output.setVertexId0(edge.f0);
output.setVertexId1(edge.f1);
output.setSharedNeighborCount(count);
output.setDistinctNeighborCount(distinctNeighbors);
out.collect(output);
}
}
示例10: reduce
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
@Override
public void reduce(Iterable<Tuple2<String, String>> values, Collector<Tuple2<String, /*TODO POJO*/String>> out) throws Exception {
coords = "";
for (Tuple2<String,String> entry: values) {
user = entry.f0;
coordSet.add(entry.f1.toString());
}
if(coordSet.size() > 2){
coords = coordSet.first();
coordSet.remove(coordSet.first());
while(coordSet.size() > 0){
coords += "|" + coordSet.first();
coordSet.remove(coordSet.first());
}
out.collect(new Tuple2<String,String>(user,coords));
}
}
示例11: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
@Override
public void apply(TimeWindow window, Iterable<Tuple2<String, Integer>> hashTags, Collector<TweetsCount> out) throws Exception {
Tuple2<String, Integer> topHashTag = new Tuple2<>("", 0);
for (Tuple2<String, Integer> hashTag : hashTags) {
if (hashTag.f1 > topHashTag.f1) {
topHashTag = hashTag;
}
}
out.collect(new TweetsCount(window.getStart(), window.getEnd(), topHashTag.f0, topHashTag.f1));
}
示例12: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
/**
* @param key the key to calculate by (address in this section)
* @param window the sliding window
* @param trafficData the Iterable<{@link TrafficData}>
* @param collector the collector to handle the hand off between streams
* @throws Exception
*/
@Override
public void apply(String key, TimeWindow window, Iterable<TrafficData> trafficData,
Collector<Tuple2<String, Float>> collector) throws Exception {
// iterating over ambient levels
float ambientLevel = 0;
int sensorsTotal = 0;
for (TrafficData traffic : trafficData) {
ambientLevel += traffic.getIntensity();
sensorsTotal += 1;
}
collector.collect(new Tuple2<>(key, ambientLevel / sensorsTotal));
}
示例13: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
/**
* Evaluates the window and outputs none or several elements.
*
* @param key The key for which this window is evaluated.
* @param window The window that is being evaluated.
* @param input The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(String key, TimeWindow window, Iterable<WindowWordWithCount> input, Collector<WindowWordWithCount> out) throws Exception {
WindowWordWithCount value = input.iterator().next();
value.setWStart(window.getStart());
value.setWEnd(window.getEnd());
value.setWord(key);
LOG.debug("WOUT: {}", value);
out.collect(value);
}
示例14: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
/**
* Evaluates the window and outputs none or several elements.
*
* @param key The key for which this window is evaluated.
* @param window The window that is being evaluated.
* @param inputs The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(Long key, TimeWindow window, Iterable<PlayerSpeedStatistics> inputs, Collector<PlayerSpeedStatistics> out) throws Exception {
PlayerSpeedStatistics stats = inputs.iterator().next();
stats.setPid(key);
stats.setTsStart(window.getStart());
stats.setTsStop(window.getEnd());
//LOG.info("OUT: {}", stats);
out.collect(stats);
}
示例15: apply
import org.apache.flink.util.Collector; //導入方法依賴的package包/類
/**
* Evaluates the window and outputs none or several elements.
*
* @param key The key for which this window is evaluated.
* @param window The window that is being evaluated.
* @param inputs The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(Long key, TimeWindow window, Iterable<PlayerGridStatistics> inputs, Collector<PlayerGridStatistics> out) throws Exception {
PlayerGridStatistics stats = inputs.iterator().next();
stats.setPid(key);
stats.setTsStart(window.getStart());
//LOG.info("OUT: {}", stats);
out.collect(stats);
}