private Map<String, String> toQueryMap(Map<String, String> queryMap, boolean normalized) { for (Map.Entry<String, Predicate> entry : constraints.entrySet()) { String name = entry.getKey(); Schema fieldSchema = SchemaUtil.fieldSchema(schema, strategy, name); if(normalized) { queryMap.put(name, Predicates.toNormalizedString(entry.getValue(), fieldSchema)); } else { queryMap.put(name, Predicates.toString(entry.getValue(), fieldSchema)); } } return queryMap; }
@SuppressWarnings("unchecked") public static <S> Class<? extends S> getSourceType(FieldPartitioner<S, ?> fp, Schema schema) { return (Class<S>) getClassForType(fieldSchema(schema, fp.getSourceName()).getType()); }
@SuppressWarnings("unchecked") public static <S, T> Class<? extends T> getPartitionType(FieldPartitioner<S, T> fp, Schema schema) { if (fp instanceof ProvidedFieldPartitioner) { // provided partitioners have no source field schema return fp.getType(); } Class<? extends S> inputType = (Class<S>) getClassForType( fieldSchema(schema, fp.getSourceName()).getType()); return fp.getType(inputType); }
/** * Checks that the type of each of {@code values} is consistent with the type of * field {@code fieldName} declared in the Avro schema (from {@code descriptor}). */ public static void checkTypeConsistency(Schema schema, PartitionStrategy strategy, String fieldName, Object... values) { Schema fieldSchema = fieldSchema(schema, strategy, fieldName); for (Object value : values) { // SpecificData#validate checks consistency for generic, reflect, // and specific models. Preconditions.checkArgument( SpecificData.get().validate(fieldSchema, value), "Value '%s' of type '%s' inconsistent with field schema %s.", value, value.getClass(), fieldSchema); } }
public static Constraints fromQueryMap(Schema schema, PartitionStrategy strategy, Map<String, String> query) { Map<String, Predicate> constraints = Maps.newLinkedHashMap(); Map<String, Object> provided = Maps.newHashMap(); for (Map.Entry<String, String> entry : query.entrySet()) { String name = entry.getKey(); if (SchemaUtil.isField(schema, strategy, name)) { Schema fieldSchema = SchemaUtil.fieldSchema(schema, strategy, name); Predicate predicate = Predicates.fromString( entry.getValue(), fieldSchema); constraints.put(name, predicate); if (predicate instanceof In) { Set values = Predicates.asSet((In) predicate); if (values.size() == 1) { provided.put(name, Iterables.getOnlyElement(values)); } } } } return new Constraints(schema, strategy, constraints, provided); }
/** * Returns a {@link Schema} for the given field name, which could be either a * schema field or a partition field. * * @param schema an entity Schema that will be partitioned * @param strategy a {@code PartitionStrategy} used to partition entities * @param name a schema or partition field name * @return a Schema for the partition or schema field */ public static Schema fieldSchema(Schema schema, PartitionStrategy strategy, String name) { if (strategy != null && Accessor.getDefault().hasPartitioner(strategy, name)) { return partitionFieldSchema(Accessor.getDefault().getPartitioner(strategy, name), schema); } Schema nested = fieldSchema(schema, name); if (nested != null) { return nested; } throw new IllegalArgumentException( "Not a schema or partition field: " + name); }
private static void checkPartitionStrategy( Schema schema, @Nullable PartitionStrategy strategy) { if (strategy == null) { return; } for (FieldPartitioner fp : strategy.getFieldPartitioners()) { if (fp instanceof ProvidedFieldPartitioner) { // provided partitioners are not based on the entity fields continue; } // check the entity is a record if there is a non-provided partitioner ValidationException.check(schema.getType() == Schema.Type.RECORD, "Cannot partition non-records: %s", schema); // the source name should be a field in the schema, but not necessarily // the record. Schema fieldSchema; try { fieldSchema = SchemaUtil.fieldSchema(schema, fp.getSourceName()); } catch (IllegalArgumentException e) { throw new ValidationException( "Cannot partition on " + fp.getSourceName(), e); } ValidationException.check( SchemaUtil.isConsistentWithExpectedType( fieldSchema.getType(), fp.getSourceType()), "Field type %s does not match partitioner %s", fieldSchema.getType(), fp); } } }
Set<String> keyMappedFields = Sets.newHashSet(); for (FieldMapping fm : mappings.getFieldMappings()) { Schema fieldSchema = SchemaUtil.fieldSchema(schema, fm.getFieldName()); ValidationException.check( SchemaUtil.isConsistentWithMappingType(
/** * Builds a Schema for the FieldPartitioner using the given Schema to * determine types not fixed by the FieldPartitioner. * * @param fp a FieldPartitioner * @param schema an entity Schema that will be partitioned * @return a Schema for the field partitioner */ public static Schema partitionFieldSchema(FieldPartitioner<?, ?> fp, Schema schema) { if (fp instanceof IdentityFieldPartitioner) { // copy the schema directly from the entity to preserve annotations return fieldSchema(schema, fp.getSourceName()); } else { Class<?> fieldType = getPartitionType(fp, schema); if (fieldType == Integer.class) { return Schema.create(Schema.Type.INT); } else if (fieldType == Long.class) { return Schema.create(Schema.Type.LONG); } else if (fieldType == String.class) { return Schema.create(Schema.Type.STRING); } else { throw new ValidationException( "Cannot encode partition " + fp.getName() + " with type " + fp.getSourceType() ); } } }