Search in sources :

Example 6 with StreamSource

use of com.hazelcast.jet.pipeline.StreamSource in project hazelcast by hazelcast.

the class DebeziumCdcIntegrationTest method mysql.

@Test
public void mysql() throws Exception {
    Assume.assumeFalse("https://github.com/hazelcast/hazelcast-jet/issues/2623, " + "https://github.com/hazelcast/hazelcast/issues/18800", System.getProperty("java.version").matches("^1[56].*"));
    MySQLContainer<?> container = mySqlContainer();
    try {
        container.start();
        // given
        List<String> expectedRecords = Arrays.asList("1001/0:INSERT:Customer {id=1001, firstName=Sally, lastName=Thomas, email=sally.thomas@acme.com}", "1002/0:INSERT:Customer {id=1002, firstName=George, lastName=Bailey, email=gbailey@foobar.com}", "1003/0:INSERT:Customer {id=1003, firstName=Edward, lastName=Walker, email=ed@walker.com}", "1004/0:INSERT:Customer {id=1004, firstName=Anne, lastName=Kretchmar, email=annek@noanswer.org}", "1004/1:UPDATE:Customer {id=1004, firstName=Anne Marie, lastName=Kretchmar, email=annek@noanswer.org}", "1005/0:INSERT:Customer {id=1005, firstName=Jason, lastName=Bourne, email=jason@bourne.org}", "1005/1:DELETE:Customer {id=1005, firstName=Jason, lastName=Bourne, email=jason@bourne.org}");
        StreamSource<ChangeRecord> source = mySqlSource(container);
        Pipeline pipeline = Pipeline.create();
        pipeline.readFrom(source).withNativeTimestamps(0).<ChangeRecord>customTransform("filter_timestamps", filterTimestampsProcessorSupplier()).groupingKey(record -> (Integer) record.key().toMap().get("id")).mapStateful(LongAccumulator::new, (accumulator, customerId, record) -> {
            long count = accumulator.get();
            accumulator.add(1);
            Operation operation = record.operation();
            RecordPart value = record.value();
            Customer customer = value.toObject(Customer.class);
            return entry(customerId + "/" + count, operation + ":" + customer);
        }).setLocalParallelism(1).writeTo(Sinks.map("results"));
        // when
        HazelcastInstance hz = createHazelcastInstances(2)[0];
        Job job = hz.getJet().newJob(pipeline);
        // then
        assertEqualsEventually(() -> hz.getMap("results").size(), 4);
        // when
        try (Connection connection = getMySqlConnection(container.withDatabaseName("inventory").getJdbcUrl(), container.getUsername(), container.getPassword())) {
            Statement statement = connection.createStatement();
            statement.addBatch("UPDATE customers SET first_name='Anne Marie' WHERE id=1004");
            statement.addBatch("INSERT INTO customers VALUES (1005, 'Jason', 'Bourne', 'jason@bourne.org')");
            statement.addBatch("DELETE FROM customers WHERE id=1005");
            statement.executeBatch();
        }
        // then
        try {
            assertEqualsEventually(() -> mapResultsToSortedList(hz.getMap("results")), expectedRecords);
        } finally {
            job.cancel();
            assertJobStatusEventually(job, JobStatus.FAILED);
        }
    } finally {
        container.stop();
    }
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) Connection(java.sql.Connection) DockerImageName(org.testcontainers.utility.DockerImageName) RunWith(org.junit.runner.RunWith) PostgreSQLContainer(org.testcontainers.containers.PostgreSQLContainer) StreamSource(com.hazelcast.jet.pipeline.StreamSource) HazelcastSerialClassRunner(com.hazelcast.test.HazelcastSerialClassRunner) JetException(com.hazelcast.jet.JetException) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Util.entry(com.hazelcast.jet.Util.entry) Assume(org.junit.Assume) JobStatus(com.hazelcast.jet.core.JobStatus) Nonnull(javax.annotation.Nonnull) Job(com.hazelcast.jet.Job) HazelcastInstance(com.hazelcast.core.HazelcastInstance) POSTGRESQL_PORT(org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Sinks(com.hazelcast.jet.pipeline.Sinks) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) MySQLContainer(org.testcontainers.containers.MySQLContainer) Objects(java.util.Objects) List(java.util.List) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Statement(java.sql.Statement) Entry(java.util.Map.Entry) MYSQL_PORT(org.testcontainers.containers.MySQLContainer.MYSQL_PORT) Statement(java.sql.Statement) Connection(java.sql.Connection) Pipeline(com.hazelcast.jet.pipeline.Pipeline) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Job(com.hazelcast.jet.Job) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 7 with StreamSource

use of com.hazelcast.jet.pipeline.StreamSource in project hazelcast by hazelcast.

the class MySqlCdcListenBeforeExistIntegrationTest method pipeline.

private Pipeline pipeline(StreamSource<ChangeRecord> source) {
    Pipeline pipeline = Pipeline.create();
    pipeline.readFrom(source).withNativeTimestamps(0).<ChangeRecord>customTransform("filter_timestamps", filterTimestampsProcessorSupplier()).setLocalParallelism(1).groupingKey(record -> (Integer) record.key().toMap().get("id")).mapStateful(LongAccumulator::new, (accumulator, rowId, record) -> {
        long count = accumulator.get();
        accumulator.add(1);
        Operation operation = record.operation();
        RecordPart value = record.value();
        TableRow row = value.toObject(TableRow.class);
        return entry(rowId + "/" + count, operation + ":" + row);
    }).setLocalParallelism(1).peek().writeTo(Sinks.map(SINK_MAP_NAME));
    return pipeline;
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Arrays(java.util.Arrays) Connection(java.sql.Connection) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Sinks(com.hazelcast.jet.pipeline.Sinks) RecordPart(com.hazelcast.jet.cdc.RecordPart) Test(org.junit.Test) StreamSource(com.hazelcast.jet.pipeline.StreamSource) Category(org.junit.experimental.categories.Category) SQLException(java.sql.SQLException) List(java.util.List) Operation(com.hazelcast.jet.cdc.Operation) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Util.entry(com.hazelcast.jet.Util.entry) Statement(java.sql.Statement) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) JobStatus(com.hazelcast.jet.core.JobStatus) Collections(java.util.Collections) Job(com.hazelcast.jet.Job) RecordPart(com.hazelcast.jet.cdc.RecordPart) Operation(com.hazelcast.jet.cdc.Operation) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Example 8 with StreamSource

use of com.hazelcast.jet.pipeline.StreamSource in project hazelcast by hazelcast.

the class MySqlCdcWhiteBlackListIntegrationTest method pipeline.

private Pipeline pipeline(StreamSource<ChangeRecord> source) {
    Pipeline pipeline = Pipeline.create();
    pipeline.readFrom(source).withNativeTimestamps(0).filter(t -> t.database().startsWith(DB_PREFIX)).setLocalParallelism(1).<ChangeRecord>customTransform("filter_timestamps", filterTimestampsProcessorSupplier()).setLocalParallelism(1).groupingKey(record -> (Integer) record.key().toMap().get("id")).mapStateful(LongAccumulator::new, (accumulator, rowId, record) -> {
        long count = accumulator.get();
        accumulator.add(1);
        Operation operation = record.operation();
        RecordPart value = record.value();
        TableRow row = value.toObject(TableRow.class);
        return entry(rowId + "/" + count, operation + ":" + row);
    }).setLocalParallelism(1).writeTo(Sinks.map(SINK_MAP_NAME));
    return pipeline;
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Arrays(java.util.Arrays) Connection(java.sql.Connection) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Sinks(com.hazelcast.jet.pipeline.Sinks) RecordPart(com.hazelcast.jet.cdc.RecordPart) Test(org.junit.Test) StreamSource(com.hazelcast.jet.pipeline.StreamSource) Category(org.junit.experimental.categories.Category) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) List(java.util.List) Operation(com.hazelcast.jet.cdc.Operation) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Util.entry(com.hazelcast.jet.Util.entry) Statement(java.sql.Statement) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) JobStatus(com.hazelcast.jet.core.JobStatus) Job(com.hazelcast.jet.Job) Before(org.junit.Before) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) RecordPart(com.hazelcast.jet.cdc.RecordPart) Operation(com.hazelcast.jet.cdc.Operation) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Example 9 with StreamSource

use of com.hazelcast.jet.pipeline.StreamSource in project hazelcast by hazelcast.

the class AbstractKinesisTest method getPipeline.

protected Pipeline getPipeline(StreamSource<Map.Entry<String, byte[]>> source) {
    Pipeline pipeline = Pipeline.create();
    pipeline.readFrom(source).withNativeTimestamps(0).rebalance(Map.Entry::getKey).map(e -> entry(e.getKey(), Collections.singletonList(new String(e.getValue())))).writeTo(Sinks.mapWithMerging(results, Map.Entry::getKey, Map.Entry::getValue, (l1, l2) -> {
        ArrayList<String> list = new ArrayList<>();
        list.addAll(l1);
        list.addAll(l2);
        return list;
    }));
    return pipeline;
}
Also used : Shard(com.amazonaws.services.kinesis.model.Shard) IntStream(java.util.stream.IntStream) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) RetryStrategies(com.hazelcast.jet.retry.RetryStrategies) HashRange(com.hazelcast.jet.kinesis.impl.source.HashRange) StreamSource(com.hazelcast.jet.pipeline.StreamSource) AmazonKinesisAsync(com.amazonaws.services.kinesis.AmazonKinesisAsync) HazelcastSerialClassRunner(com.hazelcast.test.HazelcastSerialClassRunner) TreeSet(java.util.TreeSet) SplitShardRequest(com.amazonaws.services.kinesis.model.SplitShardRequest) ArrayList(java.util.ArrayList) After(org.junit.After) Map(java.util.Map) AwsConfig(com.hazelcast.jet.kinesis.impl.AwsConfig) BigInteger(java.math.BigInteger) Nonnull(javax.annotation.Nonnull) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Before(org.junit.Before) MapUtil.entry(com.hazelcast.internal.util.MapUtil.entry) HazelcastInstance(com.hazelcast.core.HazelcastInstance) BatchSource(com.hazelcast.jet.pipeline.BatchSource) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) Sinks(com.hazelcast.jet.pipeline.Sinks) MergeShardsRequest(com.amazonaws.services.kinesis.model.MergeShardsRequest) Math.min(java.lang.Math.min) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Math.max(java.lang.Math.max) Collections(java.util.Collections) Sink(com.hazelcast.jet.pipeline.Sink) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) ArrayList(java.util.ArrayList) Map(java.util.Map) IMap(com.hazelcast.map.IMap) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Example 10 with StreamSource

use of com.hazelcast.jet.pipeline.StreamSource in project hazelcast by hazelcast.

the class KinesisIntegrationTest method customProjection.

@Test
@Category(SerialTest.class)
public void customProjection() {
    HELPER.createStream(1);
    sendMessages();
    Long expectedPerSequenceNo = 1L;
    try {
        Pipeline pipeline = Pipeline.create();
        StreamSource<String> source = kinesisSource().withProjectionFn((r, s) -> {
            byte[] payload = new byte[r.getData().remaining()];
            r.getData().get(payload);
            return r.getSequenceNumber();
        }).build();
        pipeline.readFrom(source).withoutTimestamps().groupingKey(r -> r).rollingAggregate(counting()).apply(assertCollectedEventually(ASSERT_TRUE_EVENTUALLY_TIMEOUT, results -> {
            assertEquals(MESSAGES, results.size());
            results.forEach(v -> assertEquals(expectedPerSequenceNo, v.getValue()));
        }));
        hz().getJet().newJob(pipeline).join();
        fail("Expected exception not thrown");
    } catch (CompletionException ce) {
        Throwable cause = peel(ce);
        assertTrue(cause instanceof JetException);
        assertTrue(cause.getCause() instanceof AssertionCompletedException);
    }
}
Also used : Shard(com.amazonaws.services.kinesis.model.Shard) AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) BeforeClass(org.junit.BeforeClass) AT_SEQUENCE_NUMBER(com.amazonaws.services.kinesis.model.ShardIteratorType.AT_SEQUENCE_NUMBER) JobProxy(com.hazelcast.jet.impl.JobProxy) StreamSource(com.hazelcast.jet.pipeline.StreamSource) SDKGlobalConfiguration(com.amazonaws.SDKGlobalConfiguration) AmazonKinesisAsync(com.amazonaws.services.kinesis.AmazonKinesisAsync) JetException(com.hazelcast.jet.JetException) PutRecordsResult(com.amazonaws.services.kinesis.model.PutRecordsResult) Map(java.util.Map) AwsConfig(com.hazelcast.jet.kinesis.impl.AwsConfig) AT_TIMESTAMP(com.amazonaws.services.kinesis.model.ShardIteratorType.AT_TIMESTAMP) Assert.fail(org.junit.Assert.fail) JobStatus(com.hazelcast.jet.core.JobStatus) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Job(com.hazelcast.jet.Job) Service(org.testcontainers.containers.localstack.LocalStackContainer.Service) TRIM_HORIZON(com.amazonaws.services.kinesis.model.ShardIteratorType.TRIM_HORIZON) DockerImageName.parse(org.testcontainers.utility.DockerImageName.parse) WindowDefinition(com.hazelcast.jet.pipeline.WindowDefinition) Logger(com.hazelcast.logging.Logger) LATEST(com.amazonaws.services.kinesis.model.ShardIteratorType.LATEST) NightlyTest(com.hazelcast.test.annotation.NightlyTest) AfterClass(org.junit.AfterClass) Assertions.assertCollectedEventually(com.hazelcast.jet.pipeline.test.Assertions.assertCollectedEventually) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JobConfig(com.hazelcast.jet.config.JobConfig) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CompletionException(java.util.concurrent.CompletionException) Category(org.junit.experimental.categories.Category) LocalStackContainer(org.testcontainers.containers.localstack.LocalStackContainer) Collectors(java.util.stream.Collectors) List(java.util.List) AFTER_SEQUENCE_NUMBER(com.amazonaws.services.kinesis.model.ShardIteratorType.AFTER_SEQUENCE_NUMBER) Ignore(org.junit.Ignore) AssertionCompletedException(com.hazelcast.jet.pipeline.test.AssertionCompletedException) ExceptionUtil.peel(com.hazelcast.jet.impl.util.ExceptionUtil.peel) Assert.assertFalse(org.junit.Assert.assertFalse) SerialTest(com.hazelcast.jet.test.SerialTest) DockerClientFactory(org.testcontainers.DockerClientFactory) Assume.assumeTrue(org.junit.Assume.assumeTrue) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) AssertionCompletedException(com.hazelcast.jet.pipeline.test.AssertionCompletedException) CompletionException(java.util.concurrent.CompletionException) JetException(com.hazelcast.jet.JetException) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Category(org.junit.experimental.categories.Category) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) SerialTest(com.hazelcast.jet.test.SerialTest)

Aggregations

Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 StreamSource (com.hazelcast.jet.pipeline.StreamSource)14 Job (com.hazelcast.jet.Job)13 Sinks (com.hazelcast.jet.pipeline.Sinks)13 Test (org.junit.Test)13 Category (org.junit.experimental.categories.Category)12 List (java.util.List)11 JobStatus (com.hazelcast.jet.core.JobStatus)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 NightlyTest (com.hazelcast.test.annotation.NightlyTest)8 Util.entry (com.hazelcast.jet.Util.entry)7 Arrays (java.util.Arrays)7 LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)6 JobConfig (com.hazelcast.jet.config.JobConfig)6 ProcessingGuarantee (com.hazelcast.jet.config.ProcessingGuarantee)6 Connection (java.sql.Connection)6 Statement (java.sql.Statement)6 ArrayList (java.util.ArrayList)6 Before (org.junit.Before)6 ChangeRecord (com.hazelcast.jet.cdc.ChangeRecord)5