@Override public TypeDescriptor<T> getEncodedTypeDescriptor() { if (typeDescriptor == null) { typeDescriptor = TypeDescriptor.of(type); } return typeDescriptor; }
/** * Registers the provided {@link Coder} for the given class. * * <p>Note that this is equivalent to {@code registerCoderForType(TypeDescriptor.of(clazz))}. See * {@link #registerCoderForType(TypeDescriptor, Coder)} for further details. */ public void registerCoderForClass(Class<?> clazz, Coder<?> coder) { registerCoderForType(TypeDescriptor.of(clazz), coder); }
/** * Get a schema for a given {@link Class} type. If no schema exists, throws {@link * NoSuchSchemaException}. */ public <T> Schema getSchema(Class<T> clazz) throws NoSuchSchemaException { return getSchema(TypeDescriptor.of(clazz)); }
/** Rerieve the function that converts an object of the specified type to a {@link Row} object. */ public <T> SerializableFunction<T, Row> getToRowFunction(Class<T> clazz) throws NoSuchSchemaException { return getToRowFunction(TypeDescriptor.of(clazz)); }
/** * Register a POJO type for automatic schema inference. * * <p>Currently schema field names will match field names in the POJO, and all fields must be * mutable (i.e. no final fields). */ public <T> void registerPOJO(Class<T> clazz) { registerPOJO(TypeDescriptor.of(clazz)); }
@Override public List<CoderProvider> getCoderProviders() { return ImmutableList.of( CoderProviders.forCoder( TypeDescriptor.of(PubsubMessage.class), PubsubMessageWithAttributesCoder.of())); } }
/** * Returns a {@link SerializableCoder} instance for the provided element class. * * @param <T> the element type */ public static <T extends Serializable> SerializableCoder<T> of(Class<T> clazz) { checkEqualsMethodDefined(clazz); return new SerializableCoder<>(clazz, TypeDescriptor.of(clazz)); }
@VisibleForTesting static DoFnSignature.GetRestrictionCoderMethod analyzeGetRestrictionCoderMethod( ErrorReporter errors, TypeDescriptor<? extends DoFn> fnT, Method m) { errors.checkArgument(m.getParameterTypes().length == 0, "Must have zero arguments"); TypeDescriptor<?> resT = fnT.resolveType(m.getGenericReturnType()); errors.checkArgument( resT.isSubtypeOf(TypeDescriptor.of(Coder.class)), "Must return a Coder, but returns %s", formatType(resT)); return DoFnSignature.GetRestrictionCoderMethod.create(m, resT); }
public static void main(String[] args) { Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class); Pipeline p = Pipeline.create(options); SpannerConfig spannerConfig = SpannerConfig.create() .withInstanceId(options.getInstanceId()) .withDatabaseId(options.getDatabaseId()); // [START spanner_dataflow_readall] PCollection<Struct> allRecords = p.apply(SpannerIO.read() .withSpannerConfig(spannerConfig) .withQuery("SELECT t.table_name FROM information_schema.tables AS t WHERE t" + ".table_catalog = '' AND t.table_schema = ''")).apply( MapElements.into(TypeDescriptor.of(ReadOperation.class)) .via((SerializableFunction<Struct, ReadOperation>) input -> { String tableName = input.getString(0); return ReadOperation.create().withQuery("SELECT * FROM " + tableName); })).apply(SpannerIO.readAll().withSpannerConfig(spannerConfig)); // [END spanner_dataflow_readall] PCollection<Long> dbEstimatedSize = allRecords.apply(EstimateSize.create()) .apply(Sum.longsGlobally()); dbEstimatedSize.apply(ToString.elements()).apply(TextIO.write().to(options.getOutput()) .withoutSharding()); p.run().waitUntilFinish(); }
@Override public List<CoderProvider> getCoderProviders() { return ImmutableList.of( CoderProviders.forCoder(TypeDescriptor.of(ByteString.class), ByteStringCoder.of()), ProtoCoder.getCoderProvider()); } }
/** * A more general form of {@link #where(TypeParameter, TypeDescriptor)} that returns a new {@code * TypeDescriptor} by matching {@code formal} against {@code actual} to resolve type variables in * the current {@link TypeDescriptor}. */ @SuppressWarnings("unchecked") public TypeDescriptor<T> where(Type formal, Type actual) { TypeResolver resolver = new TypeResolver().where(formal, actual); return (TypeDescriptor<T>) TypeDescriptor.of(resolver.resolveType(token.getType())); }
@Override public List<CoderProvider> getCoderProviders() { return ImmutableList.of( CoderProviders.forCoder(TypeDescriptor.of(MyValueB.class), MyValueBCoder.INSTANCE)); } }
@Test public void testEncodedTypeDescriptor() throws Exception { assertThat(TEST_CODER.getEncodedTypeDescriptor(), equalTo(TypeDescriptor.of(byte[].class))); } }
@Test public void testEncodedTypeDescriptor() throws Exception { assertThat( TEST_CODER.getEncodedTypeDescriptor(), equalTo(TypeDescriptor.of(ReadableDuration.class))); } }
@Test public void testEncodedTypeDescriptor() throws Exception { assertThat(TEST_CODER.getEncodedTypeDescriptor(), equalTo(TypeDescriptor.of(Integer.class))); } }
@Test public void testEncodedTypeDescriptor() throws Exception { AvroCoder<Pojo> coder = AvroCoder.of(Pojo.class); assertThat(coder.getEncodedTypeDescriptor(), equalTo(TypeDescriptor.of(Pojo.class))); }
@Test public void testUnknown() throws Exception { thrown.expect(CannotProvideCoderException.class); new DefaultCoderProvider().coderFor(TypeDescriptor.of(Unknown.class), Collections.emptyList()); } }
@Test public void testEncodedTypeDescriptor() throws Exception { assertThat( SerializableCoder.of(MyRecord.class).getEncodedTypeDescriptor(), Matchers.equalTo(TypeDescriptor.of(MyRecord.class))); }
/** * Tests that the error message for a type variable includes a mention of where the type variable * was declared. */ @Test public void testTypeVariableErrorMessage() throws Exception { CoderRegistry registry = CoderRegistry.createDefault(); thrown.expect(CannotProvideCoderException.class); thrown.expectMessage("Unable to provide a Coder"); registry.getCoder(TypeDescriptor.of(TestGenericClass.class.getTypeParameters()[0])); }
@Test public void testNewTrackerWrongNumArguments() throws Exception { thrown.expectMessage("Must have a single argument"); DoFnSignatures.analyzeNewTrackerMethod( errors(), TypeDescriptor.of(FakeDoFn.class), new AnonymousMethod() { private SomeRestrictionTracker method(SomeRestriction restriction, Object extra) { return null; } }.getMethod()); }