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

The following examples show how to use org.apache.pulsar.client.api.Schema#STRING . 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: 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 2
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 3
Source File: SinkTester.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public Schema<?> getInputTopicSchema() {
    return Schema.STRING;
}