private Object getExplicitValue(final RecordField field, final Map<String, Object> values) { final String canonicalFieldName = field.getFieldName(); // We use containsKey here instead of just calling get() and checking for a null value // because if the true field name is set to null, we want to return null, rather than // what the alias points to. Likewise for a specific alias, since aliases are defined // in a List with a specific ordering. Object value = values.get(canonicalFieldName); if (value != null) { return value; } for (final String alias : field.getAliases()) { value = values.get(alias); if (value != null) { return value; } } return null; }
private List<String> getColumnNames(final RecordSchema schema) { final List<String> columnNames = new ArrayList<>(); for (final RecordField field : schema.getFields()) { columnNames.add(field.getFieldName()); for (final String alias : field.getAliases()) { columnNames.add(alias); } } return columnNames; }
private boolean isFieldPresent(final RecordField field, final Record record) { final Set<String> rawFieldNames = record.getRawFieldNames(); if (rawFieldNames.contains(field.getFieldName())) { return true; } for (final String alias : field.getAliases()) { if (rawFieldNames.contains(alias)) { return true; } } return false; }
private boolean recordHasField(RecordField field, Record record) { Set<String> recordFieldNames = record.getRawFieldNames(); if (recordFieldNames.contains(field.getFieldName())) { return true; } for (String alias : field.getAliases()) { if (recordFieldNames.contains(alias)) { return true; } } return false; } }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } RecordField other = (RecordField) obj; return dataType.equals(other.getDataType()) && fieldName.equals(other.getFieldName()) && aliases.equals(other.getAliases()) && Objects.equals(defaultValue, other.defaultValue) && nullable == other.nullable; }
private Optional<RecordField> resolveField(final RecordField field) { Optional<RecordField> resolved = schema.getField(field.getFieldName()); if (resolved.isPresent()) { return resolved; } for (final String alias : field.getAliases()) { resolved = schema.getField(alias); if (resolved.isPresent()) { return resolved; } } return Optional.empty(); }
public void setFields(final List<RecordField> fields) { if (this.fields != null) { throw new IllegalArgumentException("Fields have already been set."); } this.fields = Collections.unmodifiableList(new ArrayList<>(fields)); this.fieldMap = new HashMap<>(fields.size() * 2); for (final RecordField field : fields) { RecordField previousValue = fieldMap.put(field.getFieldName(), field); if (previousValue != null) { throw new IllegalArgumentException("Two fields are given with the same name (or alias) of '" + field.getFieldName() + "'"); } for (final String alias : field.getAliases()) { previousValue = fieldMap.put(alias, field); if (previousValue != null) { throw new IllegalArgumentException("Two fields are given with the same name (or alias) of '" + field.getFieldName() + "'"); } } } }
private static boolean isMergeRequired(final RecordField thisField, final RecordField otherField) { if (!thisField.getDataType().equals(otherField.getDataType())) { return true; } if (!thisField.getAliases().equals(otherField.getAliases())) { return true; } if (!Objects.equals(thisField.getDefaultValue(), otherField.getDefaultValue())) { return true; } return false; }
private JsonNode getChildNode(final JsonNode jsonNode, final RecordField field) { if (jsonNode.has(field.getFieldName())) { return jsonNode.get(field.getFieldName()); } for (final String alias : field.getAliases()) { if (jsonNode.has(alias)) { return jsonNode.get(alias); } } return null; }
private JsonNode getChildNode(final JsonNode jsonNode, final RecordField field) { if (jsonNode.has(field.getFieldName())) { return jsonNode.get(field.getFieldName()); } for (final String alias : field.getAliases()) { if (jsonNode.has(alias)) { return jsonNode.get(alias); } } return null; }
for (final String alias : field.getAliases()) { value = converted.get(alias); if (value != null) {
/** * Method that attempts to map a record field into a provided schema * @param avroSchema - Schema to map into * @param recordField - The field of the record to be mapped * @return Pair with the LHS being the field name and RHS being the mapped field from the schema */ protected static Pair<String, Field> lookupField(final Schema avroSchema, final RecordField recordField) { String fieldName = recordField.getFieldName(); // Attempt to locate the field as is in a true 1:1 mapping with the same name Field field = avroSchema.getField(fieldName); if (field == null) { // No straight mapping was found, so check the aliases to see if it can be mapped for(final String alias: recordField.getAliases()) { field = avroSchema.getField(alias); if (field != null) { fieldName = alias; break; } } } return new ImmutablePair<>(fieldName, field); }
for (final String alias : field.getAliases()) { fieldIndices.put(alias, index); for (final String alias : otherField.getAliases()) { fieldIndex = fieldIndices.get(alias); if (fieldIndex != null) {
for (final String alias : recordField.getAliases()) { value = avroRecord.get(alias); if (value != null) {
public static RecordField merge(final RecordField thisField, final RecordField otherField) { final String fieldName = thisField.getFieldName(); final Set<String> aliases = new HashSet<>(); aliases.addAll(thisField.getAliases()); aliases.addAll(otherField.getAliases()); final Object defaultValue; if (thisField.getDefaultValue() == null && otherField.getDefaultValue() != null) { defaultValue = otherField.getDefaultValue(); } else { defaultValue = thisField.getDefaultValue(); } final DataType dataType; if (thisField.getDataType().equals(otherField.getDataType())) { dataType = thisField.getDataType(); } else { dataType = RecordFieldType.CHOICE.getChoiceDataType(thisField.getDataType(), otherField.getDataType()); } return new RecordField(fieldName, dataType, defaultValue, aliases, thisField.isNullable() || otherField.isNullable()); }
if (childField.getFieldName().equals(descendantName) || childField.getAliases().contains(descendantName)) { final Object value = record.getValue(descendantName); if (value != null) {
private static Field buildAvroField(final RecordField recordField) { final Schema schema = buildAvroSchema(recordField.getDataType(), recordField.getFieldName(), recordField.isNullable()); final Field field = new Field(recordField.getFieldName(), schema, null, recordField.getDefaultValue()); for (final String alias : recordField.getAliases()) { field.addAlias(alias); } return field; }
private static boolean isMergeRequired(final RecordField thisField, final RecordField otherField) { if (!thisField.getDataType().equals(otherField.getDataType())) { return true; } if (!thisField.getAliases().equals(otherField.getAliases())) { return true; } if (!Objects.equals(thisField.getDefaultValue(), otherField.getDefaultValue())) { return true; } return false; }
private JsonNode getChildNode(final JsonNode jsonNode, final RecordField field) { if (jsonNode.has(field.getFieldName())) { return jsonNode.get(field.getFieldName()); } for (final String alias : field.getAliases()) { if (jsonNode.has(alias)) { return jsonNode.get(alias); } } return null; }
private static Field buildAvroField(final RecordField recordField) { final Schema schema = buildAvroSchema(recordField.getDataType(), recordField.getFieldName(), recordField.isNullable()); final Field field = new Field(recordField.getFieldName(), schema, null, recordField.getDefaultValue()); for (final String alias : recordField.getAliases()) { field.addAlias(alias); } return field; }