forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-2318] Union can be used as BroadcastVariable
This closes apache#1390
- Loading branch information
Showing
3 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
flink-tests/src/test/java/org/apache/flink/test/broadcastvars/BroadcastUnionITCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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:https://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.flink.test.broadcastvars; | ||
|
||
import java.util.List; | ||
import org.apache.flink.api.common.functions.ReduceFunction; | ||
import org.apache.flink.api.common.functions.RichMapFunction; | ||
import org.apache.flink.api.java.DataSet; | ||
import org.apache.flink.api.java.ExecutionEnvironment; | ||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.test.util.JavaProgramTestBase; | ||
import org.junit.Assert; | ||
|
||
public class BroadcastUnionITCase extends JavaProgramTestBase { | ||
private static final String BC_NAME = "bc"; | ||
|
||
@Override | ||
protected void testProgram() throws Exception { | ||
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); | ||
env.setParallelism(4); | ||
|
||
DataSet<Long> input = env.generateSequence(1, 10); | ||
DataSet<Long> bc1 = env.generateSequence(1, 5); | ||
DataSet<Long> bc2 = env.generateSequence(6, 10); | ||
|
||
List<Long> result = input | ||
.map(new Mapper()) | ||
.withBroadcastSet(bc1.union(bc2), BC_NAME) | ||
.reduce(new Reducer()) | ||
.collect(); | ||
|
||
Assert.assertEquals(result.get(0), Long.valueOf(3025)); | ||
} | ||
|
||
public static class Mapper extends RichMapFunction<Long, Long> { | ||
private List<Long> values; | ||
|
||
@Override | ||
public void open(Configuration config) { | ||
values = getRuntimeContext().getBroadcastVariable(BC_NAME); | ||
} | ||
|
||
@Override | ||
public Long map(Long value) throws Exception { | ||
long sum = 0; | ||
for (Long v : values) { | ||
sum += value * v; | ||
} | ||
return sum; | ||
} | ||
} | ||
|
||
public static class Reducer implements ReduceFunction<Long> { | ||
@Override | ||
public Long reduce(Long value1, Long value2) throws Exception { | ||
return value1 + value2; | ||
} | ||
} | ||
} |