Search in sources :

Example 26 with JoinedRowData

use of org.apache.flink.table.data.utils.JoinedRowData in project flink by apache.

the class WindowRankProcessor method open.

@Override
public void open(Context<Long> context) throws Exception {
    this.ctx = context;
    // compile comparator
    sortKeyComparator = generatedSortKeyComparator.newInstance(ctx.getRuntimeContext().getUserCodeClassLoader());
    final LongSerializer namespaceSerializer = LongSerializer.INSTANCE;
    ListSerializer<RowData> listSerializer = new ListSerializer<>(inputSerializer);
    MapStateDescriptor<RowData, List<RowData>> mapStateDescriptor = new MapStateDescriptor<>("window_rank", sortKeySerializer, listSerializer);
    MapState<RowData, List<RowData>> state = ctx.getKeyedStateBackend().getOrCreateKeyedState(namespaceSerializer, mapStateDescriptor);
    this.windowTimerService = new WindowTimerServiceImpl(ctx.getTimerService(), shiftTimeZone);
    this.windowState = new WindowMapState<>((InternalMapState<RowData, Long, RowData, List<RowData>>) state);
    this.windowBuffer = bufferFactory.create(ctx.getOperatorOwner(), ctx.getMemoryManager(), ctx.getMemorySize(), ctx.getRuntimeContext(), windowTimerService, ctx.getKeyedStateBackend(), windowState, true, shiftTimeZone);
    this.reuseOutput = new JoinedRowData();
    this.reuseRankRow = new GenericRowData(1);
    this.currentProgress = Long.MIN_VALUE;
}
Also used : ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) LongSerializer(org.apache.flink.api.common.typeutils.base.LongSerializer) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) WindowTimerServiceImpl(org.apache.flink.table.runtime.operators.window.slicing.WindowTimerServiceImpl) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) InternalMapState(org.apache.flink.runtime.state.internal.InternalMapState) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) GenericRowData(org.apache.flink.table.data.GenericRowData) ArrayList(java.util.ArrayList) List(java.util.List)

Example 27 with JoinedRowData

use of org.apache.flink.table.data.utils.JoinedRowData in project flink by apache.

the class InputConversionOperator method processElement.

@Override
public void processElement(StreamRecord<E> element) throws Exception {
    final E externalRecord = element.getValue();
    final Object internalRecord;
    try {
        internalRecord = converter.toInternal(externalRecord);
    } catch (Exception e) {
        throw new FlinkRuntimeException(String.format("Error during input conversion from external DataStream API to " + "internal Table API data structures. Make sure that the " + "provided data types that configure the converters are " + "correctly declared in the schema. Affected record:\n%s", externalRecord), e);
    }
    final RowData payloadRowData;
    if (requiresWrapping) {
        final GenericRowData wrapped = new GenericRowData(RowKind.INSERT, 1);
        wrapped.setField(0, internalRecord);
        payloadRowData = wrapped;
    } else {
        // top-level records must not be null and will be skipped
        if (internalRecord == null) {
            return;
        }
        payloadRowData = (RowData) internalRecord;
    }
    final RowKind kind = payloadRowData.getRowKind();
    if (isInsertOnly && kind != RowKind.INSERT) {
        throw new FlinkRuntimeException(String.format("Error during input conversion. Conversion expects insert-only " + "records but DataStream API record contains: %s", kind));
    }
    if (!produceRowtimeMetadata) {
        output.collect(outRecord.replace(payloadRowData));
        return;
    }
    if (!element.hasTimestamp()) {
        throw new FlinkRuntimeException("Could not find timestamp in DataStream API record. " + "Make sure that timestamps have been assigned before and " + "the event-time characteristic is enabled.");
    }
    final GenericRowData rowtimeRowData = new GenericRowData(1);
    rowtimeRowData.setField(0, TimestampData.fromEpochMillis(element.getTimestamp()));
    final JoinedRowData joinedRowData = new JoinedRowData(kind, payloadRowData, rowtimeRowData);
    output.collect(outRecord.replace(joinedRowData));
}
Also used : RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) RowKind(org.apache.flink.types.RowKind) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) GenericRowData(org.apache.flink.table.data.GenericRowData) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException)

Example 28 with JoinedRowData

use of org.apache.flink.table.data.utils.JoinedRowData in project flink by apache.

the class AggregateWindowOperator method open.

@Override
public void open() throws Exception {
    super.open();
    reuseOutput = new JoinedRowData();
}
Also used : JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData)

Example 29 with JoinedRowData

use of org.apache.flink.table.data.utils.JoinedRowData in project flink by apache.

the class WindowTableFunctionOperator method open.

@Override
public void open() throws Exception {
    super.open();
    this.collector = new TimestampedCollector<>(output);
    collector.eraseTimestamp();
    outRow = new JoinedRowData();
    windowProperties = new GenericRowData(3);
}
Also used : JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) GenericRowData(org.apache.flink.table.data.GenericRowData)

Example 30 with JoinedRowData

use of org.apache.flink.table.data.utils.JoinedRowData in project flink by apache.

the class SumHashAggTestOperator method endInput.

public void endInput() throws Exception {
    StreamRecord<RowData> outElement = new StreamRecord<>(null);
    JoinedRowData hashAggOutput = new JoinedRowData();
    GenericRowData aggValueOutput = new GenericRowData(1);
    if (sorter == null) {
        // no spilling, output by iterating aggregate map.
        KeyValueIterator<BinaryRowData, BinaryRowData> iter = aggregateMap.getEntryIterator(false);
        while (iter.advanceNext()) {
            // set result and output
            aggValueOutput.setField(0, iter.getValue().isNullAt(0) ? null : iter.getValue().getLong(0));
            hashAggOutput.replace(iter.getKey(), aggValueOutput);
            getOutput().collect(outElement.replace(hashAggOutput));
        }
    } else {
        // spill last part of input' aggregation output buffer
        sorter.sortAndSpill(aggregateMap.getRecordAreaMemorySegments(), aggregateMap.getNumElements(), new BytesHashMapSpillMemorySegmentPool(aggregateMap.getBucketAreaMemorySegments()));
        // only release non-data memory in advance.
        aggregateMap.free(true);
        // fall back to sort based aggregation
        BinaryRowData lastKey = null;
        JoinedRowData fallbackInput = new JoinedRowData();
        boolean aggSumIsNull = false;
        long aggSum = -1;
        // free hash map memory, but not release back to memory manager
        MutableObjectIterator<Tuple2<BinaryRowData, BinaryRowData>> iterator = sorter.getKVIterator();
        Tuple2<BinaryRowData, BinaryRowData> kv;
        while ((kv = iterator.next()) != null) {
            BinaryRowData key = kv.f0;
            BinaryRowData value = kv.f1;
            // prepare input
            fallbackInput.replace(key, value);
            if (lastKey == null) {
                // found first key group
                lastKey = key.copy();
                aggSumIsNull = true;
                aggSum = -1L;
            } else if (key.getSizeInBytes() != lastKey.getSizeInBytes() || !(BinaryRowDataUtil.byteArrayEquals(key.getSegments()[0].getArray(), lastKey.getSegments()[0].getArray(), key.getSizeInBytes()))) {
                // output current group aggregate result
                aggValueOutput.setField(0, aggSumIsNull ? null : aggSum);
                hashAggOutput.replace(lastKey, aggValueOutput);
                getOutput().collect(outElement.replace(hashAggOutput));
                // found new group
                lastKey = key.copy();
                aggSumIsNull = true;
                aggSum = -1L;
            }
            if (!fallbackInput.isNullAt(1)) {
                long sumInput = fallbackInput.getLong(1);
                if (aggSumIsNull) {
                    aggSum = sumInput;
                } else {
                    aggSum = aggSum + sumInput;
                }
                aggSumIsNull = false;
            }
        }
        // output last key group aggregate result
        aggValueOutput.setField(0, aggSumIsNull ? null : aggSum);
        hashAggOutput.replace(lastKey, aggValueOutput);
        getOutput().collect(outElement.replace(hashAggOutput));
    }
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) Tuple2(org.apache.flink.api.java.tuple.Tuple2) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) GenericRowData(org.apache.flink.table.data.GenericRowData)

Aggregations

JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)47 RowData (org.apache.flink.table.data.RowData)22 GenericRowData (org.apache.flink.table.data.GenericRowData)17 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)12 PerKeyStateDataViewStore (org.apache.flink.table.runtime.dataview.PerKeyStateDataViewStore)12 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)11 ArrayList (java.util.ArrayList)7 List (java.util.List)7 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)7 ListTypeInfo (org.apache.flink.api.java.typeutils.ListTypeInfo)5 StreamOperator (org.apache.flink.streaming.api.operators.StreamOperator)5 Test (org.junit.Test)5 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 StateTtlConfig (org.apache.flink.api.common.state.StateTtlConfig)4 Configuration (org.apache.flink.configuration.Configuration)4 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)3 TwoInputStreamTask (org.apache.flink.streaming.runtime.tasks.TwoInputStreamTask)3 TwoInputStreamTaskTestHarness (org.apache.flink.streaming.runtime.tasks.TwoInputStreamTaskTestHarness)3 StreamRecordRowDataWrappingCollector (org.apache.flink.table.runtime.operators.python.utils.StreamRecordRowDataWrappingCollector)3 RowDataSerializer (org.apache.flink.table.runtime.typeutils.RowDataSerializer)3