@Override public Type getOwnerType() { return this.enclosingType == null ? null : this.enclosingType.toType(); } } // TypeRepresentation
/** * Set the class loader to be used by toType(), getRawType(), etc. * @param loader the class loader to use */ public void setClassLoader(ClassLoader loader) { this.classLoader = loader; // Note that this class is immutable after construction, except for the class loader // Thus we can pass down the loader to the owner and parameter types once now, no need to pass it down in toType() if (this.enclosingType != null) { this.enclosingType.setClassLoader(loader); } if (this.parameters != null) { for (TypeRepresentation param: this.parameters) { param.setClassLoader(loader); } } }
/** * @return the represented Type. Note that for a parametrized type, we can just return this, * because it implements the ParametrizedType interface. */ public Type toType() { if (this.isClass) { return this.getRawType(); } else { return this; } }
this.enclosingType = owner == null ? null : new TypeRepresentation(owner); Type[] typeArgs = pType.getActualTypeArguments(); this.parameters = new TypeRepresentation[typeArgs.length]; for (int i = 0; i < typeArgs.length; i++) { this.parameters[i] = new TypeRepresentation(typeArgs[i]);
@Test public void testEmptyValue() throws UnsupportedTypeException, IOException { Schema schema = new ReflectionSchemaGenerator().generate(RecordWithString.class); TypeRepresentation typeRep = new TypeRepresentation(RecordWithString.class); DatumWriter<RecordWithString> datumWriter = new ReflectionDatumWriter<>(schema); @SuppressWarnings("unchecked") ReflectionDatumReader<RecordWithString> datumReader = new ReflectionDatumReader<>( schema, (TypeToken<RecordWithString>) TypeToken.of(typeRep.toType())); RecordWithString record = new RecordWithString(); record.setA(42); record.setTheString(""); ByteArrayOutputStream bos = new ByteArrayOutputStream(); BinaryEncoder encoder = new BinaryEncoder(bos); datumWriter.encode(record, encoder); byte[] bytes = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); BinaryDecoder decoder = new BinaryDecoder(bis); RecordWithString rec = datumReader.read(decoder, schema); Assert.assertEquals(record.getA(), rec.getA()); Assert.assertEquals(record.getTheString(), rec.getTheString()); } }
/** * Sets the type of object stored in the table. The schema of the Hive table for an ObjectMappedTable * is derived from the object type set here and the row key explore name set by * {@link #setRowKeyExploreName(String)}. * * For example, if the type set here has three fields - "id", "name", and "price", the corresponding Hive table * for this Dataset will contain four columns - "rowkey", "id", "name", and "price". */ public Builder setType(Type type) throws UnsupportedTypeException { add(OBJECT_TYPE, gson.toJson(new TypeRepresentation(type))); add(OBJECT_SCHEMA, schemaGenerator.generate(type, false).toString()); return this; }
public Type getRecordType() { return typeRep.toType(); }
public IntegerStore(String name, KeyValueTable kvTable) throws UnsupportedTypeException { super(name, kvTable, new TypeRepresentation(Integer.class), new ReflectionSchemaGenerator().generate(Integer.class)); }
public ObjectStoreDataset(String name, KeyValueTable kvTable, TypeRepresentation typeRep, Schema schema, @Nullable ClassLoader classLoader) { super(name, kvTable); this.kvTable = kvTable; this.typeRep = typeRep; this.typeRep.setClassLoader(classLoader); this.schema = schema; this.datumWriter = new ReflectionDatumWriter<>(this.schema); }
public Type getRecordType() { return typeRep.toType(); }
/** * Creates properties for {@link ObjectStore} dataset instance. * * @param type type of objects to be stored in dataset * @return {@link DatasetProperties} for the dataset * @throws UnsupportedTypeException */ public static DatasetProperties objectStoreProperties(Type type, DatasetProperties props) throws UnsupportedTypeException { Schema schema = new ReflectionSchemaGenerator().generate(type); TypeRepresentation typeRep = new TypeRepresentation(type); return DatasetProperties.builder() .add("schema", schema.toString()) .add("type", new Gson().toJson(typeRep)) .addAll(props.getProperties()) .build(); }
public ObjectStoreDataset(String name, KeyValueTable kvTable, TypeRepresentation typeRep, Schema schema, @Nullable ClassLoader classLoader) { super(name, kvTable); this.kvTable = kvTable; this.typeRep = typeRep; this.typeRep.setClassLoader(classLoader); this.schema = schema; this.datumWriter = new ReflectionDatumWriter<>(this.schema); }
@SuppressWarnings("unchecked") private ReflectionDatumReader<T> getReflectionDatumReader() { if (datumReader == null) { datumReader = new ReflectionDatumReader<>(schema, (TypeToken<T>) TypeToken.of(this.typeRep.toType())); } return datumReader; }
TypeRepresentation typeRep = new TypeRepresentation(type); Schema schema = new ReflectionSchemaGenerator().generate(type);
public ObjectMappedTableDataset(String name, Table table, TypeRepresentation typeRep, Schema objectSchema, @Nullable ClassLoader classLoader) { super(name, table); this.table = table; this.objectSchema = objectSchema; this.typeRepresentation = typeRep; this.typeRepresentation.setClassLoader(classLoader); this.putWriter = new ReflectionPutWriter<>(objectSchema); }
@SuppressWarnings("unchecked") private ReflectionDatumReader<T> getReflectionDatumReader() { if (datumReader == null) { datumReader = new ReflectionDatumReader<>(schema, (TypeToken<T>) TypeToken.of(this.typeRep.toType())); } return datumReader; }
public ObjectMappedTableDataset(String name, Table table, TypeRepresentation typeRep, Schema objectSchema, @Nullable ClassLoader classLoader) { super(name, table); this.table = table; this.objectSchema = objectSchema; this.typeRepresentation = typeRep; this.typeRepresentation.setClassLoader(classLoader); this.putWriter = new ReflectionPutWriter<>(objectSchema); }
@SuppressWarnings("unchecked") private ReflectionRowReader<T> getReflectionRowReader() { if (rowReader == null) { try { // this can throw a runtime exception from a ClassNotFoundException Type type = typeRepresentation.toType(); rowReader = new ReflectionRowReader<>(objectSchema, (TypeToken<T>) TypeToken.of(type)); } catch (RuntimeException e) { String missingClass = isClassNotFoundException(e); if (missingClass != null) { LOG.error("Cannot load dataset because class {} could not be found. This is probably because the " + "type parameter of the dataset is not present in the dataset's jar file. See the developer " + "guide for more information.", missingClass); } throw e; } } return rowReader; }
@SuppressWarnings("unchecked") private ReflectionRowReader<T> getReflectionRowReader() { if (rowReader == null) { try { // this can throw a runtime exception from a ClassNotFoundException Type type = typeRepresentation.toType(); rowReader = new ReflectionRowReader<>(objectSchema, (TypeToken<T>) TypeToken.of(type)); } catch (RuntimeException e) { String missingClass = isClassNotFoundException(e); if (missingClass != null) { LOG.error("Cannot load dataset because class {} could not be found. This is probably because the " + "type parameter of the dataset is not present in the dataset's jar file. See the developer " + "guide for more information.", missingClass); } throw e; } } return rowReader; }