Search in sources :

Example 66 with GenericRowData

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

the class HBaseSerde method convertToRow.

private RowData convertToRow(Result result, GenericRowData resultRow, GenericRowData[] familyRows) {
    for (int i = 0; i < fieldLength; i++) {
        if (rowkeyIndex == i) {
            assert keyDecoder != null;
            Object rowkey = keyDecoder.decode(result.getRow());
            resultRow.setField(rowkeyIndex, rowkey);
        } else {
            int f = (rowkeyIndex != -1 && i > rowkeyIndex) ? i - 1 : i;
            // get family key
            byte[] familyKey = families[f];
            GenericRowData familyRow = familyRows[f];
            for (int q = 0; q < this.qualifiers[f].length; q++) {
                // get quantifier key
                byte[] qualifier = qualifiers[f][q];
                // read value
                byte[] value = result.getValue(familyKey, qualifier);
                familyRow.setField(q, qualifierDecoders[f][q].decode(value));
            }
            resultRow.setField(i, familyRow);
        }
    }
    return resultRow;
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData)

Example 67 with GenericRowData

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

the class ElasticsearchDynamicSinkBaseITCase method testWritingDocuments.

@Test
public void testWritingDocuments() throws Exception {
    ResolvedSchema schema = new ResolvedSchema(Arrays.asList(Column.physical("a", DataTypes.BIGINT().notNull()), Column.physical("b", DataTypes.TIME()), Column.physical("c", DataTypes.STRING().notNull()), Column.physical("d", DataTypes.FLOAT()), Column.physical("e", DataTypes.TINYINT().notNull()), Column.physical("f", DataTypes.DATE()), Column.physical("g", DataTypes.TIMESTAMP().notNull())), Collections.emptyList(), UniqueConstraint.primaryKey("name", Arrays.asList("a", "g")));
    GenericRowData rowData = GenericRowData.of(1L, 12345, StringData.fromString("ABCDE"), 12.12f, (byte) 2, 12345, TimestampData.fromLocalDateTime(LocalDateTime.parse("2012-12-12T12:12:12")));
    String index = "writing-documents";
    ElasticsearchDynamicSinkFactoryBase sinkFactory = getDynamicSinkFactory();
    DynamicTableSink.SinkRuntimeProvider runtimeProvider = sinkFactory.createDynamicTableSink(getPrefilledTestContext(index).withSchema(schema).build()).getSinkRuntimeProvider(new ElasticsearchUtil.MockContext());
    final SinkV2Provider sinkProvider = (SinkV2Provider) runtimeProvider;
    final Sink<RowData> sink = sinkProvider.createSink();
    StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
    environment.setParallelism(4);
    rowData.setRowKind(RowKind.UPDATE_AFTER);
    environment.<RowData>fromElements(rowData).sinkTo(sink);
    environment.execute();
    RestHighLevelClient client = getClient();
    Map<String, Object> response = makeGetRequest(client, index, "1_2012-12-12T12:12:12");
    Map<Object, Object> expectedMap = new HashMap<>();
    expectedMap.put("a", 1);
    expectedMap.put("b", "00:00:12");
    expectedMap.put("c", "ABCDE");
    expectedMap.put("d", 12.12d);
    expectedMap.put("e", 2);
    expectedMap.put("f", "2003-10-20");
    expectedMap.put("g", "2012-12-12 12:12:12");
    Assertions.assertEquals(response, expectedMap);
}
Also used : HashMap(java.util.HashMap) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) ElasticsearchUtil(org.apache.flink.connector.elasticsearch.ElasticsearchUtil) GenericRowData(org.apache.flink.table.data.GenericRowData) SinkV2Provider(org.apache.flink.table.connector.sink.SinkV2Provider) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) Test(org.junit.jupiter.api.Test)

Example 68 with GenericRowData

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

the class HBaseRowDataAsyncLookupFunction method fetchResult.

/**
 * Execute async fetch result .
 *
 * @param resultFuture The result or exception is returned.
 * @param currentRetry Current number of retries.
 * @param rowKey the lookup key.
 */
private void fetchResult(CompletableFuture<Collection<RowData>> resultFuture, int currentRetry, Object rowKey) {
    Get get = serde.createGet(rowKey);
    CompletableFuture<Result> responseFuture = table.get(get);
    responseFuture.whenCompleteAsync((result, throwable) -> {
        if (throwable != null) {
            if (throwable instanceof TableNotFoundException) {
                LOG.error("Table '{}' not found ", hTableName, throwable);
                resultFuture.completeExceptionally(new RuntimeException("HBase table '" + hTableName + "' not found.", throwable));
            } else {
                LOG.error(String.format("HBase asyncLookup error, retry times = %d", currentRetry), throwable);
                if (currentRetry >= maxRetryTimes) {
                    resultFuture.completeExceptionally(throwable);
                } else {
                    try {
                        Thread.sleep(1000 * currentRetry);
                    } catch (InterruptedException e1) {
                        resultFuture.completeExceptionally(e1);
                    }
                    fetchResult(resultFuture, currentRetry + 1, rowKey);
                }
            }
        } else {
            if (result.isEmpty()) {
                resultFuture.complete(Collections.emptyList());
                if (cache != null) {
                    cache.put(rowKey, new GenericRowData(0));
                }
            } else {
                if (cache != null) {
                    RowData rowData = serde.convertToNewRow(result);
                    resultFuture.complete(Collections.singletonList(rowData));
                    cache.put(rowKey, rowData);
                } else {
                    resultFuture.complete(Collections.singletonList(serde.convertToNewRow(result)));
                }
            }
        }
    });
}
Also used : TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) Get(org.apache.hadoop.hbase.client.Get) GenericRowData(org.apache.flink.table.data.GenericRowData) Result(org.apache.hadoop.hbase.client.Result)

Example 69 with GenericRowData

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

the class PartitionPathUtils method fillPartitionValueForRecord.

/**
 * Extract partition value from path and fill to record.
 *
 * @param fieldNames record field names.
 * @param fieldTypes record field types.
 * @param selectFields the selected fields.
 * @param partitionKeys the partition field names.
 * @param path the file path that the partition located.
 * @param defaultPartValue default value of partition field.
 * @return the filled record.
 */
public static GenericRowData fillPartitionValueForRecord(String[] fieldNames, DataType[] fieldTypes, int[] selectFields, List<String> partitionKeys, Path path, String defaultPartValue) {
    GenericRowData record = new GenericRowData(selectFields.length);
    LinkedHashMap<String, String> partSpec = PartitionPathUtils.extractPartitionSpecFromPath(path);
    for (int i = 0; i < selectFields.length; i++) {
        int selectField = selectFields[i];
        String name = fieldNames[selectField];
        if (partitionKeys.contains(name)) {
            String value = partSpec.get(name);
            value = defaultPartValue.equals(value) ? null : value;
            record.setField(i, PartitionPathUtils.convertStringToInternalValue(value, fieldTypes[selectField]));
        }
    }
    return record;
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData)

Example 70 with GenericRowData

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

the class FirstValueAggFunction method createAccumulator.

// --------------------------------------------------------------------------------------------
// Runtime
// --------------------------------------------------------------------------------------------
@Override
public RowData createAccumulator() {
    GenericRowData acc = new GenericRowData(2);
    acc.setField(0, null);
    acc.setField(1, Long.MAX_VALUE);
    return acc;
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData)

Aggregations

GenericRowData (org.apache.flink.table.data.GenericRowData)94 RowData (org.apache.flink.table.data.RowData)32 JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)16 Test (org.junit.Test)14 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)13 RowType (org.apache.flink.table.types.logical.RowType)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 IntType (org.apache.flink.table.types.logical.IntType)11 List (java.util.List)9 LogicalType (org.apache.flink.table.types.logical.LogicalType)9 GenericArrayData (org.apache.flink.table.data.GenericArrayData)6 StringData (org.apache.flink.table.data.StringData)6 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 OutputStream (java.io.OutputStream)4 PrintStream (java.io.PrintStream)4 Collections (java.util.Collections)4 Random (java.util.Random)4 Consumer (java.util.function.Consumer)4