本文整理汇总了Java中org.apache.kafka.connect.data.SchemaBuilder.optional方法的典型用法代码示例。如果您正苦于以下问题:Java SchemaBuilder.optional方法的具体用法?Java SchemaBuilder.optional怎么用?Java SchemaBuilder.optional使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.kafka.connect.data.SchemaBuilder
的用法示例。
在下文中一共展示了SchemaBuilder.optional方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOrBuildSchema
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
private Schema getOrBuildSchema(Schema valueSchema) {
Schema updatedSchema = schemaUpdateCache.get(valueSchema);
if (updatedSchema != null)
return updatedSchema;
final SchemaBuilder builder;
if (wholeValueCastType != null) {
builder = SchemaUtil.copySchemaBasics(valueSchema, convertFieldType(wholeValueCastType));
} else {
builder = SchemaUtil.copySchemaBasics(valueSchema, SchemaBuilder.struct());
for (Field field : valueSchema.fields()) {
SchemaBuilder fieldBuilder =
convertFieldType(casts.containsKey(field.name()) ? casts.get(field.name()) : field.schema().type());
if (field.schema().isOptional())
fieldBuilder.optional();
if (field.schema().defaultValue() != null)
fieldBuilder.defaultValue(castValueToType(field.schema().defaultValue(), fieldBuilder.type()));
builder.field(field.name(), fieldBuilder.build());
}
}
if (valueSchema.isOptional())
builder.optional();
if (valueSchema.defaultValue() != null)
builder.defaultValue(castValueToType(valueSchema.defaultValue(), builder.type()));
updatedSchema = builder.build();
schemaUpdateCache.put(valueSchema, updatedSchema);
return updatedSchema;
}
示例2: convertFieldSchema
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
/**
* Convert the schema for a field of a Struct with a primitive schema to the schema to be used for the flattened
* version, taking into account that we may need to override optionality and default values in the flattened version
* to take into account the optionality and default values of parent/ancestor schemas
* @param orig the original schema for the field
* @param optional whether the new flattened field should be optional
* @param defaultFromParent the default value either taken from the existing field or provided by the parent
*/
private Schema convertFieldSchema(Schema orig, boolean optional, Object defaultFromParent) {
// Note that we don't use the schema translation cache here. It might save us a bit of effort, but we really
// only care about caching top-level schema translations.
final SchemaBuilder builder = SchemaUtil.copySchemaBasics(orig);
if (optional)
builder.optional();
if (defaultFromParent != null)
builder.defaultValue(defaultFromParent);
return builder.build();
}
示例3: copySchemaBasics
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
private static SchemaBuilder copySchemaBasics(Schema source, SchemaBuilder target) {
if (source.isOptional()) {
target.optional();
}
if (source.defaultValue() != null && source.type() != Schema.Type.STRUCT) {
final Object preProcessedDefaultValue = preProcessValue(source.defaultValue(), source, target);
target.defaultValue(preProcessedDefaultValue);
}
return target;
}
示例4: buildStringSchema
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
/**
* Build Kafka string schema definition
*
* @param optional Whether or not the field is optional
* @param backwards Whether or not this field needs to be backwards
* compatible
*
* @return Kafka string schema
*/
private Schema buildStringSchema (boolean optional, boolean backwards) {
SchemaBuilder fieldBuilder = SchemaBuilder.string();
if (optional) {
fieldBuilder.optional();
}
if (backwards) {
/* add empty string default for missing values */
fieldBuilder.defaultValue(new String(""));
}
return fieldBuilder.build();
}
示例5: buildTimestampSchema
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
/**
* Build Kafka timestamp schema definition
*
* @param optional Whether or not the field is optional
* @param backwards Whether or not this field needs to be backwards
* compatible
*
* @return Kafka timestamp schema
*/
private Schema buildTimestampSchema (boolean optional, boolean backwards) {
SchemaBuilder fieldBuilder = Timestamp.builder();
if (optional) {
fieldBuilder.optional();
}
if (backwards) {
/* add epoch start as default for missing values */
fieldBuilder.defaultValue(new Date(0));
}
return fieldBuilder.build();
}
示例6: buildBytesSchema
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
/**
* Build Kafka raw bytes schema definition
*
* @param optional Whether or not the field is nullable
* @param backwards Whether or not this field needs to be backwards
* compatible
*
* @return Kafka bytes schema
*/
private Schema buildBytesSchema (boolean optional, boolean backwards) {
SchemaBuilder fieldBuilder = SchemaBuilder.bytes();
if (optional) {
fieldBuilder.optional();
}
if (backwards) {
/* add empty byte as default for missing values */
fieldBuilder.defaultValue(new byte[] {});
}
return fieldBuilder.build();
}
示例7: copySchemaBasics
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
private SchemaBuilder copySchemaBasics(Schema source, SchemaBuilder target) {
if (source.isOptional()) {
target.optional();
}
if (source.defaultValue() != null && source.type() != Schema.Type.STRUCT) {
final Object defaultVal = preProcessValue(source.defaultValue(), source, target);
target.defaultValue(defaultVal);
}
return target;
}
示例8: generateSchema
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
Schema generateSchema(ResultSet resultSet, final String columnName) throws SQLException {
SchemaBuilder builder = null;
String dataType = resultSet.getString(2);
int scale = resultSet.getInt(3);
boolean nullable = "Y".equalsIgnoreCase(resultSet.getString(4));
String comments = resultSet.getString(5);
if (TYPE_LOOKUP.containsKey(dataType)) {
Schema.Type type = TYPE_LOOKUP.get(dataType);
builder = SchemaBuilder.type(type);
} else if ("NUMBER".equals(dataType)) {
builder = Decimal.builder(scale);
} else if (matches(TIMESTAMP_PATTERN, dataType)) {
builder = Timestamp.builder();
} else if (matches(TIMESTAMP_WITH_LOCAL_TIMEZONE, dataType)) {
builder = Timestamp.builder();
} else if (matches(TIMESTAMP_WITH_TIMEZONE, dataType)) {
builder = Timestamp.builder();
} else if ("DATE".equals(dataType)) {
builder = Timestamp.builder();
} else {
String message = String.format("Could not determine schema type for column %s. dataType = %s", columnName, dataType);
throw new DataException(message);
}
if (nullable) {
builder.optional();
}
if (!Strings.isNullOrEmpty(comments)) {
builder.doc(comments);
}
builder.parameters(
ImmutableMap.of(Change.ColumnValue.COLUMN_NAME, columnName)
);
return builder.build();
}
示例9: build
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
public Schema build() {
log.trace(this.toString());
SchemaBuilder builder;
switch (this.type) {
case MAP:
Preconditions.checkNotNull(this.keySchema, "keySchema cannot be null.");
Preconditions.checkNotNull(this.valueSchema, "valueSchema cannot be null.");
builder = SchemaBuilder.map(this.keySchema, this.valueSchema);
break;
case ARRAY:
Preconditions.checkNotNull(this.valueSchema, "valueSchema cannot be null.");
builder = SchemaBuilder.array(this.valueSchema);
break;
default:
builder = SchemaBuilder.type(this.type);
break;
}
if (Schema.Type.STRUCT == this.type) {
for (Map.Entry<String, Schema> kvp : this.fieldSchemas.entrySet()) {
builder.field(kvp.getKey(), kvp.getValue());
}
}
if (!Strings.isNullOrEmpty(this.name)) {
builder.name(this.name);
}
if (!Strings.isNullOrEmpty(this.doc)) {
builder.doc(this.doc);
}
if (null != this.defaultValue) {
Object value;
switch (this.type) {
case INT8:
value = ((Number) this.defaultValue).byteValue();
break;
case INT16:
value = ((Number) this.defaultValue).shortValue();
break;
case INT32:
value = ((Number) this.defaultValue).intValue();
break;
case INT64:
value = ((Number) this.defaultValue).longValue();
break;
case FLOAT32:
value = ((Number) this.defaultValue).floatValue();
break;
case FLOAT64:
value = ((Number) this.defaultValue).doubleValue();
break;
default:
value = this.defaultValue;
break;
}
builder.defaultValue(value);
}
if (null != this.parameters) {
builder.parameters(this.parameters);
}
if (this.isOptional) {
builder.optional();
}
if (null != this.version) {
builder.version(this.version);
}
return builder.build();
}
示例10: buildDecimalSchema
import org.apache.kafka.connect.data.SchemaBuilder; //导入方法依赖的package包/类
/**
* Build decimal schema from scale and precision
*
* @param scale The numeric scale
* @param precision The numeric precision
* @param optional Whether or not the field is optional
* @param backwards Whether or not this field needs to be backwards
* compatible
*
* @return Kafka decimal schema
*/
private Schema buildDecimalSchema (
int scale,
int precision,
boolean optional,
boolean backwards
) {
SchemaBuilder fieldBuilder = null;
if (
scale > 0 ||
precision <= 0 ||
precision - scale >= ColumnDataDecoder.NUMBER_LONG_MAX_PRECISION
) {
/* decimal type */
fieldBuilder = Decimal.builder(scale);
if (optional) {
fieldBuilder.optional();
}
if (backwards) {
/* add zero default for missing values */
fieldBuilder.defaultValue(BigDecimal.ZERO.setScale(scale));
}
}
else if (
scale <= 0 &&
precision - Math.abs (scale) <
ColumnDataDecoder.NUMBER_INTEGER_MAX_PRECISION
) {
/* integer data type */
fieldBuilder = SchemaBuilder.int32();
if (optional) {
fieldBuilder.optional();
}
if (backwards) {
/* add zero default for missing values */
fieldBuilder.defaultValue(0);
}
}
else {
/* long data type */
fieldBuilder = SchemaBuilder.int64();
if (optional) {
fieldBuilder.optional();
}
if (backwards) {
/* add zero default for missing values */
fieldBuilder.defaultValue(0L);
}
}
return fieldBuilder.build();
}