Search in sources :

Example 26 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class TestBuildOutputRecordUdf method testSinglePair.

@Test
public void testSinglePair() {
    BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf();
    SamzaSqlRelRecord actualRecord = buildOutputRecordUdf.execute("key", "value");
    SamzaSqlRelRecord expectedRecord = new SamzaSqlRelRecord(Arrays.asList("key"), Arrays.asList("value"));
    Assert.assertEquals(actualRecord.getFieldNames(), expectedRecord.getFieldNames());
    Assert.assertEquals(actualRecord.getFieldValues(), expectedRecord.getFieldValues());
}
Also used : SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) Test(org.junit.Test)

Example 27 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class TestBuildOutputRecordUdf method testNestedRecord.

@Test
public void testNestedRecord() {
    BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf();
    SamzaSqlRelRecord nestedSamzaSqlRelRecord = new SamzaSqlRelRecord(Arrays.asList("k3"), Arrays.asList("v3"));
    SamzaSqlRelRecord actualRecord = buildOutputRecordUdf.execute("k1", "v1", "k2", nestedSamzaSqlRelRecord);
    SamzaSqlRelRecord expectedRecord = new SamzaSqlRelRecord(Arrays.asList("k1", "k2"), Arrays.asList("v1", nestedSamzaSqlRelRecord));
    Assert.assertEquals(actualRecord.getFieldNames(), expectedRecord.getFieldNames());
    Assert.assertEquals(actualRecord.getFieldValues(), expectedRecord.getFieldValues());
}
Also used : SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) Test(org.junit.Test)

Example 28 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class TestBuildOutputRecordUdf method testMultiPairs.

@Test
public void testMultiPairs() {
    BuildOutputRecordUdf buildOutputRecordUdf = new BuildOutputRecordUdf();
    SamzaSqlRelRecord actualRecord = buildOutputRecordUdf.execute("k1", "v1", "k2", "v2");
    SamzaSqlRelRecord expectedRecord = new SamzaSqlRelRecord(Arrays.asList("k1", "k2"), Arrays.asList("v1", "v2"));
    Assert.assertEquals(actualRecord.getFieldNames(), expectedRecord.getFieldNames());
    Assert.assertEquals(actualRecord.getFieldValues(), expectedRecord.getFieldValues());
}
Also used : SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) Test(org.junit.Test)

Example 29 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class JoinTranslator method getTable.

private Table getTable(JoinInputNode tableNode, TranslatorContext context) {
    SqlIOConfig sourceTableConfig = resolveSQlIOForTable(tableNode.getRelNode(), context.getExecutionContext().getSamzaSqlApplicationConfig().getInputSystemStreamConfigBySource());
    if (sourceTableConfig == null || !sourceTableConfig.getTableDescriptor().isPresent()) {
        String errMsg = "Failed to resolve table source in join operation: node=" + tableNode.getRelNode();
        log.error(errMsg);
        throw new SamzaException(errMsg);
    }
    Table<KV<SamzaSqlRelRecord, SamzaSqlRelMessage>> table = context.getStreamAppDescriptor().getTable(sourceTableConfig.getTableDescriptor().get());
    if (tableNode.isRemoteTable()) {
        return table;
    }
    // If local table, load the table.
    // Load the local table with the fields in the join condition as composite key and relational message as the value.
    // Send the messages from the input stream denoted as 'table' to the created table store.
    MessageStream<SamzaSqlRelMessage> relOutputStream = context.getMessageStream(tableNode.getRelNode().getId());
    SamzaSqlRelRecordSerdeFactory.SamzaSqlRelRecordSerde keySerde = (SamzaSqlRelRecordSerdeFactory.SamzaSqlRelRecordSerde) new SamzaSqlRelRecordSerdeFactory().getSerde(null, null);
    SamzaSqlRelMessageSerdeFactory.SamzaSqlRelMessageSerde valueSerde = (SamzaSqlRelMessageSerdeFactory.SamzaSqlRelMessageSerde) new SamzaSqlRelMessageSerdeFactory().getSerde(null, null);
    List<Integer> tableKeyIds = tableNode.getKeyIds();
    // Let's always repartition by the join fields as key before sending the key and value to the table.
    // We need to repartition the stream denoted as table to ensure that both the stream and table that are joined
    // have the same partitioning scheme with the same partition key and number. Please note that bootstrap semantic is
    // not propagated to the intermediate streams. Please refer SAMZA-1613 for more details on this. Subsequently, the
    // results are consistent only after the local table is caught up.
    relOutputStream.partitionBy(m -> createSamzaSqlCompositeKey(m, tableKeyIds), m -> m, KVSerde.of(keySerde, valueSerde), intermediateStreamPrefix + "table_" + logicalOpId).sendTo(table);
    return table;
}
Also used : SqlIOConfig(org.apache.samza.sql.interfaces.SqlIOConfig) TableScan(org.apache.calcite.rel.core.TableScan) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) LoggerFactory(org.slf4j.LoggerFactory) RelOptUtil(org.apache.calcite.plan.RelOptUtil) ArrayList(java.util.ArrayList) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) SamzaSqlRelRecordSerdeFactory(org.apache.samza.sql.serializers.SamzaSqlRelRecordSerdeFactory) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage) LinkedList(java.util.LinkedList) KV(org.apache.samza.operators.KV) MessageStream(org.apache.samza.operators.MessageStream) Table(org.apache.samza.table.Table) SqlKind(org.apache.calcite.sql.SqlKind) Logger(org.slf4j.Logger) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) StreamTableJoinFunction(org.apache.samza.operators.functions.StreamTableJoinFunction) RexLiteral(org.apache.calcite.rex.RexLiteral) SqlExplainLevel(org.apache.calcite.sql.SqlExplainLevel) SamzaSqlRelMessage.getSamzaSqlCompositeKeyFieldNames(org.apache.samza.sql.data.SamzaSqlRelMessage.getSamzaSqlCompositeKeyFieldNames) RelNode(org.apache.calcite.rel.RelNode) Collectors(java.util.stream.Collectors) SamzaSqlRelMessage.createSamzaSqlCompositeKey(org.apache.samza.sql.data.SamzaSqlRelMessage.createSamzaSqlCompositeKey) SamzaException(org.apache.samza.SamzaException) SqlIOConfig(org.apache.samza.sql.interfaces.SqlIOConfig) RexInputRef(org.apache.calcite.rex.RexInputRef) List(java.util.List) Validate(org.apache.commons.lang3.Validate) SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) JoinRelType(org.apache.calcite.rel.core.JoinRelType) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) KVSerde(org.apache.samza.serializers.KVSerde) RexShuttle(org.apache.calcite.rex.RexShuttle) SamzaSqlRelMessageSerdeFactory(org.apache.samza.sql.serializers.SamzaSqlRelMessageSerdeFactory) Collections(java.util.Collections) SqlExplainFormat(org.apache.calcite.sql.SqlExplainFormat) RexCall(org.apache.calcite.rex.RexCall) SamzaSqlRelRecordSerdeFactory(org.apache.samza.sql.serializers.SamzaSqlRelRecordSerdeFactory) KV(org.apache.samza.operators.KV) SamzaException(org.apache.samza.SamzaException) SamzaSqlRelMessageSerdeFactory(org.apache.samza.sql.serializers.SamzaSqlRelMessageSerdeFactory) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage)

Example 30 with SamzaSqlRelRecord

use of org.apache.samza.sql.SamzaSqlRelRecord in project samza by apache.

the class TestSamzaSqlRelMessage method testCompositeKeyCreation.

@Test
public void testCompositeKeyCreation() {
    List<String> keyPartNames = Arrays.asList("kfield1", "kfield2");
    SamzaSqlRelMessage message = new SamzaSqlRelMessage(names, values, new SamzaSqlRelMsgMetadata(0L, 0L));
    SamzaSqlRelRecord relRecord1 = SamzaSqlRelMessage.createSamzaSqlCompositeKey(message, Collections.singletonList(0));
    Assert.assertEquals(relRecord1.getFieldNames().size(), 1);
    Assert.assertEquals(relRecord1.getFieldNames().get(0), "field1");
    Assert.assertEquals(relRecord1.getFieldValues().get(0), "value1");
    SamzaSqlRelRecord relRecord2 = SamzaSqlRelMessage.createSamzaSqlCompositeKey(message, Arrays.asList(1, 0), SamzaSqlRelMessage.getSamzaSqlCompositeKeyFieldNames(keyPartNames, Arrays.asList(1, 0)));
    Assert.assertEquals(relRecord2.getFieldNames().size(), 2);
    Assert.assertEquals(relRecord2.getFieldNames().get(0), "kfield2");
    Assert.assertEquals(relRecord2.getFieldValues().get(0), "value2");
    Assert.assertEquals(relRecord2.getFieldNames().get(1), "kfield1");
    Assert.assertEquals(relRecord2.getFieldValues().get(1), "value1");
}
Also used : SamzaSqlRelRecord(org.apache.samza.sql.SamzaSqlRelRecord) Test(org.junit.Test)

Aggregations

SamzaSqlRelRecord (org.apache.samza.sql.SamzaSqlRelRecord)31 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)6 SamzaSqlRelMessage (org.apache.samza.sql.data.SamzaSqlRelMessage)5 List (java.util.List)3 Schema (org.apache.avro.Schema)3 JoinRelType (org.apache.calcite.rel.core.JoinRelType)3 SamzaSqlRelMsgMetadata (org.apache.samza.sql.data.SamzaSqlRelMsgMetadata)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GenericData (org.apache.avro.generic.GenericData)2 ByteString (org.apache.calcite.avatica.util.ByteString)2 SamzaException (org.apache.samza.SamzaException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 Collectors (java.util.stream.Collectors)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 IndexedRecord (org.apache.avro.generic.IndexedRecord)1