Search in sources :

Example 1 with GenericRecordBuilder

use of org.apache.pulsar.client.api.schema.GenericRecordBuilder in project pulsar-flink by streamnative.

the class PulsarSerializer method newStructConverter.

private Function<Object, Object> newStructConverter(FieldsDataType dataType, Schema avroStruct) throws IncompatibleSchemaException {
    if (avroStruct.getType() != Schema.Type.RECORD || avroStruct.getFields().size() != dataType.getChildren().size()) {
        throw new IncompatibleSchemaException(String.format("Cannot convert Flink type %s to Avro type %s.", dataType.toString(), avroStruct.toString(true)));
    }
    RowType rowType = (RowType) dataType.getLogicalType();
    List<RowType.RowField> fields = rowType.getFields();
    List<BiFunction<PositionedGetter, Integer, Object>> fieldConverters = new ArrayList<>();
    for (int i = 0; i < fields.size(); i++) {
        org.apache.flink.table.types.logical.LogicalType logicalType = rowType.getTypeAt(i);
        DataType dt = TypeConversions.fromLogicalToDataType(logicalType);
        Schema.Field at = avroStruct.getFields().get(i);
        fieldConverters.add(newConverter(dt, resolveNullableType(at.schema(), dt.getLogicalType().isNullable())));
    }
    int numFields = dataType.getChildren().size();
    return row -> {
        GenericSchema<GenericRecord> pSchema = SchemaUtils.avroSchema2PulsarSchema(avroStruct);
        GenericRecordBuilder builder = pSchema.newRecordBuilder();
        Row rowX = (Row) row;
        for (int i = 0; i < numFields; i++) {
            if (rowX.getField(i) == null) {
                builder.set(pSchema.getFields().get(i), null);
            } else {
                builder.set(pSchema.getFields().get(i), fieldConverters.get(i).apply(new PositionedGetter(rowX), i));
            }
        }
        return (GenericAvroRecord) builder.build();
    };
}
Also used : DataType(org.apache.flink.table.types.DataType) Arrays(java.util.Arrays) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) BiFunction(java.util.function.BiFunction) LocalDateTime(java.time.LocalDateTime) Field(org.apache.pulsar.client.api.schema.Field) LogicalType(org.apache.pulsar.shade.org.apache.avro.LogicalType) RowType(org.apache.flink.table.types.logical.RowType) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DecimalType(org.apache.flink.table.types.logical.DecimalType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) LogicalTypes(org.apache.pulsar.shade.org.apache.avro.LogicalTypes) Utf8(org.apache.pulsar.shade.org.apache.avro.util.Utf8) GenericData(org.apache.pulsar.shade.org.apache.avro.generic.GenericData) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) Schema(org.apache.pulsar.shade.org.apache.avro.Schema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Collectors(java.util.stream.Collectors) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Slf4j(lombok.extern.slf4j.Slf4j) Conversions(org.apache.pulsar.shade.org.apache.avro.Conversions) List(java.util.List) CollectionDataType(org.apache.flink.table.types.CollectionDataType) LocalDate(java.time.LocalDate) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) Row(org.apache.flink.types.Row) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.shade.org.apache.avro.Schema) ArrayList(java.util.ArrayList) RowType(org.apache.flink.table.types.logical.RowType) BiFunction(java.util.function.BiFunction) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) DataType(org.apache.flink.table.types.DataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Row(org.apache.flink.types.Row)

Example 2 with GenericRecordBuilder

use of org.apache.pulsar.client.api.schema.GenericRecordBuilder in project pulsar by yahoo.

the class PulsarSinkTest method testWriteGenericRecords.

private void testWriteGenericRecords(ProcessingGuarantees guarantees) throws Exception {
    String defaultTopic = "default";
    PulsarSinkConfig sinkConfig = getPulsarConfigs();
    sinkConfig.setTopic(defaultTopic);
    sinkConfig.setTypeClassName(GenericRecord.class.getName());
    sinkConfig.setProcessingGuarantees(guarantees);
    PulsarClient client = getPulsarClient();
    PulsarSink pulsarSink = new PulsarSink(client, sinkConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    if (ProcessingGuarantees.ATMOST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtMostOnceProcessor);
    } else if (ProcessingGuarantees.ATLEAST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtLeastOnceProcessor);
    } else {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkEffectivelyOnceProcessor);
    }
    PulsarSinkProcessorBase processor = (PulsarSinkProcessorBase) pulsarSink.pulsarSinkProcessor;
    assertFalse(processor.publishProducers.containsKey(defaultTopic));
    String[] topics = { "topic-1", "topic-2", "topic-3" };
    for (String topic : topics) {
        RecordSchemaBuilder builder = SchemaBuilder.record("MyRecord");
        builder.field("number").type(SchemaType.INT32);
        builder.field("text").type(SchemaType.STRING);
        GenericSchema<GenericRecord> schema = Schema.generic(builder.build(SchemaType.AVRO));
        GenericRecordBuilder recordBuilder = schema.newRecordBuilder();
        recordBuilder.set("number", 1);
        recordBuilder.set("text", topic);
        GenericRecord genericRecord = recordBuilder.build();
        SinkRecord<GenericRecord> record = new SinkRecord<>(new Record<GenericRecord>() {

            @Override
            public Optional<String> getDestinationTopic() {
                return Optional.of(topic);
            }

            @Override
            public Schema<GenericRecord> getSchema() {
                return schema;
            }

            @Override
            public GenericRecord getValue() {
                return genericRecord;
            }

            @Override
            public Optional<String> getPartitionId() {
                return Optional.of(topic + "-id-1");
            }

            @Override
            public Optional<Long> getRecordSequence() {
                return Optional.of(1L);
            }
        }, genericRecord);
        pulsarSink.write(record);
        if (ProcessingGuarantees.EFFECTIVELY_ONCE == guarantees) {
            assertTrue(processor.publishProducers.containsKey(String.format("%s-%s-id-1", topic, topic)));
        } else {
            assertTrue(processor.publishProducers.containsKey(topic));
        }
        verify(client.newProducer(), times(1)).topic(argThat(otherTopic -> topic != null ? topic.equals(otherTopic) : defaultTopic.equals(otherTopic)));
        verify(client, times(1)).newProducer(argThat(otherSchema -> Objects.equals(otherSchema, schema)));
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Producer(org.apache.pulsar.client.api.Producer) Test(org.testng.annotations.Test) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) Record(org.apache.pulsar.functions.api.Record) BeforeMethod(org.testng.annotations.BeforeMethod) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Slf4j(lombok.extern.slf4j.Slf4j) SerDe(org.apache.pulsar.functions.api.SerDe) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Setter(lombok.Setter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assert.assertNull(org.testng.Assert.assertNull) Getter(lombok.Getter) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) ProcessingGuarantees(org.apache.pulsar.common.functions.FunctionConfig.ProcessingGuarantees) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) Schema(org.apache.pulsar.client.api.Schema) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) MessageId(org.apache.pulsar.client.api.MessageId) SinkContext(org.apache.pulsar.io.core.SinkContext) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaBuilder(org.apache.pulsar.client.api.schema.SchemaBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Optional(java.util.Optional) SinkContext(org.apache.pulsar.io.core.SinkContext) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) PulsarClient(org.apache.pulsar.client.api.PulsarClient) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord)

Example 3 with GenericRecordBuilder

use of org.apache.pulsar.client.api.schema.GenericRecordBuilder in project nosqlbench by nosqlbench.

the class AvroUtil method GetGenericRecord_PulsarAvro.

// Get a Pulsar Avro record from an OSS Avro schema record, matching a specific Pulsar Avro schema
public static GenericRecord GetGenericRecord_PulsarAvro(GenericAvroSchema pulsarGenericAvroSchema, org.apache.avro.generic.GenericRecord apacheAvroGenericRecord) {
    GenericRecordBuilder recordBuilder = pulsarGenericAvroSchema.newRecordBuilder();
    List<Field> fieldList = pulsarGenericAvroSchema.getFields();
    for (Field field : fieldList) {
        String fieldName = field.getName();
        recordBuilder.set(fieldName, apacheAvroGenericRecord.get(fieldName));
    }
    return recordBuilder.build();
}
Also used : Field(org.apache.pulsar.client.api.schema.Field) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder)

Example 4 with GenericRecordBuilder

use of org.apache.pulsar.client.api.schema.GenericRecordBuilder in project incubator-pulsar by apache.

the class PulsarSinkTest method testWriteGenericRecords.

private void testWriteGenericRecords(ProcessingGuarantees guarantees) throws Exception {
    String defaultTopic = "default";
    PulsarSinkConfig sinkConfig = getPulsarConfigs();
    sinkConfig.setTopic(defaultTopic);
    sinkConfig.setTypeClassName(GenericRecord.class.getName());
    sinkConfig.setProcessingGuarantees(guarantees);
    PulsarClient client = getPulsarClient();
    PulsarSink pulsarSink = new PulsarSink(client, sinkConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    if (ProcessingGuarantees.ATMOST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtMostOnceProcessor);
    } else if (ProcessingGuarantees.ATLEAST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtLeastOnceProcessor);
    } else {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkEffectivelyOnceProcessor);
    }
    PulsarSinkProcessorBase processor = (PulsarSinkProcessorBase) pulsarSink.pulsarSinkProcessor;
    assertFalse(processor.publishProducers.containsKey(defaultTopic));
    String[] topics = { "topic-1", "topic-2", "topic-3" };
    for (String topic : topics) {
        RecordSchemaBuilder builder = SchemaBuilder.record("MyRecord");
        builder.field("number").type(SchemaType.INT32);
        builder.field("text").type(SchemaType.STRING);
        GenericSchema<GenericRecord> schema = Schema.generic(builder.build(SchemaType.AVRO));
        GenericRecordBuilder recordBuilder = schema.newRecordBuilder();
        recordBuilder.set("number", 1);
        recordBuilder.set("text", topic);
        GenericRecord genericRecord = recordBuilder.build();
        SinkRecord<GenericRecord> record = new SinkRecord<>(new Record<GenericRecord>() {

            @Override
            public Optional<String> getDestinationTopic() {
                return Optional.of(topic);
            }

            @Override
            public Schema<GenericRecord> getSchema() {
                return schema;
            }

            @Override
            public GenericRecord getValue() {
                return genericRecord;
            }

            @Override
            public Optional<String> getPartitionId() {
                return Optional.of(topic + "-id-1");
            }

            @Override
            public Optional<Long> getRecordSequence() {
                return Optional.of(1L);
            }
        }, genericRecord);
        pulsarSink.write(record);
        if (ProcessingGuarantees.EFFECTIVELY_ONCE == guarantees) {
            assertTrue(processor.publishProducers.containsKey(String.format("%s-%s-id-1", topic, topic)));
        } else {
            assertTrue(processor.publishProducers.containsKey(topic));
        }
        verify(client.newProducer(), times(1)).topic(argThat(otherTopic -> topic != null ? topic.equals(otherTopic) : defaultTopic.equals(otherTopic)));
        verify(client, times(1)).newProducer(argThat(otherSchema -> Objects.equals(otherSchema, schema)));
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Producer(org.apache.pulsar.client.api.Producer) Test(org.testng.annotations.Test) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) Record(org.apache.pulsar.functions.api.Record) BeforeMethod(org.testng.annotations.BeforeMethod) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Slf4j(lombok.extern.slf4j.Slf4j) SerDe(org.apache.pulsar.functions.api.SerDe) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Setter(lombok.Setter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assert.assertNull(org.testng.Assert.assertNull) Getter(lombok.Getter) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) ProcessingGuarantees(org.apache.pulsar.common.functions.FunctionConfig.ProcessingGuarantees) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) Schema(org.apache.pulsar.client.api.Schema) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) MessageId(org.apache.pulsar.client.api.MessageId) SinkContext(org.apache.pulsar.io.core.SinkContext) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaBuilder(org.apache.pulsar.client.api.schema.SchemaBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Optional(java.util.Optional) SinkContext(org.apache.pulsar.io.core.SinkContext) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) PulsarClient(org.apache.pulsar.client.api.PulsarClient) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord)

Example 5 with GenericRecordBuilder

use of org.apache.pulsar.client.api.schema.GenericRecordBuilder in project pulsar by apache.

the class PulsarSinkTest method testWriteGenericRecords.

private void testWriteGenericRecords(ProcessingGuarantees guarantees) throws Exception {
    String defaultTopic = "default";
    PulsarSinkConfig sinkConfig = getPulsarConfigs();
    sinkConfig.setTopic(defaultTopic);
    sinkConfig.setTypeClassName(GenericRecord.class.getName());
    sinkConfig.setProcessingGuarantees(guarantees);
    PulsarClient client = getPulsarClient();
    PulsarSink pulsarSink = new PulsarSink(client, sinkConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    if (ProcessingGuarantees.ATMOST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtMostOnceProcessor);
    } else if (ProcessingGuarantees.ATLEAST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtLeastOnceProcessor);
    } else {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkEffectivelyOnceProcessor);
    }
    PulsarSinkProcessorBase processor = (PulsarSinkProcessorBase) pulsarSink.pulsarSinkProcessor;
    assertFalse(processor.publishProducers.containsKey(defaultTopic));
    String[] topics = { "topic-1", "topic-2", "topic-3" };
    for (String topic : topics) {
        RecordSchemaBuilder builder = SchemaBuilder.record("MyRecord");
        builder.field("number").type(SchemaType.INT32);
        builder.field("text").type(SchemaType.STRING);
        GenericSchema<GenericRecord> schema = Schema.generic(builder.build(SchemaType.AVRO));
        GenericRecordBuilder recordBuilder = schema.newRecordBuilder();
        recordBuilder.set("number", 1);
        recordBuilder.set("text", topic);
        GenericRecord genericRecord = recordBuilder.build();
        SinkRecord<GenericRecord> record = new SinkRecord<>(new Record<GenericRecord>() {

            @Override
            public Optional<String> getDestinationTopic() {
                return Optional.of(topic);
            }

            @Override
            public Schema<GenericRecord> getSchema() {
                return schema;
            }

            @Override
            public GenericRecord getValue() {
                return genericRecord;
            }

            @Override
            public Optional<String> getPartitionId() {
                return Optional.of(topic + "-id-1");
            }

            @Override
            public Optional<Long> getRecordSequence() {
                return Optional.of(1L);
            }
        }, genericRecord);
        pulsarSink.write(record);
        if (ProcessingGuarantees.EFFECTIVELY_ONCE == guarantees) {
            assertTrue(processor.publishProducers.containsKey(String.format("%s-%s-id-1", topic, topic)));
        } else {
            assertTrue(processor.publishProducers.containsKey(topic));
        }
        verify(client.newProducer(), times(1)).topic(argThat(otherTopic -> topic != null ? topic.equals(otherTopic) : defaultTopic.equals(otherTopic)));
        verify(client, times(1)).newProducer(argThat(otherSchema -> Objects.equals(otherSchema, schema)));
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Producer(org.apache.pulsar.client.api.Producer) Test(org.testng.annotations.Test) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) Record(org.apache.pulsar.functions.api.Record) BeforeMethod(org.testng.annotations.BeforeMethod) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Slf4j(lombok.extern.slf4j.Slf4j) SerDe(org.apache.pulsar.functions.api.SerDe) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Setter(lombok.Setter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assert.assertNull(org.testng.Assert.assertNull) Getter(lombok.Getter) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) ProcessingGuarantees(org.apache.pulsar.common.functions.FunctionConfig.ProcessingGuarantees) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) Schema(org.apache.pulsar.client.api.Schema) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) MessageId(org.apache.pulsar.client.api.MessageId) SinkContext(org.apache.pulsar.io.core.SinkContext) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaBuilder(org.apache.pulsar.client.api.schema.SchemaBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Optional(java.util.Optional) SinkContext(org.apache.pulsar.io.core.SinkContext) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) PulsarClient(org.apache.pulsar.client.api.PulsarClient) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord)

Aggregations

GenericRecordBuilder (org.apache.pulsar.client.api.schema.GenericRecordBuilder)6 Slf4j (lombok.extern.slf4j.Slf4j)4 GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)4 GenericSchema (org.apache.pulsar.client.api.schema.GenericSchema)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Getter (lombok.Getter)3 Setter (lombok.Setter)3 Consumer (org.apache.pulsar.client.api.Consumer)3 ConsumerBuilder (org.apache.pulsar.client.api.ConsumerBuilder)3 MessageId (org.apache.pulsar.client.api.MessageId)3 Producer (org.apache.pulsar.client.api.Producer)3 ProducerBuilder (org.apache.pulsar.client.api.ProducerBuilder)3 PulsarClient (org.apache.pulsar.client.api.PulsarClient)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 Schema (org.apache.pulsar.client.api.Schema)3 TypedMessageBuilder (org.apache.pulsar.client.api.TypedMessageBuilder)3