All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.pulsar.common.schema.SchemaInfo Maven / Gradle / Ivy

There is a newer version: 4.0.1.1
Show newest version
/**
 * 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.common.schema;

import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.Base64;
import java.util.Collections;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.pulsar.client.internal.DefaultImplementation;
import org.apache.pulsar.common.classification.InterfaceAudience;
import org.apache.pulsar.common.classification.InterfaceStability;

/**
 * Information about the schema.
 */
@InterfaceAudience.Public
@InterfaceStability.Stable
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Builder
public class SchemaInfo {

    @EqualsAndHashCode.Exclude
    private String name;

    /**
     * The schema data in AVRO JSON format.
     */
    private byte[] schema;

    /**
     * The type of schema (AVRO, JSON, PROTOBUF, etc..).
     */
    private SchemaType type;

    /**
     * Additional properties of the schema definition (implementation defined).
     */
    @Builder.Default
    private Map properties = Collections.emptyMap();

    public String getSchemaDefinition() {
        if (null == schema) {
            return "";
        }

        switch (type) {
            case AVRO:
            case JSON:
            case PROTOBUF:
            case PROTOBUF_NATIVE:
                return new String(schema, UTF_8);
            case KEY_VALUE:
                KeyValue schemaInfoKeyValue =
                    DefaultImplementation.decodeKeyValueSchemaInfo(this);
                return DefaultImplementation.jsonifyKeyValueSchemaInfo(schemaInfoKeyValue);
            default:
                return Base64.getEncoder().encodeToString(schema);
        }
    }

    @Override
    public String toString(){
        return DefaultImplementation.jsonifySchemaInfo(this);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy