Search in sources :

Example 1 with StreamSource

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

the class DebeziumCdcIntegrationTest method postgres.

@Test
public void postgres() throws Exception {
    PostgreSQLContainer<?> container = postgresContainer();
    try {
        container.start();
        // given
        List<String> expectedRecords = Arrays.asList("1001/0:SYNC:Customer {id=1001, firstName=Sally, lastName=Thomas, email=sally.thomas@acme.com}", "1002/0:SYNC:Customer {id=1002, firstName=George, lastName=Bailey, email=gbailey@foobar.com}", "1003/0:SYNC:Customer {id=1003, firstName=Edward, lastName=Walker, email=ed@walker.com}", "1004/0:SYNC: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 = DebeziumCdcSources.debezium("postgres", "io.debezium.connector.postgresql.PostgresConnector").setProperty("database.server.name", "dbserver1").setProperty("database.hostname", container.getContainerIpAddress()).setProperty("database.port", Integer.toString(container.getMappedPort(POSTGRESQL_PORT))).setProperty("database.user", "postgres").setProperty("database.password", "postgres").setProperty("database.dbname", "postgres").setProperty("table.whitelist", "inventory.customers").build();
        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 = getPostgreSqlConnection(container.getJdbcUrl(), container.getUsername(), container.getPassword())) {
            connection.setSchema("inventory");
            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 2 with StreamSource

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

the class PostgresCdcWhiteBlackListIntegrationTest method pipeline.

private Pipeline pipeline(StreamSource<ChangeRecord> source) {
    Pipeline pipeline = Pipeline.create();
    pipeline.readFrom(source).withNativeTimestamps(0).filter(t -> t.schema().startsWith(SCHEMA_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).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) 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) 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 3 with StreamSource

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

the class JmsSourceIntegrationTestBase method sourceQueue_whenBuilder_withFunctions.

@Test
public void sourceQueue_whenBuilder_withFunctions() throws JMSException {
    String queueName = destinationName;
    StreamSource<String> source = Sources.jmsQueueBuilder(getConnectionFactory()).connectionFn(ConnectionFactory::createConnection).consumerFn(session -> session.createConsumer(session.createQueue(queueName))).messageIdFn(m -> {
        throw new UnsupportedOperationException();
    }).build(TEXT_MESSAGE_FN);
    p.readFrom(source).withoutTimestamps().writeTo(Sinks.list(sinkList));
    startJob();
    List<Object> messages = sendMessages(true);
    assertEqualsEventually(sinkList::size, messages.size());
    assertContainsAll(sinkList, messages);
}
Also used : AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) Mockito.doThrow(org.mockito.Mockito.doThrow) Session(javax.jms.Session) Future(java.util.concurrent.Future) WindowDefinition.tumbling(com.hazelcast.jet.pipeline.WindowDefinition.tumbling) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) MessageProducer(javax.jms.MessageProducer) JobStatus(com.hazelcast.jet.core.JobStatus) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) FunctionEx(com.hazelcast.function.FunctionEx) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JobConfig(com.hazelcast.jet.config.JobConfig) PredicateEx.alwaysFalse(com.hazelcast.function.PredicateEx.alwaysFalse) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) DUPS_OK_ACKNOWLEDGE(javax.jms.Session.DUPS_OK_ACKNOWLEDGE) JMSException(javax.jms.JMSException) Collectors(java.util.stream.Collectors) SupplierEx(com.hazelcast.function.SupplierEx) ZoneId(java.time.ZoneId) Sources(com.hazelcast.jet.pipeline.Sources) List(java.util.List) MessageConsumer(javax.jms.MessageConsumer) Assert.assertFalse(org.junit.Assert.assertFalse) WindowResult(com.hazelcast.jet.datamodel.WindowResult) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeClass(org.junit.BeforeClass) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) AUTO_ACKNOWLEDGE(javax.jms.Session.AUTO_ACKNOWLEDGE) MINUTES(java.util.concurrent.TimeUnit.MINUTES) JobProxy(com.hazelcast.jet.impl.JobProxy) StreamSource(com.hazelcast.jet.pipeline.StreamSource) ExceptionUtil.sneakyThrow(com.hazelcast.jet.impl.util.ExceptionUtil.sneakyThrow) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MapWatermarksToString.mapWatermarksToString(com.hazelcast.jet.core.TestProcessors.MapWatermarksToString.mapWatermarksToString) JmsSourceBuilder(com.hazelcast.jet.pipeline.JmsSourceBuilder) Message(javax.jms.Message) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Job(com.hazelcast.jet.Job) Before(org.junit.Before) IList(com.hazelcast.collection.IList) JobRepository(com.hazelcast.jet.impl.JobRepository) Connection(javax.jms.Connection) TextMessage(javax.jms.TextMessage) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) Sinks(com.hazelcast.jet.pipeline.Sinks) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) NONE(com.hazelcast.jet.config.ProcessingGuarantee.NONE) TreeMap(java.util.TreeMap) JmsTestUtil.consumeMessages(com.hazelcast.jet.impl.connector.JmsTestUtil.consumeMessages) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) AT_LEAST_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.AT_LEAST_ONCE) ConnectionFactory(javax.jms.ConnectionFactory) Assert.assertEquals(org.junit.Assert.assertEquals) MapWatermarksToString.mapWatermarksToString(com.hazelcast.jet.core.TestProcessors.MapWatermarksToString.mapWatermarksToString) Test(org.junit.Test)

Example 4 with StreamSource

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

the class JobExecutionMetricsTest method snapshotPipeline.

private Pipeline snapshotPipeline() {
    Pipeline p = Pipeline.create();
    StreamSource<Long> source = SourceBuilder.stream("src", procCtx -> new long[1]).<Long>fillBufferFn((ctx, buf) -> {
        buf.add(ctx[0]++);
        Thread.sleep(5);
    }).createSnapshotFn(ctx -> ctx[0]).restoreSnapshotFn((ctx, state) -> ctx[0] = state.get(0)).build();
    p.readFrom(source).withoutTimestamps().writeTo(Sinks.logger());
    return p;
}
Also used : SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) JobRepository(com.hazelcast.jet.impl.JobRepository) Config(com.hazelcast.config.Config) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) BeforeClass(org.junit.BeforeClass) Pipeline(com.hazelcast.jet.pipeline.Pipeline) QuickTest(com.hazelcast.test.annotation.QuickTest) JobConfig(com.hazelcast.jet.config.JobConfig) Sinks(com.hazelcast.jet.pipeline.Sinks) Test(org.junit.Test) StreamSource(com.hazelcast.jet.pipeline.StreamSource) Category(org.junit.experimental.categories.Category) TestSources(com.hazelcast.jet.pipeline.test.TestSources) EXECUTION_START_TIME(com.hazelcast.jet.core.metrics.MetricNames.EXECUTION_START_TIME) EXECUTION_COMPLETION_TIME(com.hazelcast.jet.core.metrics.MetricNames.EXECUTION_COMPLETION_TIME) SourceBuilder(com.hazelcast.jet.pipeline.SourceBuilder) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) Job(com.hazelcast.jet.Job) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Example 5 with StreamSource

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

the class JobSnapshotMetricsTest method pipeline.

private Pipeline pipeline() {
    Pipeline p = Pipeline.create();
    StreamSource<Long> source = SourceBuilder.stream(SOURCE_VERTEX_NAME, procCtx -> new long[1]).<Long>fillBufferFn((ctx, buf) -> {
        buf.add(ctx[0]++);
        Thread.sleep(5);
    }).createSnapshotFn(ctx -> ctx[0]).restoreSnapshotFn((ctx, state) -> ctx[0] = state.get(0)).build();
    p.readFrom(source).withoutTimestamps().filter(t -> t % 2 == 0).writeTo(Sinks.noop());
    return p;
}
Also used : SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) JobRepository(com.hazelcast.jet.impl.JobRepository) Config(com.hazelcast.config.Config) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) BeforeClass(org.junit.BeforeClass) Pipeline(com.hazelcast.jet.pipeline.Pipeline) QuickTest(com.hazelcast.test.annotation.QuickTest) JobConfig(com.hazelcast.jet.config.JobConfig) Sinks(com.hazelcast.jet.pipeline.Sinks) Test(org.junit.Test) StreamSource(com.hazelcast.jet.pipeline.StreamSource) Category(org.junit.experimental.categories.Category) SourceBuilder(com.hazelcast.jet.pipeline.SourceBuilder) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) Job(com.hazelcast.jet.Job) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

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