@Override public void apply() throws Exception { records.delete("123"); } });
@ReadOnly @Override public T read(String key) { return read(Bytes.toBytes(key)); }
@ReadOnly @Override public CloseableIterator<KeyValue<byte[], T>> scan(@Nullable String startRow, @Nullable String stopRow) { return scan(startRow == null ? null : Bytes.toBytes(startRow), stopRow == null ? null : Bytes.toBytes(stopRow)); }
@Override public void apply() throws Exception { records.write("123", record); } });
@Override public void apply() throws Exception { List<Split> splits = records.getSplits(1, rowkey, null); List<Record> recordsRead = new ArrayList<>(); for (Split split : splits) { SplitReader<byte[], Record> splitReader = records.createSplitReader(split); try { splitReader.initialize(split); while (splitReader.nextKeyValue()) { recordsRead.add(splitReader.getCurrentValue()); } } finally { splitReader.close(); } } Assert.assertEquals(1, recordsRead.size()); Assert.assertEquals(record, recordsRead.get(0)); } });
private String isClassNotFoundException(Throwable e) { if (e instanceof ClassNotFoundException) { return e.getMessage(); } if (e.getCause() != null) { return isClassNotFoundException(e.getCause()); } return null; }
private T readRow(Row row) { try { if (row.isEmpty()) { return null; } return getReflectionRowReader().read(row, objectSchema); } catch (Exception e) { // should not happen. Can happen if somebody changes the type in an incompatible way? throw new DataSetException("Failed to decode object: " + e.getMessage(), e); } } }
@ReadOnly @Override public T read(byte[] key) { return readRow(table.get(key)); }
@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); // if the table spec did not have schema, this is an ObjectMappedTable from CDAP 2.8. // add the schema and row key as arguments so that explore will work // 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); }
@Override public void apply() throws Exception { records.write("123", record2); } });
@Override public void apply() throws Exception { List<Split> splits = records.getSplits(1, null, rowkey); List<Record> recordsRead = new ArrayList<>(); for (Split split : splits) { SplitReader<byte[], Record> splitReader = records.createSplitReader(split); try { splitReader.initialize(split); while (splitReader.nextKeyValue()) { recordsRead.add(splitReader.getCurrentValue()); } } finally { splitReader.close(); } } Assert.assertEquals(0, recordsRead.size()); } });
private String isClassNotFoundException(Throwable e) { if (e instanceof ClassNotFoundException) { return e.getMessage(); } if (e.getCause() != null) { return isClassNotFoundException(e.getCause()); } return null; }
private T readRow(Row row) { try { if (row.isEmpty()) { return null; } return getReflectionRowReader().read(row, objectSchema); } catch (Exception e) { // should not happen. Can happen if somebody changes the type in an incompatible way? throw new DataSetException("Failed to decode object: " + e.getMessage(), e); } } }
@ReadOnly @Override public T read(byte[] key) { return readRow(table.get(key)); }
@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); // if the table spec did not have schema, this is an ObjectMappedTable from CDAP 2.8. // add the schema and row key as arguments so that explore will work // 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); }
@Override public void apply() throws Exception { records.write(rowkey, record); } });
@ReadOnly @Override public T read(String key) { return read(Bytes.toBytes(key)); }
@ReadOnly @Override public CloseableIterator<KeyValue<byte[], T>> scan(@Nullable String startRow, @Nullable String stopRow) { return scan(startRow == null ? null : Bytes.toBytes(startRow), stopRow == null ? null : Bytes.toBytes(stopRow)); }
@WriteOnly @Override public void delete(String key) { delete(Bytes.toBytes(key)); }
@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; }