Search in sources :

Example 1 with LookupTableConfig

use of io.cdap.cdap.etl.api.LookupTableConfig in project hydrator-plugins by cdapio.

the class ScriptLookupProvider method provide.

@SuppressWarnings("unchecked")
public ScriptLookup provide(String table, JavaTypeConverters converters) {
    if (config == null) {
        throw new RuntimeException("Missing lookup configuration");
    }
    LookupTableConfig tableConfig = config.getTable(table);
    if (tableConfig == null || tableConfig.getType() != LookupTableConfig.TableType.DATASET) {
        throw new RuntimeException(String.format("Dataset %s not declared in configuration", table));
    }
    DatasetProperties arguments = DatasetProperties.builder().addAll(tableConfig.getDatasetProperties()).build();
    return new ScriptLookup(delegate.provide(table, arguments.getProperties()), tableConfig, converters);
}
Also used : LookupTableConfig(io.cdap.cdap.etl.api.LookupTableConfig) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties)

Example 2 with LookupTableConfig

use of io.cdap.cdap.etl.api.LookupTableConfig in project hydrator-plugins by cdapio.

the class ValueMapper method createLookupTableData.

/**
 * retrieve lookup table from table name
 */
private void createLookupTableData(TransformContext context) {
    for (String key : mappingValues.keySet()) {
        ValueMapping mapping = mappingValues.get(key);
        String lookupTableName = mapping.getLookupTableName();
        if (!lookupTableCache.containsKey(lookupTableName)) {
            LookupTableConfig tableConfig = new LookupTableConfig(LookupTableConfig.TableType.DATASET);
            Lookup<String> lookupTable = context.provide(lookupTableName, tableConfig.getDatasetProperties());
            lookupTableCache.put(lookupTableName, lookupTable);
        }
    }
}
Also used : LookupTableConfig(io.cdap.cdap.etl.api.LookupTableConfig)

Example 3 with LookupTableConfig

use of io.cdap.cdap.etl.api.LookupTableConfig in project hydrator-plugins by cdapio.

the class JavaScriptTransformTest method testLookup.

@Test
public void testLookup() throws Exception {
    JavaScriptTransform.Config config = new JavaScriptTransform.Config("function transform(x, emitter, ctx) { " + "var single = ctx.getLookup('purchases').lookup('abc');" + "var batch = ctx.getLookup('purchases').lookup(['abc', 'sdf']);" + "x.stringField = '1_' + single + ' 2_' + batch['abc'] + batch['sdf'] + '::' + batch.abc;" + "emitter.emit(x);" + "}", null, new LookupConfig(ImmutableMap.of("purchases", new LookupTableConfig(LookupTableConfig.TableType.DATASET))));
    Transform<StructuredRecord, StructuredRecord> transform = new JavaScriptTransform(config);
    transform.initialize(new MockTransformContext("somestage", new HashMap<String, String>(), new MockLookupProvider(TEST_LOOKUP)));
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(STRING_RECORD, emitter);
    StructuredRecord output = emitter.getEmitted().get(0);
    // check record1
    Assert.assertEquals(STRING_SCHEMA, output.getSchema());
    Assert.assertEquals("1_abc 2_abcsdf::abc", output.get("stringField"));
}
Also used : LookupTableConfig(io.cdap.cdap.etl.api.LookupTableConfig) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) HashMap(java.util.HashMap) LookupConfig(io.cdap.cdap.etl.api.LookupConfig) LookupTableConfig(io.cdap.cdap.etl.api.LookupTableConfig) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) LookupConfig(io.cdap.cdap.etl.api.LookupConfig) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) MockLookupProvider(io.cdap.cdap.etl.mock.common.MockLookupProvider) Test(org.junit.Test)

Aggregations

LookupTableConfig (io.cdap.cdap.etl.api.LookupTableConfig)3 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)1 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)1 LookupConfig (io.cdap.cdap.etl.api.LookupConfig)1 MockEmitter (io.cdap.cdap.etl.mock.common.MockEmitter)1 MockLookupProvider (io.cdap.cdap.etl.mock.common.MockLookupProvider)1 MockTransformContext (io.cdap.cdap.etl.mock.transform.MockTransformContext)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1