GenericJsonSchema.java 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.pulsar.client.impl.schema.generic;

21
import java.util.stream.Collectors;
22

23 24
import lombok.extern.slf4j.Slf4j;
import org.apache.avro.Schema;
25
import org.apache.pulsar.client.api.schema.Field;
26
import org.apache.pulsar.client.api.schema.GenericRecord;
27
import org.apache.pulsar.client.api.schema.GenericRecordBuilder;
28
import org.apache.pulsar.client.api.schema.SchemaReader;
29
import org.apache.pulsar.client.impl.schema.SchemaUtils;
30 31 32 33 34
import org.apache.pulsar.common.schema.SchemaInfo;

/**
 * A generic json schema.
 */
35
@Slf4j
36
class GenericJsonSchema extends GenericSchemaImpl {
37 38

    public GenericJsonSchema(SchemaInfo schemaInfo) {
39 40 41 42 43 44
        this(schemaInfo, true);
    }

    GenericJsonSchema(SchemaInfo schemaInfo,
                      boolean useProvidedSchemaAsReaderSchema) {
        super(schemaInfo, useProvidedSchemaAsReaderSchema);
45 46
        setWriter(new GenericJsonWriter());
        setReader(new GenericJsonReader(fields));
47 48 49
    }

    @Override
50
    protected SchemaReader<GenericRecord> loadReader(byte[] schemaVersion) {
51
        SchemaInfo schemaInfo = getSchemaInfoByVersion(schemaVersion);
52
        if (schemaInfo != null) {
53 54 55 56 57 58 59 60 61
            log.info("Load schema reader for version({}), schema is : {}",
                SchemaUtils.getStringSchemaVersion(schemaVersion),
                schemaInfo.getSchemaDefinition());
            Schema readerSchema;
            if (useProvidedSchemaAsReaderSchema) {
                readerSchema = schema;
            } else {
                readerSchema = parseAvroSchema(schemaInfo.getSchemaDefinition());
            }
62
            return new GenericJsonReader(schemaVersion,
63
                    readerSchema.getFields()
64 65
                            .stream()
                            .map(f -> new Field(f.name(), f.pos()))
66
                            .collect(Collectors.toList()));
67
        } else {
68 69 70
            log.warn("No schema found for version({}), use latest schema : {}",
                SchemaUtils.getStringSchemaVersion(schemaVersion),
                this.schemaInfo.getSchemaDefinition());
71
            return reader;
72 73
        }
    }
74 75 76 77 78

    @Override
    public GenericRecordBuilder newRecordBuilder() {
        throw new UnsupportedOperationException("Json Schema doesn't support record builder yet");
    }
79
}