main.java.com.ibm.cloudant.kafka.schema.JsonArrayAsSchemaArray Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kafka-connect-cloudant Show documentation
Show all versions of kafka-connect-cloudant Show documentation
Apache Kafka Connect API connector for Cloudant
The newest version!
/*
* Copyright © 2018 IBM Corp. All rights reserved.
*
* Licensed 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 com.ibm.cloudant.kafka.schema;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ibm.cloudant.kafka.common.MessageKey;
import com.ibm.cloudant.kafka.common.utils.ResourceBundleUtil;
import org.apache.kafka.connect.data.Field;
import org.apache.kafka.connect.data.Schema;
import org.apache.kafka.connect.data.SchemaBuilder;
import org.apache.kafka.connect.data.Struct;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A class for converting a JsonArray into a Schema.Type.ARRAY
*/
public class JsonArrayAsSchemaArray extends
JsonArrayConverter {
protected JsonArrayAsSchemaArray(JsonArray array) {
super(array);
}
@Override
protected SchemaValue generate() {
// If there is no element, i.e. an empty array, then we assume String type elements.
if (fields.size() == 0) {
return new SchemaValue(SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA),
Collections.EMPTY_LIST);
}
// Otherwise check the schemas because we can't do mixed type arrays in Kafka's
// Schema.Type.ARRAY
List schemas = fields.values().stream().map(sv -> sv.schema).distinct().collect
(Collectors.toList());
Schema schemaToUse;
boolean remappingRequired = false;
if (schemas.size() > 1) {
// There were multiple schemas in the array
if (schemas.stream().allMatch(s -> Schema.Type.STRUCT == s.type())) {
// All were structs, but they didn't match
// We can merge the schemas for all the struct types in the array
schemaToUse = mergeStructSchemas(schemas);
// However, a new merged schema means we need to re-evaluate all the values to be
// structs created from the new merged schema
remappingRequired = true;
} else {
// The schemas were not all of the same type, this isn't supported in Kafka arrays
throw new IllegalArgumentException(ResourceBundleUtil.get(MessageKey
.CLOUDANT_STRUCT_SCHEMA_JSON_MIXED_ARRAY));
}
} else {
// There was one distinct schema
schemaToUse = schemas.get(0);
}
Stream