use of com.hazelcast.function.ToLongFunctionEx in project hazelcast by hazelcast.
the class CreateDagVisitor method onSlidingWindowAggregate.
public Vertex onSlidingWindowAggregate(SlidingWindowAggregatePhysicalRel rel) {
FunctionEx<JetSqlRow, ?> groupKeyFn = rel.groupKeyFn();
AggregateOperation<?, JetSqlRow> aggregateOperation = rel.aggrOp();
Expression<?> timestampExpression = rel.timestampExpression();
ToLongFunctionEx<JetSqlRow> timestampFn = row -> WindowUtils.extractMillis(timestampExpression.eval(row.getRow(), MOCK_EEC));
SlidingWindowPolicy windowPolicy = rel.windowPolicyProvider().apply(MOCK_EEC);
KeyedWindowResultFunction<? super Object, ? super JetSqlRow, ?> resultMapping = rel.outputValueMapping();
if (rel.numStages() == 1) {
Vertex vertex = dag.newUniqueVertex("Sliding-Window-AggregateByKey", Processors.aggregateToSlidingWindowP(singletonList(groupKeyFn), singletonList(timestampFn), TimestampKind.EVENT, windowPolicy, 0, aggregateOperation, resultMapping));
connectInput(rel.getInput(), vertex, edge -> edge.distributeTo(localMemberAddress).allToOne(""));
return vertex;
} else {
assert rel.numStages() == 2;
Vertex vertex1 = dag.newUniqueVertex("Sliding-Window-AccumulateByKey", Processors.accumulateByFrameP(singletonList(groupKeyFn), singletonList(timestampFn), TimestampKind.EVENT, windowPolicy, aggregateOperation));
Vertex vertex2 = dag.newUniqueVertex("Sliding-Window-CombineByKey", Processors.combineToSlidingWindowP(windowPolicy, aggregateOperation, resultMapping));
connectInput(rel.getInput(), vertex1, edge -> edge.partitioned(groupKeyFn));
dag.edge(between(vertex1, vertex2).distributed().partitioned(entryKey()));
return vertex2;
}
}
use of com.hazelcast.function.ToLongFunctionEx in project hazelcast by hazelcast.
the class SourceBuilderTest method stream_distributed_socketSource_withTimestamps.
@Test
public void stream_distributed_socketSource_withTimestamps() throws IOException {
// Given
try (ServerSocket serverSocket = new ServerSocket(0)) {
startServer(serverSocket);
// When
int localPort = serverSocket.getLocalPort();
ToLongFunctionEx<String> timestampFn = line -> Long.valueOf(line.substring(LINE_PREFIX.length()));
BatchSource<String> socketSource = SourceBuilder.batch("socket-source-with-timestamps", ctx -> socketReader(localPort)).<String>fillBufferFn((in, buf) -> {
String line = in.readLine();
if (line != null) {
buf.add(line);
} else {
buf.close();
}
}).destroyFn(BufferedReader::close).distributed(PREFERRED_LOCAL_PARALLELISM).build();
// Then
Pipeline p = Pipeline.create();
p.readFrom(socketSource).addTimestamps(timestampFn, 1000).window(tumbling(1)).aggregate(AggregateOperations.counting()).writeTo(sinkList());
hz().getJet().newJob(p).join();
List<WindowResult<Long>> expected = LongStream.range(1, itemCount + 1).mapToObj(i -> new WindowResult<>(i - 1, i, (long) PREFERRED_LOCAL_PARALLELISM * MEMBER_COUNT)).collect(toList());
assertEquals(expected, new ArrayList<>(sinkList));
}
}
use of com.hazelcast.function.ToLongFunctionEx in project hazelcast by hazelcast.
the class SourceBuilderTest method stream_socketSource_withTimestamps.
@Test
public void stream_socketSource_withTimestamps() throws IOException {
// Given
try (ServerSocket serverSocket = new ServerSocket(0)) {
startServer(serverSocket);
// When
int localPort = serverSocket.getLocalPort();
ToLongFunctionEx<String> timestampFn = line -> Long.valueOf(line.substring(LINE_PREFIX.length()));
BatchSource<String> socketSource = SourceBuilder.batch("socket-source-with-timestamps", ctx -> socketReader(localPort)).<String>fillBufferFn((in, buf) -> {
String line = in.readLine();
if (line != null) {
buf.add(line);
} else {
buf.close();
}
}).destroyFn(BufferedReader::close).build();
// Then
Pipeline p = Pipeline.create();
p.readFrom(socketSource).addTimestamps(timestampFn, 0).window(tumbling(1)).aggregate(AggregateOperations.counting()).writeTo(sinkList());
hz().getJet().newJob(p).join();
List<WindowResult<Long>> expected = LongStream.range(1, itemCount + 1).mapToObj(i -> new WindowResult<>(i - 1, i, 1L)).collect(toList());
assertEquals(expected, new ArrayList<>(sinkList));
}
}
use of com.hazelcast.function.ToLongFunctionEx in project hazelcast by hazelcast.
the class SlidingWindowPTest method before.
@Before
public void before() {
SlidingWindowPolicy winPolicy = slidingWinPolicy(4, 1);
AggregateOperation1<Entry<?, Long>, LongAccumulator, Long> operation = AggregateOperation.withCreate(LongAccumulator::new).andAccumulate((LongAccumulator acc, Entry<?, Long> item) -> acc.add(item.getValue())).andCombine(LongAccumulator::add).andDeduct(hasDeduct ? LongAccumulator::subtract : null).andExportFinish(LongAccumulator::get);
FunctionEx<?, Long> keyFn = t -> KEY;
ToLongFunctionEx<Entry<Long, Long>> timestampFn = Entry::getKey;
SupplierEx<Processor> procSupplier = singleStageProcessor ? aggregateToSlidingWindowP(singletonList(keyFn), singletonList(timestampFn), TimestampKind.EVENT, winPolicy, 0L, operation, KeyedWindowResult::new) : combineToSlidingWindowP(winPolicy, operation, KeyedWindowResult::new);
// new supplier to save the last supplied instance
supplier = () -> lastSuppliedProcessor = (SlidingWindowP) procSupplier.get();
}
use of com.hazelcast.function.ToLongFunctionEx in project hazelcast by hazelcast.
the class SlidingWindowP_twoStageSnapshotTest method before.
@Before
public void before() {
SlidingWindowPolicy windowDef = slidingWinPolicy(4, 1);
AggregateOperation1<Entry<?, Long>, LongAccumulator, Long> aggrOp = AggregateOperation.withCreate(LongAccumulator::new).andAccumulate((LongAccumulator acc, Entry<?, Long> item) -> acc.add(item.getValue())).andCombine(LongAccumulator::add).andDeduct(LongAccumulator::subtract).andExportFinish(LongAccumulator::get);
SupplierEx<Processor> procSupplier1 = Processors.accumulateByFrameP(singletonList((FunctionEx<? super Entry<Long, Long>, ?>) t -> KEY), singletonList((ToLongFunctionEx<? super Entry<Long, Long>>) Entry::getKey), TimestampKind.EVENT, windowDef, aggrOp.withIdentityFinish());
SupplierEx<Processor> procSupplier2 = combineToSlidingWindowP(windowDef, aggrOp, KeyedWindowResult::new);
// new supplier to save the last supplied instance
stage1Supplier = () -> lastSuppliedStage1Processor = (SlidingWindowP<?, ?, ?, ?>) procSupplier1.get();
stage2Supplier = () -> lastSuppliedStage2Processor = (SlidingWindowP<?, ?, ?, ?>) procSupplier2.get();
}
Aggregations