Search in sources :

Example 1 with SamzaRelTableKeyConverter

use of org.apache.samza.sql.interfaces.SamzaRelTableKeyConverter in project samza by apache.

the class TestSamzaSqlRemoteTableJoinFunction method testWithInnerJoinWithTableOnRight.

@Test
public void testWithInnerJoinWithTableOnRight() {
    Map<String, String> props = new HashMap<>();
    SystemStream ss = new SystemStream("test", "nestedRecord");
    props.put(String.format(ConfigBasedAvroRelSchemaProviderFactory.CFG_SOURCE_SCHEMA, ss.getSystem(), ss.getStream()), SimpleRecord.SCHEMA$.toString());
    ConfigBasedAvroRelSchemaProviderFactory factory = new ConfigBasedAvroRelSchemaProviderFactory();
    AvroRelSchemaProvider schemaProvider = (AvroRelSchemaProvider) factory.create(ss, new MapConfig(props));
    AvroRelConverter relConverter = new AvroRelConverter(ss, schemaProvider, new MapConfig());
    SamzaRelTableKeyConverter relTableKeyConverter = new SampleRelTableKeyConverter();
    String remoteTableName = "testDb.testTable.$table";
    GenericData.Record tableRecord = new GenericData.Record(SimpleRecord.SCHEMA$);
    tableRecord.put("id", 1);
    tableRecord.put("name", "name1");
    SamzaSqlRelMessage streamMsg = new SamzaSqlRelMessage(streamFieldNames, streamFieldValues, new SamzaSqlRelMsgMetadata(0L, 0L));
    SamzaSqlRelMessage tableMsg = relConverter.convertToRelMessage(new KV(tableRecord.get("id"), tableRecord));
    JoinRelType joinRelType = JoinRelType.INNER;
    List<Integer> streamKeyIds = Arrays.asList(1);
    List<Integer> tableKeyIds = Arrays.asList(0);
    KV<Object, GenericRecord> record = KV.of(tableRecord.get("id"), tableRecord);
    JoinInputNode mockTableInputNode = mock(JoinInputNode.class);
    when(mockTableInputNode.getKeyIds()).thenReturn(tableKeyIds);
    when(mockTableInputNode.isPosOnRight()).thenReturn(true);
    when(mockTableInputNode.getFieldNames()).thenReturn(tableMsg.getSamzaSqlRelRecord().getFieldNames());
    when(mockTableInputNode.getSourceName()).thenReturn(remoteTableName);
    JoinInputNode mockStreamInputNode = mock(JoinInputNode.class);
    when(mockStreamInputNode.getKeyIds()).thenReturn(streamKeyIds);
    when(mockStreamInputNode.isPosOnRight()).thenReturn(false);
    when(mockStreamInputNode.getFieldNames()).thenReturn(streamFieldNames);
    SamzaSqlRemoteTableJoinFunction joinFn = new SamzaSqlRemoteTableJoinFunction(relConverter, relTableKeyConverter, mockStreamInputNode, mockTableInputNode, joinRelType, 0);
    SamzaSqlRelMessage outMsg = joinFn.apply(streamMsg, record);
    Assert.assertEquals(outMsg.getSamzaSqlRelRecord().getFieldValues().size(), outMsg.getSamzaSqlRelRecord().getFieldNames().size());
    List<String> expectedFieldNames = new ArrayList<>(streamFieldNames);
    expectedFieldNames.addAll(tableMsg.getSamzaSqlRelRecord().getFieldNames());
    List<Object> expectedFieldValues = new ArrayList<>(streamFieldValues);
    expectedFieldValues.addAll(tableMsg.getSamzaSqlRelRecord().getFieldValues());
    Assert.assertEquals(expectedFieldNames, outMsg.getSamzaSqlRelRecord().getFieldNames());
    Assert.assertEquals(expectedFieldValues, outMsg.getSamzaSqlRelRecord().getFieldValues());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AvroRelConverter(org.apache.samza.sql.avro.AvroRelConverter) SampleRelTableKeyConverter(org.apache.samza.sql.util.SampleRelTableKeyConverter) GenericRecord(org.apache.avro.generic.GenericRecord) SimpleRecord(org.apache.samza.sql.avro.schemas.SimpleRecord) MapConfig(org.apache.samza.config.MapConfig) GenericRecord(org.apache.avro.generic.GenericRecord) SamzaRelTableKeyConverter(org.apache.samza.sql.interfaces.SamzaRelTableKeyConverter) SamzaSqlRelMsgMetadata(org.apache.samza.sql.data.SamzaSqlRelMsgMetadata) SystemStream(org.apache.samza.system.SystemStream) KV(org.apache.samza.operators.KV) GenericData(org.apache.avro.generic.GenericData) JoinRelType(org.apache.calcite.rel.core.JoinRelType) AvroRelSchemaProvider(org.apache.samza.sql.avro.AvroRelSchemaProvider) ConfigBasedAvroRelSchemaProviderFactory(org.apache.samza.sql.avro.ConfigBasedAvroRelSchemaProviderFactory) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GenericData (org.apache.avro.generic.GenericData)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 JoinRelType (org.apache.calcite.rel.core.JoinRelType)1 MapConfig (org.apache.samza.config.MapConfig)1 KV (org.apache.samza.operators.KV)1 AvroRelConverter (org.apache.samza.sql.avro.AvroRelConverter)1 AvroRelSchemaProvider (org.apache.samza.sql.avro.AvroRelSchemaProvider)1 ConfigBasedAvroRelSchemaProviderFactory (org.apache.samza.sql.avro.ConfigBasedAvroRelSchemaProviderFactory)1 SimpleRecord (org.apache.samza.sql.avro.schemas.SimpleRecord)1 SamzaSqlRelMessage (org.apache.samza.sql.data.SamzaSqlRelMessage)1 SamzaSqlRelMsgMetadata (org.apache.samza.sql.data.SamzaSqlRelMsgMetadata)1 SamzaRelTableKeyConverter (org.apache.samza.sql.interfaces.SamzaRelTableKeyConverter)1 SampleRelTableKeyConverter (org.apache.samza.sql.util.SampleRelTableKeyConverter)1 SystemStream (org.apache.samza.system.SystemStream)1 Test (org.junit.Test)1