本文整理匯總了Java中org.apache.flink.api.java.tuple.Tuple2類的典型用法代碼示例。如果您正苦於以下問題:Java Tuple2類的具體用法?Java Tuple2怎麽用?Java Tuple2使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Tuple2類屬於org.apache.flink.api.java.tuple包,在下文中一共展示了Tuple2類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: flatMap
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的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: restoreState
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Override
public void restoreState(List<Tuple2<Integer, BitSet>> state) throws Exception {
if (state.isEmpty()) {
Assert.fail("Function was restored without state - no checkpoint completed before.");
}
if (state.size() > 1) {
Assert.fail("Function was restored with multiple states. unexpected scale-in");
}
Tuple2<Integer, BitSet> s = state.get(0);
this.numElementsSoFar = s.f0;
this.duplicateChecker.clear();
this.duplicateChecker.or(s.f1);
log.debug("IntSequenceExactlyOnceValidator was restored with {} elements", numElementsSoFar);
}
示例3: map
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Override
public StreamState map(Tuple2<String, Message> input) throws Exception {
StreamState currentState = internalState.value();
if(currentState == null)
currentState = new StreamState(input.f1.getId());
currentState.appendMessage(input.f1);
StreamState state = null;
if(currentState.size() >= input.f1.getValues()) {
currentState.isReadyForReduction = true;
state = currentState;
internalState.clear();
} else {
internalState.update(currentState);
}
return state;
}
示例4: randomTransition
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
public Tuple2<Event.EventType, State> randomTransition(Random rnd) {
if (transitions.isEmpty()) {
throw new RuntimeException("Cannot transition from state " + name);
}
float p = rnd.nextFloat();
float mass = 0.0f;
Transition transition = null;
for (Transition t : transitions) {
mass += t.prob;
if (transition == null && p <= mass) {
transition = t;
}
}
return new Tuple2<>(transition.event, transition.targetState);
}
示例5: main
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
// set up the execution environment
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
// get input data
DataSet<String> text = env.fromElements(
"To be, or not to be,--that is the question:--",
"Whether 'tis nobler in the mind to suffer",
"The slings and arrows of outrageous fortune",
"Or to take arms against a sea of troubles,"
);
DataSet<Tuple2<String, Integer>> counts =
// split up the lines in pairs (2-tuples) containing: (word,1)
text.flatMap(new LineSplitter())
// group by the tuple field "0" and sum up tuple field "1"
.groupBy(0)
.sum(1);
// execute and print result
counts.print();
}
示例6: map
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Override
public Tuple2<Integer, Boolean> map(TaxiRide taxiRide) throws Exception {
float lon;
float lat;
final boolean isStart = taxiRide.isStart;
if(isStart) {
lon = taxiRide.startLon;
lat = taxiRide.startLat;
}
else {
lon = taxiRide.endLon;
lat = taxiRide.endLat;
}
int gridId = GeoUtils.mapToGridCell(lon, lat);
return Tuple2.of(gridId, isStart);
}
示例7: snapshotState
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Override
public List<Tuple2<Integer, BitSet>> snapshotState(long checkpointId, long timestamp) throws Exception {
log.info("IntSequenceExactlyOnceValidator - at checkpoint {} having {} elements, waitUntil {}",
checkpointId, numElementsSoFar, waitUntil);
// after we are done, we need to wait for two more checkpoint to complete
// before finishing the program - that is to be on the safe side that
// the sink also got the "commit" notification for all relevant checkpoints
// and committed the data to pravega
if (numElementsSoFar == numElementsTotal) {
waitUntil--;
if (waitUntil == 0) {
throw new SuccessException();
}
}
return Collections.singletonList(new Tuple2<>(numElementsSoFar, duplicateChecker));
}
示例8: restoreState
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Override
public void restoreState(List<Tuple2<Integer, BitSet>> state) throws Exception {
if (state.isEmpty()) {
Assert.fail("Function was restored without state - no checkpoint completed before.");
}
if (state.size() > 1) {
Assert.fail("Function was restored with multiple states. unexpected scale-in");
}
Tuple2<Integer, BitSet> s = state.get(0);
this.numElementsSoFar = s.f0;
this.duplicateChecker.clear();
this.duplicateChecker.or(s.f1);
log.info("IntSequenceExactlyOnceValidator was restored with {} elements", numElementsSoFar);
}
示例9: main
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<Tuple2<String, Integer>> source = env.fromCollection(collection);
CassandraSink.addSink(source)
.setQuery(INSERT)
.setClusterBuilder(new ClusterBuilder() {
@Override
protected Cluster buildCluster(Builder builder) {
return builder.addContactPoint("127.0.0.1").build();
}
})
.build();
env.execute("WriteTupleIntoCassandra");
}
示例10: flatMap
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Override
public void flatMap(Tuple2<Long, String> tweet, Collector<Tuple3<Long, String, Float>> out) throws Exception {
String text = tweet.f1;
Set<String> posWords = PositiveWords.getWords();
String[] words = text.split(" ");
int numWords = words.length;
int numPosWords = 0;
for (String word : words) {
if (posWords.contains(word))
numPosWords++;
}
out.collect(new Tuple3<>(
tweet.f0,
tweet.f1,
(float) numPosWords / numWords));
}
示例11: testCorrectnessOfCrossWithHuge
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Test
public void testCorrectnessOfCrossWithHuge() throws Exception {
/*
* check correctness of crossWithHuge (only correctness of result -> should be the same as with normal cross)
*/
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<Tuple5<Integer, Long, Integer, String, Long>> ds = CollectionDataSets.getSmall5TupleDataSet(env);
DataSet<Tuple5<Integer, Long, Integer, String, Long>> ds2 = CollectionDataSets.getSmall5TupleDataSet(env);
DataSet<Tuple2<Integer, String>> crossDs = ds.crossWithHuge(ds2).with(new Tuple5Cross());
List<Tuple2<Integer, String>> result = crossDs.collect();
String expected = "0,HalloHallo\n" +
"1,HalloHallo Welt\n" +
"2,HalloHallo Welt wie\n" +
"1,Hallo WeltHallo\n" +
"2,Hallo WeltHallo Welt\n" +
"3,Hallo WeltHallo Welt wie\n" +
"2,Hallo Welt wieHallo\n" +
"3,Hallo Welt wieHallo Welt\n" +
"4,Hallo Welt wieHallo Welt wie\n";
compareResultAsTuples(result, expected);
}
示例12: processElement
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Override
public void processElement(
String value,
Context ctx,
Collector<Tuple2<String, Integer>> out) throws Exception {
// normalize and split the line
String[] tokens = value.toLowerCase().split("\\W+");
// emit the pairs
for (String token : tokens) {
if (token.length() > 5) {
ctx.output(rejectedWordsTag, token);
} else if (token.length() > 0) {
out.collect(new Tuple2<>(token, 1));
}
}
}
示例13: testAreCompatible4
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Test
public void testAreCompatible4() throws Keys.IncompatibleKeysException {
TypeInformation<Tuple3<String, Long, Integer>> t1 = new TupleTypeInfo<>(
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.LONG_TYPE_INFO,
BasicTypeInfo.INT_TYPE_INFO
);
TypeInformation<PojoWithMultiplePojos> t2 = TypeExtractor.getForClass(PojoWithMultiplePojos.class);
Keys.ExpressionKeys<Tuple3<String, Long, Integer>> ek1 = new Keys.ExpressionKeys<>(new int[]{2,0}, t1);
Keys<PojoWithMultiplePojos> sk2 = new Keys.SelectorFunctionKeys<>(
new KeySelector3(),
t2,
new TupleTypeInfo<Tuple2<Integer, String>>(BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO)
);
Assert.assertTrue(sk2.areCompatible(ek1));
}
示例14: createFileAndFillWithData
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
/**
* Create a file with pre-determined String format of the form:
* {@code fileIdx +": "+ sampleLine +" "+ lineNo}.
* */
private static Tuple2<org.apache.hadoop.fs.Path, String> createFileAndFillWithData(
String base, String fileName, int fileIdx, String sampleLine) throws IOException {
assert (hdfs != null);
final String fileRandSuffix = UUID.randomUUID().toString();
org.apache.hadoop.fs.Path file = new org.apache.hadoop.fs.Path(base + "/" + fileName + fileRandSuffix);
Assert.assertFalse(hdfs.exists(file));
org.apache.hadoop.fs.Path tmp = new org.apache.hadoop.fs.Path(base + "/." + fileName + fileRandSuffix);
FSDataOutputStream stream = hdfs.create(tmp);
StringBuilder str = new StringBuilder();
for (int i = 0; i < LINES_PER_FILE; i++) {
String line = fileIdx + ": " + sampleLine + " " + i + "\n";
str.append(line);
stream.write(line.getBytes(ConfigConstants.DEFAULT_CHARSET));
}
stream.close();
hdfs.rename(tmp, file);
Assert.assertTrue("No result file present", hdfs.exists(file));
return new Tuple2<>(file, str.toString());
}
示例15: testSumOfInNeighborsNoValue
import org.apache.flink.api.java.tuple.Tuple2; //導入依賴的package包/類
@Test
public void testSumOfInNeighborsNoValue() throws Exception {
/*
* Get the sum of in-neighbor values
* times the edge weights for each vertex
*/
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
TestGraphUtils.getLongLongEdgeData(env), env);
DataSet<Tuple2<Long, Long>> verticesWithSum =
graph.groupReduceOnNeighbors(new SumInNeighborsNoValue(), EdgeDirection.IN);
List<Tuple2<Long, Long>> result = verticesWithSum.collect();
expectedResult = "1,255\n" +
"2,12\n" +
"3,59\n" +
"4,102\n" +
"5,285\n";
compareResultAsTuples(result, expectedResult);
}