Java Code Examples for org.apache.pulsar.client.api.Schema#BYTES

The following examples show how to use org.apache.pulsar.client.api.Schema#BYTES . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RawReaderImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
RawConsumerImpl(PulsarClientImpl client, ConsumerConfigurationData<byte[]> conf,
        CompletableFuture<Consumer<byte[]>> consumerFuture) {
    super(client,
        conf.getSingleTopic(),
        conf,
        client.externalExecutorProvider().getExecutor(),
        TopicName.getPartitionIndex(conf.getSingleTopic()),
        false,
        consumerFuture,
        MessageId.earliest,
        0 /* startMessageRollbackDurationInSec */,
        Schema.BYTES, null,
        true
    );
    incomingRawMessages = new GrowableArrayBlockingQueue<>();
    pendingRawReceives = new ConcurrentLinkedQueue<>();
}
 
Example 2
Source File: PulsarSink.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@VisibleForTesting
Schema<T> initializeSchema() throws ClassNotFoundException {
    if (StringUtils.isEmpty(this.pulsarSinkConfig.getTypeClassName())) {
        return (Schema<T>) Schema.BYTES;
    }

    Class<?> typeArg = Reflections.loadClass(this.pulsarSinkConfig.getTypeClassName(), functionClassLoader);
    if (Void.class.equals(typeArg)) {
        // return type is 'void', so there's no schema to check
        return null;
    }
    ConsumerConfig consumerConfig = new ConsumerConfig();
    consumerConfig.setSchemaProperties(pulsarSinkConfig.getSchemaProperties());
    if (!StringUtils.isEmpty(pulsarSinkConfig.getSchemaType())) {
        consumerConfig.setSchemaType(pulsarSinkConfig.getSchemaType());
        return (Schema<T>) topicSchema.getSchema(pulsarSinkConfig.getTopic(), typeArg,
                consumerConfig, false);
    } else {
        consumerConfig.setSchemaType(pulsarSinkConfig.getSerdeClassName());
        return (Schema<T>) topicSchema.getSchema(pulsarSinkConfig.getTopic(), typeArg,
                consumerConfig, false, functionClassLoader);
    }
}
 
Example 3
Source File: MessageRecordUtils.java    From kop with Apache License 2.0 5 votes vote down vote up
public static MessageImpl<byte[]> recordToEntry(Record record) {
    @SuppressWarnings("unchecked")
    TypedMessageBuilderImpl<byte[]> builder = new TypedMessageBuilderImpl(null, Schema.BYTES);

    // key
    if (record.hasKey()) {
        byte[] key = new byte[record.keySize()];
        record.key().get(key);
        builder.keyBytes(key);
        // reuse ordering key to avoid converting string < > bytes
        builder.orderingKey(key);
    }

    // value
    if (record.hasValue()) {
        byte[] value = new byte[record.valueSize()];
        record.value().get(value);
        builder.value(value);
    } else {
        builder.value(new byte[0]);
    }

    // sequence
    if (record.sequence() >= 0) {
        builder.sequenceId(record.sequence());
    }

    // timestamp
    if (record.timestamp() >= 0) {
        builder.eventTime(record.timestamp());
    }

    // header
    for (Header h : record.headers()) {
        builder.property(h.key(),
            new String(h.value(), UTF_8));
    }

    return (MessageImpl<byte[]>) builder.getMessage();
}
 
Example 4
Source File: KafkaConnectSource.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public Schema<byte[]> getKeySchema() {
    if (jsonWithEnvelope || keySchema == null) {
        return Schema.BYTES;
    } else {
        return keySchema;
    }
}
 
Example 5
Source File: KafkaConnectSource.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public Schema<byte[]> getValueSchema() {
    if (jsonWithEnvelope || valueSchema == null) {
        return Schema.BYTES;
    } else {
        return valueSchema;
    }
}
 
Example 6
Source File: TopicSchema.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static <T> Schema<T> newSchemaInstance(Class<T> clazz, SchemaType type, ConsumerConfig conf) {
    switch (type) {
    case NONE:
        return (Schema<T>) Schema.BYTES;

    case AUTO_CONSUME:
    case AUTO:
        return (Schema<T>) Schema.AUTO_CONSUME();

    case STRING:
        return (Schema<T>) Schema.STRING;

    case AVRO:
        return AvroSchema.of(SchemaDefinition.<T>builder()
                .withProperties(new HashMap<>(conf.getSchemaProperties()))
                .withPojo(clazz).build());

    case JSON:
        return JSONSchema.of(SchemaDefinition.<T>builder().withPojo(clazz).build());

    case KEY_VALUE:
        return (Schema<T>)Schema.KV_BYTES();

    case PROTOBUF:
        return ProtobufSchema.ofGenericClass(clazz, new HashMap<>());

    default:
        throw new RuntimeException("Unsupported schema type" + type);
    }
}
 
Example 7
Source File: PartitionedProducerImplTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() {
    client = mock(PulsarClientImpl.class);
    schema = mock(Schema.class);
    producerInterceptors = mock(ProducerInterceptors.class);
    producerCreatedFuture = mock(CompletableFuture.class);
    ClientConfigurationData clientConfigurationData = mock(ClientConfigurationData.class);
    Timer timer = mock(Timer.class);

    producerBuilderImpl = new ProducerBuilderImpl(client, Schema.BYTES);

    when(client.getConfiguration()).thenReturn(clientConfigurationData);
    when(client.timer()).thenReturn(timer);
    when(client.newProducer()).thenReturn(producerBuilderImpl);
}
 
Example 8
Source File: PulsarConsumerSourceTests.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private static Message<byte[]> createMessage(String content, String messageId) {
    return new MessageImpl<byte[]>("my-topic", messageId, Collections.emptyMap(),
                                   content.getBytes(), Schema.BYTES, PulsarApi.MessageMetadata.newBuilder());
}
 
Example 9
Source File: PulsarAppenderTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
MockedMessageBuilder() {
    super(null, Schema.BYTES);
}
 
Example 10
Source File: PulsarFunctionsTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void testExclamationFunction(Runtime runtime,
                                     boolean isTopicPattern,
                                     boolean pyZip,
                                     boolean withExtraDeps) throws Exception {
    if (functionRuntimeType == FunctionRuntimeType.THREAD && runtime == Runtime.PYTHON) {
        // python can only run on process mode
        return;
    }

    if (pulsarCluster == null) {
        super.setupCluster();
        super.setupFunctionWorkers();
    }

    Schema<?> schema;
    if (Runtime.JAVA == runtime) {
        schema = Schema.STRING;
    } else {
        schema = Schema.BYTES;
    }

    String inputTopicName = "persistent://public/default/test-exclamation-" + runtime + "-input-" + randomName(8);
    String outputTopicName = "test-exclamation-" + runtime + "-output-" + randomName(8);
    try (PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(pulsarCluster.getHttpServiceUrl()).build()) {
        admin.topics().createNonPartitionedTopic(inputTopicName);
        admin.topics().createNonPartitionedTopic(outputTopicName);
    }
    if (isTopicPattern) {
        @Cleanup PulsarClient client = PulsarClient.builder()
                .serviceUrl(pulsarCluster.getPlainTextServiceUrl())
                .build();

        @Cleanup Consumer<?> consumer1 = client.newConsumer(schema)
                .topic(inputTopicName + "1")
                .subscriptionType(SubscriptionType.Exclusive)
                .subscriptionName("test-sub")
                .subscribe();

        @Cleanup Consumer<?> consumer2 = client.newConsumer(schema)
                .topic(inputTopicName + "2")
                .subscriptionType(SubscriptionType.Exclusive)
                .subscriptionName("test-sub")
                .subscribe();
        inputTopicName = inputTopicName + ".*";
    }
    String functionName = "test-exclamation-fn-" + randomName(8);
    final int numMessages = 10;

    // submit the exclamation function
    submitExclamationFunction(
        runtime, inputTopicName, outputTopicName, functionName, pyZip, withExtraDeps, schema);

    // get function info
    getFunctionInfoSuccess(functionName);

    // get function stats
    getFunctionStatsEmpty(functionName);

    // publish and consume result
    if (Runtime.JAVA == runtime) {
        // java supports schema
        publishAndConsumeMessages(inputTopicName, outputTopicName, numMessages);
    } else {
        // python doesn't support schema
        publishAndConsumeMessagesBytes(inputTopicName, outputTopicName, numMessages);
    }

    // get function status
    getFunctionStatus(functionName, numMessages, true);

    // get function stats
    getFunctionStats(functionName, numMessages);

    // update parallelism
    updateFunctionParallelism(functionName, 2);

    //get function status
    getFunctionStatus(functionName, 0, true, 2);

    // delete function
    deleteFunction(functionName);

    // get function info
    getFunctionInfoNotFound(functionName);

    // make sure subscriptions are cleanup
    checkSubscriptionsCleanup(inputTopicName);

}
 
Example 11
Source File: PulsarClientImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ProducerBuilder<byte[]> newProducer() {
    return new ProducerBuilderImpl<>(this, Schema.BYTES);
}
 
Example 12
Source File: PulsarClientImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ConsumerBuilder<byte[]> newConsumer() {
    return new ConsumerBuilderImpl<>(this, Schema.BYTES);
}
 
Example 13
Source File: PulsarClientImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public ReaderBuilder<byte[]> newReader() {
    return new ReaderBuilderImpl<>(this, Schema.BYTES);
}