Search in sources :

Example 1 with PulsarRecord

use of org.apache.pulsar.functions.source.PulsarRecord in project incubator-pulsar by apache.

the class SinkRecord method individualAck.

/**
 * Some sink sometimes wants to control the ack type.
 */
public void individualAck() {
    if (sourceRecord instanceof PulsarRecord) {
        PulsarRecord pulsarRecord = (PulsarRecord) sourceRecord;
        pulsarRecord.individualAck();
    } else {
        throw new RuntimeException("SourceRecord class type must be PulsarRecord");
    }
}
Also used : PulsarRecord(org.apache.pulsar.functions.source.PulsarRecord)

Example 2 with PulsarRecord

use of org.apache.pulsar.functions.source.PulsarRecord in project incubator-pulsar by apache.

the class KafkaConnectSinkTest method genericRecordCastTest.

@Test
public void genericRecordCastTest() throws Exception {
    props.put("kafkaConnectorSinkClass", SchemaedFileStreamSinkConnector.class.getCanonicalName());
    KafkaConnectSink sink = new KafkaConnectSink();
    sink.open(props, context);
    AvroSchema<PulsarSchemaToKafkaSchemaTest.StructWithAnnotations> pulsarAvroSchema = AvroSchema.of(PulsarSchemaToKafkaSchemaTest.StructWithAnnotations.class);
    final GenericData.Record obj = new GenericData.Record(pulsarAvroSchema.getAvroSchema());
    // schema type INT32
    obj.put("field1", (byte) 10);
    // schema type STRING
    obj.put("field2", "test");
    // schema type INT64
    obj.put("field3", (short) 100);
    final GenericRecord rec = getGenericRecord(obj, pulsarAvroSchema);
    Message msg = mock(MessageImpl.class);
    when(msg.getValue()).thenReturn(rec);
    when(msg.getKey()).thenReturn("key");
    when(msg.hasKey()).thenReturn(true);
    when(msg.getMessageId()).thenReturn(new MessageIdImpl(1, 0, 0));
    final AtomicInteger status = new AtomicInteger(0);
    Record<GenericObject> record = PulsarRecord.<String>builder().topicName("fake-topic").message(msg).schema(pulsarAvroSchema).ackFunction(status::incrementAndGet).failFunction(status::decrementAndGet).build();
    SinkRecord sinkRecord = sink.toSinkRecord(record);
    Struct out = (Struct) sinkRecord.value();
    Assert.assertEquals(out.get("field1").getClass(), Integer.class);
    Assert.assertEquals(out.get("field2").getClass(), String.class);
    Assert.assertEquals(out.get("field3").getClass(), Long.class);
    Assert.assertEquals(out.get("field1"), 10);
    Assert.assertEquals(out.get("field2"), "test");
    Assert.assertEquals(out.get("field3"), 100L);
    sink.close();
}
Also used : Message(org.apache.pulsar.client.api.Message) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) GenericData(org.apache.avro.generic.GenericData) Struct(org.apache.kafka.connect.data.Struct) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GenericObject(org.apache.pulsar.client.api.schema.GenericObject) Record(org.apache.pulsar.functions.api.Record) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) PulsarRecord(org.apache.pulsar.functions.source.PulsarRecord) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Test(org.testng.annotations.Test)

Example 3 with PulsarRecord

use of org.apache.pulsar.functions.source.PulsarRecord in project incubator-pulsar by apache.

the class SinkRecordTest method testCustomAck.

@Test
public void testCustomAck() {
    PulsarRecord pulsarRecord = Mockito.mock(PulsarRecord.class);
    SinkRecord sinkRecord = new SinkRecord<>(pulsarRecord, new Object());
    sinkRecord.cumulativeAck();
    Mockito.verify(pulsarRecord, Mockito.times(1)).cumulativeAck();
    sinkRecord = new SinkRecord(Mockito.mock(Record.class), new Object());
    try {
        sinkRecord.individualAck();
        Assert.fail("Should throw runtime exception");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof RuntimeException);
        Assert.assertEquals(e.getMessage(), "SourceRecord class type must be PulsarRecord");
    }
}
Also used : PulsarRecord(org.apache.pulsar.functions.source.PulsarRecord) Test(org.testng.annotations.Test)

Example 4 with PulsarRecord

use of org.apache.pulsar.functions.source.PulsarRecord in project incubator-pulsar by apache.

the class SinkRecord method cumulativeAck.

/**
 * Some sink sometimes wants to control the ack type.
 */
public void cumulativeAck() {
    if (sourceRecord instanceof PulsarRecord) {
        PulsarRecord pulsarRecord = (PulsarRecord) sourceRecord;
        pulsarRecord.cumulativeAck();
    } else {
        throw new RuntimeException("SourceRecord class type must be PulsarRecord");
    }
}
Also used : PulsarRecord(org.apache.pulsar.functions.source.PulsarRecord)

Example 5 with PulsarRecord

use of org.apache.pulsar.functions.source.PulsarRecord in project pulsar by yahoo.

the class KafkaConnectSinkTest method genericRecordCastTest.

@Test
public void genericRecordCastTest() throws Exception {
    props.put("kafkaConnectorSinkClass", SchemaedFileStreamSinkConnector.class.getCanonicalName());
    KafkaConnectSink sink = new KafkaConnectSink();
    sink.open(props, context);
    AvroSchema<PulsarSchemaToKafkaSchemaTest.StructWithAnnotations> pulsarAvroSchema = AvroSchema.of(PulsarSchemaToKafkaSchemaTest.StructWithAnnotations.class);
    final GenericData.Record obj = new GenericData.Record(pulsarAvroSchema.getAvroSchema());
    // schema type INT32
    obj.put("field1", (byte) 10);
    // schema type STRING
    obj.put("field2", "test");
    // schema type INT64
    obj.put("field3", (short) 100);
    final GenericRecord rec = getGenericRecord(obj, pulsarAvroSchema);
    Message msg = mock(MessageImpl.class);
    when(msg.getValue()).thenReturn(rec);
    when(msg.getKey()).thenReturn("key");
    when(msg.hasKey()).thenReturn(true);
    when(msg.getMessageId()).thenReturn(new MessageIdImpl(1, 0, 0));
    final AtomicInteger status = new AtomicInteger(0);
    Record<GenericObject> record = PulsarRecord.<String>builder().topicName("fake-topic").message(msg).schema(pulsarAvroSchema).ackFunction(status::incrementAndGet).failFunction(status::decrementAndGet).build();
    SinkRecord sinkRecord = sink.toSinkRecord(record);
    Struct out = (Struct) sinkRecord.value();
    Assert.assertEquals(out.get("field1").getClass(), Integer.class);
    Assert.assertEquals(out.get("field2").getClass(), String.class);
    Assert.assertEquals(out.get("field3").getClass(), Long.class);
    Assert.assertEquals(out.get("field1"), 10);
    Assert.assertEquals(out.get("field2"), "test");
    Assert.assertEquals(out.get("field3"), 100L);
    sink.close();
}
Also used : Message(org.apache.pulsar.client.api.Message) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) GenericData(org.apache.avro.generic.GenericData) Struct(org.apache.kafka.connect.data.Struct) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GenericObject(org.apache.pulsar.client.api.schema.GenericObject) Record(org.apache.pulsar.functions.api.Record) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) PulsarRecord(org.apache.pulsar.functions.source.PulsarRecord) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Test(org.testng.annotations.Test)

Aggregations

PulsarRecord (org.apache.pulsar.functions.source.PulsarRecord)5 Test (org.testng.annotations.Test)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 GenericData (org.apache.avro.generic.GenericData)2 Struct (org.apache.kafka.connect.data.Struct)2 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)2 Message (org.apache.pulsar.client.api.Message)2 GenericObject (org.apache.pulsar.client.api.schema.GenericObject)2 GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)2 MessageIdImpl (org.apache.pulsar.client.impl.MessageIdImpl)2 GenericAvroRecord (org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord)2 Record (org.apache.pulsar.functions.api.Record)2