Search in sources :

Example 11 with SinkContext

use of org.apache.pulsar.io.core.SinkContext in project incubator-pulsar by apache.

the class KinesisSinkTest method testWrite.

@Test
public void testWrite() throws Exception {
    AtomicBoolean ackCalled = new AtomicBoolean();
    AtomicLong sequenceCounter = new AtomicLong(0);
    Message<GenericObject> message = mock(Message.class);
    when(message.getData()).thenReturn("hello".getBytes(StandardCharsets.UTF_8));
    final Record<GenericObject> pulsarRecord = new Record<GenericObject>() {

        @Override
        public Optional<String> getKey() {
            return Optional.of("key-" + sequenceCounter.incrementAndGet());
        }

        @Override
        public GenericObject getValue() {
            // Value comes from the message raw data, not the GenericObject
            return null;
        }

        @Override
        public void ack() {
            ackCalled.set(true);
        }

        @Override
        public Optional<Message<GenericObject>> getMessage() {
            return Optional.of(message);
        }
    };
    try (final KinesisSink sink = new KinesisSink()) {
        Map<String, Object> map = createConfig();
        SinkContext mockSinkContext = mock(SinkContext.class);
        sink.open(map, mockSinkContext);
        for (int i = 0; i < 10; i++) {
            sink.write(pulsarRecord);
        }
        Awaitility.await().untilAsserted(() -> {
            assertTrue(ackCalled.get());
        });
        final GetRecordsResponse getRecords = getStreamRecords();
        assertEquals(getRecords.records().size(), 10);
        for (software.amazon.awssdk.services.kinesis.model.Record record : getRecords.records()) {
            assertEquals(record.data().asString(StandardCharsets.UTF_8), "hello");
        }
    }
}
Also used : Message(org.apache.pulsar.client.api.Message) SinkContext(org.apache.pulsar.io.core.SinkContext) GetRecordsResponse(software.amazon.awssdk.services.kinesis.model.GetRecordsResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) GenericObject(org.apache.pulsar.client.api.schema.GenericObject) Record(org.apache.pulsar.functions.api.Record) GenericObject(org.apache.pulsar.client.api.schema.GenericObject) Test(org.testng.annotations.Test)

Example 12 with SinkContext

use of org.apache.pulsar.io.core.SinkContext in project incubator-pulsar by apache.

the class KafkaAbstractSinkTest method testInvalidConfigWillThrownException.

@Test
public void testInvalidConfigWillThrownException() throws Exception {
    KafkaAbstractSink sink = new DummySink();
    Map<String, Object> config = new HashMap<>();
    SinkContext sc = new SinkContext() {

        @Override
        public int getInstanceId() {
            return 0;
        }

        @Override
        public int getNumInstances() {
            return 0;
        }

        @Override
        public void recordMetric(String metricName, double value) {
        }

        @Override
        public Collection<String> getInputTopics() {
            return null;
        }

        @Override
        public SinkConfig getSinkConfig() {
            return null;
        }

        @Override
        public String getTenant() {
            return null;
        }

        @Override
        public String getNamespace() {
            return null;
        }

        @Override
        public String getSinkName() {
            return null;
        }

        @Override
        public Logger getLogger() {
            return null;
        }

        @Override
        public String getSecret(String key) {
            return null;
        }

        @Override
        public void incrCounter(String key, long amount) {
        }

        @Override
        public CompletableFuture<Void> incrCounterAsync(String key, long amount) {
            return null;
        }

        @Override
        public long getCounter(String key) {
            return 0;
        }

        @Override
        public CompletableFuture<Long> getCounterAsync(String key) {
            return null;
        }

        @Override
        public void putState(String key, ByteBuffer value) {
        }

        @Override
        public CompletableFuture<Void> putStateAsync(String key, ByteBuffer value) {
            return null;
        }

        @Override
        public ByteBuffer getState(String key) {
            return null;
        }

        @Override
        public CompletableFuture<ByteBuffer> getStateAsync(String key) {
            return null;
        }

        @Override
        public void deleteState(String key) {
        }

        @Override
        public CompletableFuture<Void> deleteStateAsync(String key) {
            return null;
        }

        @Override
        public PulsarClient getPulsarClient() {
            return null;
        }
    };
    ThrowingRunnable openAndClose = () -> {
        try {
            sink.open(config, sc);
            fail();
        } finally {
            sink.close();
        }
    };
    expectThrows(NullPointerException.class, "Kafka topic is not set", openAndClose);
    config.put("topic", "topic_2");
    expectThrows(NullPointerException.class, "Kafka bootstrapServers is not set", openAndClose);
    config.put("bootstrapServers", "localhost:6667");
    expectThrows(NullPointerException.class, "Kafka acks mode is not set", openAndClose);
    config.put("acks", "1");
    config.put("batchSize", "-1");
    expectThrows(IllegalArgumentException.class, "Invalid Kafka Producer batchSize : -1", openAndClose);
    config.put("batchSize", "16384");
    config.put("maxRequestSize", "-1");
    expectThrows(IllegalArgumentException.class, "Invalid Kafka Producer maxRequestSize : -1", openAndClose);
    config.put("maxRequestSize", "1048576");
    config.put("acks", "none");
    expectThrows(ConfigException.class, "Invalid value none for configuration acks: String must be one of: all, -1, 0, 1", openAndClose);
    config.put("acks", "1");
    sink.open(config, sc);
    sink.close();
}
Also used : HashMap(java.util.HashMap) SinkContext(org.apache.pulsar.io.core.SinkContext) KafkaAbstractSink(org.apache.pulsar.io.kafka.KafkaAbstractSink) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 13 with SinkContext

use of org.apache.pulsar.io.core.SinkContext in project pulsar by yahoo.

the class ContextImplTest method testGetSubscriptionType.

@Test
public void testGetSubscriptionType() {
    SinkContext ctx = context;
    // make sure SinkContext can get SubscriptionType.
    Assert.assertEquals(ctx.getSubscriptionType(), SubscriptionType.Shared);
}
Also used : SinkContext(org.apache.pulsar.io.core.SinkContext) Test(org.testng.annotations.Test)

Example 14 with SinkContext

use of org.apache.pulsar.io.core.SinkContext in project pulsar by yahoo.

the class KinesisSinkConfigTests method loadFromMapCredentialFromSecretTest.

@Test
public final void loadFromMapCredentialFromSecretTest() throws IOException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("awsEndpoint", "https://some.endpoint.aws");
    map.put("awsRegion", "us-east-1");
    map.put("awsKinesisStreamName", "my-stream");
    SinkContext sinkContext = Mockito.mock(SinkContext.class);
    Mockito.when(sinkContext.getSecret("awsCredentialPluginParam")).thenReturn("{\"accessKey\":\"myKey\",\"secretKey\":\"my-Secret\"}");
    KinesisSinkConfig config = IOConfigUtils.loadWithSecrets(map, KinesisSinkConfig.class, sinkContext);
    assertNotNull(config);
    assertEquals(config.getAwsEndpoint(), "https://some.endpoint.aws");
    assertEquals(config.getAwsRegion(), "us-east-1");
    assertEquals(config.getAwsKinesisStreamName(), "my-stream");
    assertEquals(config.getAwsCredentialPluginParam(), "{\"accessKey\":\"myKey\",\"secretKey\":\"my-Secret\"}");
}
Also used : HashMap(java.util.HashMap) SinkContext(org.apache.pulsar.io.core.SinkContext) Test(org.testng.annotations.Test)

Example 15 with SinkContext

use of org.apache.pulsar.io.core.SinkContext in project pulsar by yahoo.

the class KinesisSinkTest method testWrite.

@Test
public void testWrite() throws Exception {
    AtomicBoolean ackCalled = new AtomicBoolean();
    AtomicLong sequenceCounter = new AtomicLong(0);
    Message<GenericObject> message = mock(Message.class);
    when(message.getData()).thenReturn("hello".getBytes(StandardCharsets.UTF_8));
    final Record<GenericObject> pulsarRecord = new Record<GenericObject>() {

        @Override
        public Optional<String> getKey() {
            return Optional.of("key-" + sequenceCounter.incrementAndGet());
        }

        @Override
        public GenericObject getValue() {
            // Value comes from the message raw data, not the GenericObject
            return null;
        }

        @Override
        public void ack() {
            ackCalled.set(true);
        }

        @Override
        public Optional<Message<GenericObject>> getMessage() {
            return Optional.of(message);
        }
    };
    try (final KinesisSink sink = new KinesisSink()) {
        Map<String, Object> map = createConfig();
        SinkContext mockSinkContext = mock(SinkContext.class);
        sink.open(map, mockSinkContext);
        for (int i = 0; i < 10; i++) {
            sink.write(pulsarRecord);
        }
        Awaitility.await().untilAsserted(() -> {
            assertTrue(ackCalled.get());
        });
        final GetRecordsResponse getRecords = getStreamRecords();
        assertEquals(getRecords.records().size(), 10);
        for (software.amazon.awssdk.services.kinesis.model.Record record : getRecords.records()) {
            assertEquals(record.data().asString(StandardCharsets.UTF_8), "hello");
        }
    }
}
Also used : Message(org.apache.pulsar.client.api.Message) SinkContext(org.apache.pulsar.io.core.SinkContext) GetRecordsResponse(software.amazon.awssdk.services.kinesis.model.GetRecordsResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) GenericObject(org.apache.pulsar.client.api.schema.GenericObject) Record(org.apache.pulsar.functions.api.Record) GenericObject(org.apache.pulsar.client.api.schema.GenericObject) Test(org.testng.annotations.Test)

Aggregations

SinkContext (org.apache.pulsar.io.core.SinkContext)17 Test (org.testng.annotations.Test)16 HashMap (java.util.HashMap)10 ByteBuffer (java.nio.ByteBuffer)3 KafkaAbstractSink (org.apache.pulsar.io.kafka.KafkaAbstractSink)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Message (org.apache.pulsar.client.api.Message)2 GenericObject (org.apache.pulsar.client.api.schema.GenericObject)2 SinkConfig (org.apache.pulsar.common.io.SinkConfig)2 Record (org.apache.pulsar.functions.api.Record)2 GetRecordsResponse (software.amazon.awssdk.services.kinesis.model.GetRecordsResponse)2 CloudStorageSinkConfig (org.apache.pulsar.io.jcloud.sink.CloudStorageSinkConfig)1 Test (org.junit.Test)1