use of io.cdap.cdap.internal.io.TypeRepresentation in project cdap by caskdata.
the class ObjectStoreDefinition method getDataset.
@Override
public ObjectStoreDataset<?> getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException {
DatasetSpecification kvTableSpec = spec.getSpecification("objects");
KeyValueTable table = tableDef.getDataset(datasetContext, kvTableSpec, arguments, classLoader);
TypeRepresentation typeRep = GSON.fromJson(spec.getProperty("type"), TypeRepresentation.class);
Schema schema = GSON.fromJson(spec.getProperty("schema"), Schema.class);
return new ObjectStoreDataset(spec.getName(), table, typeRep, schema, classLoader);
}
use of io.cdap.cdap.internal.io.TypeRepresentation in project cdap by caskdata.
the class RecordWithString method testEmptyValue.
// this tests that the datum reader treats empty fields correctly. It reproduces the issue in ENG-2404.
@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());
}
use of io.cdap.cdap.internal.io.TypeRepresentation in project cdap by caskdata.
the class ObjectStoreDatasetTest method testWithCustomClassLoader.
@Test
public void testWithCustomClassLoader() throws Exception {
DatasetId kv = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("kv");
// create a dummy class loader that records the name of the class it loaded
final AtomicReference<String> lastClassLoaded = new AtomicReference<>(null);
ClassLoader loader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
lastClassLoaded.set(name);
return super.loadClass(name);
}
};
dsFrameworkUtil.createInstance("keyValueTable", kv, DatasetProperties.EMPTY);
KeyValueTable kvTable = dsFrameworkUtil.getInstance(kv);
Type type = Custom.class;
TypeRepresentation typeRep = new TypeRepresentation(type);
Schema schema = new ReflectionSchemaGenerator().generate(type);
final ObjectStoreDataset<Custom> objectStore = new ObjectStoreDataset<>("kv", kvTable, typeRep, schema, loader);
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor(objectStore);
// need to call this to actually load the Custom class, because the Custom class is no longer used in the
// ObjectStoreDataset's constructor, but rather lazily when its actually needed.
objectStore.getRecordType();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
objectStore.write("dummy", new Custom(382, Lists.newArrayList("blah")));
}
});
// verify the class name was recorded (the dummy class loader was used).
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals(Custom.class.getName(), lastClassLoaded.get());
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
deleteAndVerify(objectStore, Bytes.toBytes("dummy"));
}
});
dsFrameworkUtil.deleteInstance(kv);
}
use of io.cdap.cdap.internal.io.TypeRepresentation in project cdap by caskdata.
the class ObjectStores method objectStoreProperties.
/**
* 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();
}
use of io.cdap.cdap.internal.io.TypeRepresentation in project cdap by caskdata.
the class ObjectMappedTableDefinition method getDataset.
@Override
public ObjectMappedTableDataset<?> getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException {
String keyName = ObjectMappedTableProperties.getRowKeyExploreName(spec.getProperties());
DatasetSpecification tableSpec = spec.getSpecification(TABLE_NAME);
// TODO: remove after CDAP-2122 is done
if (!tableSpec.getProperties().containsKey(Table.PROPERTY_SCHEMA)) {
tableSpec = DatasetSpecification.builder(tableSpec.getName(), tableSpec.getType()).properties(tableSpec.getProperties()).property(Table.PROPERTY_SCHEMA, spec.getProperty(Table.PROPERTY_SCHEMA)).property(Table.PROPERTY_SCHEMA_ROW_FIELD, keyName).datasets(tableSpec.getSpecifications().values()).build();
}
// reconstruct the table schema here because of backwards compatibility
DatasetDefinition<Table, DatasetAdmin> tableDef = getDelegate(TABLE_NAME);
Table table = tableDef.getDataset(datasetContext, tableSpec, arguments, classLoader);
Map<String, String> properties = spec.getProperties();
TypeRepresentation typeRep = GSON.fromJson(ObjectMappedTableProperties.getObjectTypeRepresentation(properties), TypeRepresentation.class);
Schema objSchema = ObjectMappedTableProperties.getObjectSchema(properties);
return new ObjectMappedTableDataset(spec.getName(), table, typeRep, objSchema, classLoader);
}
Aggregations