Search in sources :

Example 96 with RowData

use of org.apache.flink.table.data.RowData in project flink by apache.

the class BatchArrowPythonOverWindowAggregateFunctionOperatorTest method testFinishBundleTriggeredByCount.

@Test
public void testFinishBundleTriggeredByCount() throws Exception {
    Configuration conf = new Configuration();
    conf.setInteger(PythonOptions.MAX_BUNDLE_SIZE, 3);
    OneInputStreamOperatorTestHarness<RowData, RowData> testHarness = getTestHarness(conf);
    long initialTime = 0L;
    ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();
    testHarness.open();
    testHarness.processElement(new StreamRecord<>(newBinaryRow(true, "c1", "c2", 0L, 0L), initialTime + 1));
    testHarness.processElement(new StreamRecord<>(newBinaryRow(true, "c1", "c4", 1L, 0L), initialTime + 2));
    testHarness.processElement(new StreamRecord<>(newBinaryRow(true, "c1", "c6", 2L, 10L), initialTime + 3));
    assertOutputEquals("FinishBundle should not be triggered.", expectedOutput, testHarness.getOutput());
    testHarness.processElement(new StreamRecord<>(newBinaryRow(true, "c2", "c8", 3L, 0L), initialTime + 3));
    expectedOutput.add(new StreamRecord<>(newRow(true, "c1", "c2", 0L, 0L, 0L)));
    expectedOutput.add(new StreamRecord<>(newRow(true, "c1", "c4", 1L, 0L, 0L)));
    expectedOutput.add(new StreamRecord<>(newRow(true, "c1", "c6", 2L, 10L, 2L)));
    assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());
    testHarness.close();
    expectedOutput.add(new StreamRecord<>(newRow(true, "c2", "c8", 3L, 0L, 3L)));
    assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());
}
Also used : RowData(org.apache.flink.table.data.RowData) Configuration(org.apache.flink.configuration.Configuration) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 97 with RowData

use of org.apache.flink.table.data.RowData in project flink by apache.

the class PythonTableFunctionOperatorTest method newRow.

@Override
public RowData newRow(boolean accumulateMsg, Object... fields) {
    if (accumulateMsg) {
        return row(fields);
    } else {
        RowData row = row(fields);
        row.setRowKind(RowKind.DELETE);
        return row;
    }
}
Also used : RowData(org.apache.flink.table.data.RowData)

Example 98 with RowData

use of org.apache.flink.table.data.RowData in project flink by apache.

the class JdbcOutputFormatBuilder method createBufferReduceExecutor.

private static JdbcBatchStatementExecutor<RowData> createBufferReduceExecutor(JdbcDmlOptions opt, RuntimeContext ctx, TypeInformation<RowData> rowDataTypeInfo, LogicalType[] fieldTypes) {
    checkArgument(opt.getKeyFields().isPresent());
    JdbcDialect dialect = opt.getDialect();
    String tableName = opt.getTableName();
    String[] pkNames = opt.getKeyFields().get();
    int[] pkFields = Arrays.stream(pkNames).mapToInt(Arrays.asList(opt.getFieldNames())::indexOf).toArray();
    LogicalType[] pkTypes = Arrays.stream(pkFields).mapToObj(f -> fieldTypes[f]).toArray(LogicalType[]::new);
    final TypeSerializer<RowData> typeSerializer = rowDataTypeInfo.createSerializer(ctx.getExecutionConfig());
    final Function<RowData, RowData> valueTransform = ctx.getExecutionConfig().isObjectReuseEnabled() ? typeSerializer::copy : Function.identity();
    return new TableBufferReducedStatementExecutor(createUpsertRowExecutor(dialect, tableName, opt.getFieldNames(), fieldTypes, pkFields, pkNames, pkTypes), createDeleteExecutor(dialect, tableName, pkNames, pkTypes), createRowKeyExtractor(fieldTypes, pkFields), valueTransform);
}
Also used : DataType(org.apache.flink.table.types.DataType) Arrays(java.util.Arrays) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) JdbcExecutionOptions(org.apache.flink.connector.jdbc.JdbcExecutionOptions) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) JdbcRowConverter(org.apache.flink.connector.jdbc.converter.JdbcRowConverter) RowType(org.apache.flink.table.types.logical.RowType) Function(java.util.function.Function) FieldNamedPreparedStatement(org.apache.flink.connector.jdbc.statement.FieldNamedPreparedStatement) RowData.createFieldGetter(org.apache.flink.table.data.RowData.createFieldGetter) TableSimpleStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableSimpleStatementExecutor) GenericRowData(org.apache.flink.table.data.GenericRowData) JdbcBatchStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.JdbcBatchStatementExecutor) TableBufferReducedStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableBufferReducedStatementExecutor) TableBufferedStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableBufferedStatementExecutor) SimpleJdbcConnectionProvider(org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) RowData(org.apache.flink.table.data.RowData) JdbcOutputFormat(org.apache.flink.connector.jdbc.internal.JdbcOutputFormat) JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) Serializable(java.io.Serializable) LogicalType(org.apache.flink.table.types.logical.LogicalType) Preconditions.checkArgument(org.apache.flink.util.Preconditions.checkArgument) TableInsertOrUpdateStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableInsertOrUpdateStatementExecutor) JdbcDialect(org.apache.flink.connector.jdbc.dialect.JdbcDialect) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) LogicalType(org.apache.flink.table.types.logical.LogicalType) JdbcDialect(org.apache.flink.connector.jdbc.dialect.JdbcDialect) TableBufferReducedStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableBufferReducedStatementExecutor)

Example 99 with RowData

use of org.apache.flink.table.data.RowData in project flink by apache.

the class JdbcRowDataLookupFunction method eval.

/**
 * This is a lookup method which is called by Flink framework in runtime.
 *
 * @param keys lookup keys
 */
public void eval(Object... keys) {
    RowData keyRow = GenericRowData.of(keys);
    if (cache != null) {
        List<RowData> cachedRows = cache.getIfPresent(keyRow);
        if (cachedRows != null) {
            for (RowData cachedRow : cachedRows) {
                collect(cachedRow);
            }
            return;
        }
    }
    for (int retry = 0; retry <= maxRetryTimes; retry++) {
        try {
            statement.clearParameters();
            statement = lookupKeyRowConverter.toExternal(keyRow, statement);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (cache == null) {
                    while (resultSet.next()) {
                        collect(jdbcRowConverter.toInternal(resultSet));
                    }
                } else {
                    ArrayList<RowData> rows = new ArrayList<>();
                    while (resultSet.next()) {
                        RowData row = jdbcRowConverter.toInternal(resultSet);
                        rows.add(row);
                        collect(row);
                    }
                    rows.trimToSize();
                    if (!rows.isEmpty() || cacheMissingKey) {
                        cache.put(keyRow, rows);
                    }
                }
            }
            break;
        } catch (SQLException e) {
            LOG.error(String.format("JDBC executeBatch error, retry times = %d", retry), e);
            if (retry >= maxRetryTimes) {
                throw new RuntimeException("Execution of JDBC statement failed.", e);
            }
            try {
                if (!connectionProvider.isConnectionValid()) {
                    statement.close();
                    connectionProvider.closeConnection();
                    establishConnectionAndStatement();
                }
            } catch (SQLException | ClassNotFoundException exception) {
                LOG.error("JDBC connection is not valid, and reestablish connection failed", exception);
                throw new RuntimeException("Reestablish JDBC connection failed", exception);
            }
            try {
                Thread.sleep(1000 * retry);
            } catch (InterruptedException e1) {
                throw new RuntimeException(e1);
            }
        }
    }
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList)

Example 100 with RowData

use of org.apache.flink.table.data.RowData in project flink by apache.

the class JdbcRowDataInputFormat method nextRecord.

/**
 * Stores the next resultSet row in a tuple.
 *
 * @param reuse row to be reused.
 * @return row containing next {@link RowData}
 * @throws IOException
 */
@Override
public RowData nextRecord(RowData reuse) throws IOException {
    try {
        if (!hasNext) {
            return null;
        }
        RowData row = rowConverter.toInternal(resultSet);
        // update hasNext after we've read the record
        hasNext = resultSet.next();
        return row;
    } catch (SQLException se) {
        throw new IOException("Couldn't read data - " + se.getMessage(), se);
    } catch (NullPointerException npe) {
        throw new IOException("Couldn't access resultSet", npe);
    }
}
Also used : RowData(org.apache.flink.table.data.RowData) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

RowData (org.apache.flink.table.data.RowData)602 Test (org.junit.Test)201 GenericRowData (org.apache.flink.table.data.GenericRowData)178 ArrayList (java.util.ArrayList)109 RowType (org.apache.flink.table.types.logical.RowType)105 JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)90 Watermark (org.apache.flink.streaming.api.watermark.Watermark)84 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)72 Transformation (org.apache.flink.api.dag.Transformation)70 Configuration (org.apache.flink.configuration.Configuration)68 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)67 List (java.util.List)65 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)54 DataType (org.apache.flink.table.types.DataType)52 Map (java.util.Map)42 LogicalType (org.apache.flink.table.types.logical.LogicalType)41 TableException (org.apache.flink.table.api.TableException)34 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)33 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)32 OperatorSubtaskState (org.apache.flink.runtime.checkpoint.OperatorSubtaskState)31