private <S, T> T valueFor(FieldPartitioner<S, T> fp) { if (values.containsKey(fp.getName())) { return Conversions.convert(values.get(fp.getName()), fp.getType()); } else if (values.containsKey(fp.getSourceName())) { return fp.apply(Conversions.convert( values.get(fp.getSourceName()), fp.getSourceType())); } else { throw new IllegalStateException( "Cannot create StorageKey, missing data for field:" + fp.getName()); } } }
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); } } }
/** * Return the value of a {@code FieldPartitioner} field for this {@link Marker}. * * If the {@code Marker} has a value for the field's name, that value is * returned using {@link Marker#getAs(java.lang.String, java.lang.Class)}. If * the {@code Marker} only has a value for the the source field name, then * that value is retrieved using * {@link org.kitesdk.data.spi.Marker#getAs(java.lang.String, * java.lang.Class)} and the field's transformation is applied to it as the source value. * * @param fp a {@code FieldPartitioner} * @return the value of the field for this {@code marker}, or null * @since 0.9.0 */ @Nullable public <S, T> T valueFor(FieldPartitioner<S, T> fp) { if (has(fp.getName())) { return getAs(fp.getName(), fp.getType()); } else if (has(fp.getSourceName())) { return fp.apply(getAs(fp.getSourceName(), fp.getSourceType())); } else { return null; } }
/** * 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() ); } } }