/** * Convert the given date into the type of the given field, and set the value for that field. * A Date can be converted into a long or a string. * This method does not support {@link LogicalType#DATE} * * @deprecated As of release 5.1.0, use {@link StructuredRecord.Builder#setDate(String, LocalDate)} instead. * * @param fieldName name of the field to set * @param date date value for the field * @return this builder * @throws UnexpectedFormatException if the field is not in the schema, or the field is not nullable but a null * value is given, or the date cannot be converted to the type for the field */ @Deprecated public Builder convertAndSet(String fieldName, @Nullable Date date) throws UnexpectedFormatException { return convertAndSet(fieldName, date, DEFAULT_FORMAT); }
/** * Convert the given date into the type of the given field, and set the value for that field. * A Date can be converted into a long or a string. * This method does not support {@link LogicalType#DATE} * * @deprecated As of release 5.1.0, use {@link StructuredRecord.Builder#setDate(String, LocalDate)} instead. * * @param fieldName name of the field to set * @param date date value for the field * @return this builder * @throws UnexpectedFormatException if the field is not in the schema, or the field is not nullable but a null * value is given, or the date cannot be converted to the type for the field */ @Deprecated public Builder convertAndSet(String fieldName, @Nullable Date date) throws UnexpectedFormatException { return convertAndSet(fieldName, date, DEFAULT_FORMAT); }
/** * Converts a delimited string to a {@link StructuredRecord} based on the schema. */ public static StructuredRecord fromDelimitedString(String delimitedString, String delimiter, Schema schema) { StructuredRecord.Builder builder = StructuredRecord.builder(schema); Iterator<Schema.Field> fields = schema.getFields().iterator(); for (String part : Splitter.on(delimiter).split(delimitedString)) { if (!part.isEmpty()) { builder.convertAndSet(fields.next().getName(), part); } } return builder.build(); }
String fieldName = field.getName(); if (excelColumnValueMap.containsKey(fieldName)) { builder.convertAndSet(fieldName, excelColumnValueMap.get(fieldName)); } else { builder.set(fieldName, NULL);
@Override public StructuredRecord make(Schema schema, Iterator<String> bodyFields) { StructuredRecord.Builder builder = StructuredRecord.builder(schema); Iterator<Schema.Field> fieldsIterator = schema.getFields().iterator(); while (fieldsIterator.hasNext()) { Schema.Field field = fieldsIterator.next(); Schema fieldSchema = field.getSchema(); String fieldName = field.getName(); if (isStringArray(fieldSchema)) { if (!fieldsIterator.hasNext()) { // only do varargs-style string array parsing on bodyField if it's the last field List<String> fields = Lists.newArrayList(bodyFields); builder.set(fieldName, fields.toArray(new String[fields.size()])); } else { throw new UnexpectedFormatException( String.format("string array type field '%s' must be the last schema field", fieldName)); } } else { // simple type (not string array) String bodyField = bodyFields.hasNext() ? bodyFields.next() : null; String val = parseBodyValue(bodyField, fieldSchema); builder.convertAndSet(fieldName, val); } } return builder.build(); } }
@Override public StructuredRecord make(Schema schema, Iterator<String> bodyFields) { StructuredRecord.Builder builder = StructuredRecord.builder(schema); Iterator<Schema.Field> fieldsIterator = schema.getFields().iterator(); while (fieldsIterator.hasNext()) { Schema.Field field = fieldsIterator.next(); Schema fieldSchema = field.getSchema(); String fieldName = field.getName(); if (isStringArray(fieldSchema)) { if (!fieldsIterator.hasNext()) { // only do varargs-style string array parsing on bodyField if it's the last field List<String> fields = Lists.newArrayList(bodyFields); builder.set(fieldName, fields.toArray(new String[fields.size()])); } else { throw new UnexpectedFormatException( String.format("string array type field '%s' must be the last schema field", fieldName)); } } else { // simple type (not string array) String bodyField = bodyFields.hasNext() ? bodyFields.next() : null; String val = parseBodyValue(bodyField, fieldSchema); builder.convertAndSet(fieldName, val); } } return builder.build(); } }
private void convertAndSet(StructuredRecord.Builder builder, String fieldName, Object val, Schema inputSchema, Schema outputSchema) { // guaranteed that if the input type is nullable, the output type is also nullable. Schema.Type inputType = inputSchema.getType(); Schema.Type outputType = outputSchema.getType(); if (inputSchema.isNullable()) { if (val == null) { builder.set(fieldName, null); return; } inputType = inputSchema.getNonNullable().getType(); outputType = outputSchema.getNonNullable().getType(); } // if the input is a string, try and do some sensible conversion if (inputType == Schema.Type.STRING) { builder.convertAndSet(fieldName, (String) val); } else { // otherwise, just try to cast it. builder.set(fieldName, convertPrimitive(val, inputType, outputType)); } }
@Override public StructuredRecord make(Schema schema, Iterator<String> bodyFields) { StructuredRecord.Builder builder = StructuredRecord.builder(schema); // TODO: only read what's necessary from event.getBody() (e.g. if mapping is "0:f0", then only read first entry) List<String> fields = Lists.newArrayList(Iterators.limit(bodyFields, lastMappingIndex + 1)); for (Schema.Field field : schema.getFields()) { Schema fieldSchema = field.getSchema(); String fieldName = field.getName(); int index = mapping.get(fieldName); if (index < fields.size()) { String val = parseBodyValue(fields.get(index), fieldSchema); builder.convertAndSet(fieldName, val); } } return builder.build(); } }
@Override public StructuredRecord read(StreamEvent event) throws UnexpectedFormatException { String bodyAsStr = Bytes.toString(event.getBody()); StructuredRecord.Builder builder = StructuredRecord.builder(schema); List<String> parts = getLogEntries(bodyAsStr); List<Schema.Field> fields = schema.getFields(); int index = 0; while (index < fields.size()) { Schema.Field field = fields.get(index); String val = (parts.size() < index || (parts.get(index).equals("-") && field.getSchema().getType() != Schema.Type.STRING)) ? null : parts.get(index); builder.convertAndSet(field.getName(), val); index++; } return builder.build(); }
@Override public StructuredRecord read(ByteBuffer input) throws UnexpectedFormatException { String bodyAsStr = Bytes.toString(input, StandardCharsets.UTF_8); StructuredRecord.Builder builder = StructuredRecord.builder(schema); List<String> parts = getLogEntries(bodyAsStr); List<Schema.Field> fields = schema.getFields(); int index = 0; while (index < fields.size()) { Schema.Field field = fields.get(index); String val = (parts.size() < index || (parts.get(index).equals("-") && field.getSchema().getType() != Schema.Type.STRING)) ? null : parts.get(index); builder.convertAndSet(field.getName(), val); index++; } return builder.build(); }
@Override public StructuredRecord make(Schema schema, Iterator<String> bodyFields) { StructuredRecord.Builder builder = StructuredRecord.builder(schema); // TODO: only read what's necessary from event.getBody() (e.g. if mapping is "0:f0", then only read first entry) List<String> fields = Lists.newArrayList(Iterators.limit(bodyFields, lastMappingIndex + 1)); for (Schema.Field field : schema.getFields()) { Schema fieldSchema = field.getSchema(); String fieldName = field.getName(); int index = mapping.get(fieldName); if (index < fields.size()) { String val = parseBodyValue(fields.get(index), fieldSchema); builder.convertAndSet(fieldName, val); } } return builder.build(); } }
@Override public StructuredRecord read(ByteBuffer input) throws UnexpectedFormatException { String bodyAsStr = Bytes.toString(input, StandardCharsets.UTF_8); StructuredRecord.Builder builder = StructuredRecord.builder(schema); Match gm = grok.match(bodyAsStr); gm.captures(); Map<String, Object> x = gm.toMap(); for (Schema.Field field : schema.getFields()) { String fieldName = field.getName(); Object value = x.get(fieldName); if (value != null) { builder.convertAndSet(fieldName, value.toString()); } } return builder.build(); }
@Override public StructuredRecord read(StreamEvent event) throws UnexpectedFormatException { String bodyAsStr = Bytes.toString(event.getBody(), Charsets.UTF_8); StructuredRecord.Builder builder = StructuredRecord.builder(schema); Match gm = grok.match(bodyAsStr); gm.captures(); Map<String, Object> x = gm.toMap(); for (Schema.Field field : schema.getFields()) { String fieldName = field.getName(); Object value = x.get(fieldName); if (value != null) { builder.convertAndSet(fieldName, value.toString()); } } return builder.build(); }
/** * Converts a delimited string to a {@link StructuredRecord} based on the schema. */ public static StructuredRecord fromDelimitedString(String delimitedString, String delimiter, Schema schema) { StructuredRecord.Builder builder = StructuredRecord.builder(schema); Iterator<Schema.Field> fields = schema.getFields().iterator(); for (String part : Splitter.on(delimiter).split(delimitedString)) { if (part.isEmpty()) { builder.set(fields.next().getName(), null); } else { builder.convertAndSet(fields.next().getName(), part); } } return builder.build(); }
/** * Converts a delimited string to a {@link StructuredRecord} based on the schema. */ public static StructuredRecord fromDelimitedString(String delimitedString, String delimiter, Schema schema) { StructuredRecord.Builder builder = StructuredRecord.builder(schema); Iterator<Schema.Field> fields = schema.getFields().iterator(); for (String part : Splitter.on(delimiter).split(delimitedString)) { if (part.isEmpty()) { builder.set(fields.next().getName(), null); } else { builder.convertAndSet(fields.next().getName(), part); } } return builder.build(); }
/** * Converts a delimited string to a {@link StructuredRecord} based on the schema. */ public static StructuredRecord fromDelimitedString(String delimitedString, String delimiter, Schema schema) { StructuredRecord.Builder builder = StructuredRecord.builder(schema); Iterator<Schema.Field> fields = schema.getFields().iterator(); for (String part : Splitter.on(delimiter).split(delimitedString)) { if (!part.isEmpty()) { builder.convertAndSet(fields.next().getName(), part); } } return builder.build(); }