Java Code Examples for org.apache.flink.table.types.logical.RowType#getFieldNames()

The following examples show how to use org.apache.flink.table.types.logical.RowType#getFieldNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JacksonRecordParser.java    From pulsar-flink with Apache License 2.0 5 votes vote down vote up
private Row convertObject(JsonParser parser, FieldsDataType fdt, List<Function<JsonParser, Object>> fieldConverters, Row row) throws IOException {

        RowType rowType = (RowType) fdt.getLogicalType();
        List<String> fieldNames = rowType.getFieldNames();
        while (nextUntil(parser, JsonToken.END_OBJECT)) {
            int index = fieldNames.indexOf(parser.getCurrentName());
            if (index == -1) {
                parser.skipChildren();
            } else {
                row.setField(index, fieldConverters.get(index).apply(parser));
            }
        }
        return row;
    }
 
Example 2
Source File: HiveTypeUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TypeInfo visit(RowType rowType) {
	List<String> names = rowType.getFieldNames();
	List<TypeInfo> typeInfos = new ArrayList<>(names.size());
	for (String name : names) {
		TypeInfo typeInfo =
				rowType.getTypeAt(rowType.getFieldIndex(name)).accept(new TypeInfoLogicalTypeVisitor(dataType));
		if (null != typeInfo) {
			typeInfos.add(typeInfo);
		} else {
			return defaultMethod(rowType);
		}
	}
	return TypeInfoFactory.getStructTypeInfo(names, typeInfos);
}
 
Example 3
Source File: KuduTableSource.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@Override
public TableSource<Row> projectFields(int[] ints) {
    String[] fieldNames = new String[ints.length];
    RowType producedDataType = (RowType) getProducedDataType().getLogicalType();
    List<String> prevFieldNames = producedDataType.getFieldNames();
    for (int i = 0; i < ints.length; i++) {
        fieldNames[i] = prevFieldNames.get(ints[i]);
    }
    return new KuduTableSource(configBuilder, tableInfo, flinkSchema, fieldNames);
}
 
Example 4
Source File: HiveTypeUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TypeInfo visit(RowType rowType) {
	List<String> names = rowType.getFieldNames();
	List<TypeInfo> typeInfos = new ArrayList<>(names.size());
	for (String name : names) {
		TypeInfo typeInfo =
				rowType.getTypeAt(rowType.getFieldIndex(name)).accept(this);
		if (null != typeInfo) {
			typeInfos.add(typeInfo);
		} else {
			return defaultMethod(rowType);
		}
	}
	return TypeInfoFactory.getStructTypeInfo(names, typeInfos);
}
 
Example 5
Source File: TableSchemaUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the field indices of primary key in the physical columns of
 * this schema (not include computed columns).
 */
public static int[] getPrimaryKeyIndices(TableSchema schema) {
	if (schema.getPrimaryKey().isPresent()) {
		RowType physicalRowType = (RowType) schema.toPhysicalRowDataType().getLogicalType();
		List<String> fieldNames = physicalRowType.getFieldNames();
		return schema.getPrimaryKey().get().getColumns().stream()
			.mapToInt(fieldNames::indexOf)
			.toArray();
	} else {
		return new int[0];
	}
}
 
Example 6
Source File: SchemaUtils.java    From pulsar-flink with Apache License 2.0 4 votes vote down vote up
private static Schema sqlType2AvroSchema(DataType flinkType, boolean nullable, String recordName, String namespace) throws IncompatibleSchemaException {
    SchemaBuilder.TypeBuilder<Schema> builder = SchemaBuilder.builder();
    LogicalTypeRoot type = flinkType.getLogicalType().getTypeRoot();
    Schema schema = null;

    if (flinkType instanceof AtomicDataType) {
        switch (type) {
            case BOOLEAN:
                schema = builder.booleanType();
                break;
            case TINYINT:
            case SMALLINT:
            case INTEGER:
                schema = builder.intType();
                break;
            case BIGINT:
                schema = builder.longType();
                break;
            case DATE:
                schema = LogicalTypes.date().addToSchema(builder.intType());
                break;
            case TIMESTAMP_WITHOUT_TIME_ZONE:
                schema = LogicalTypes.timestampMicros().addToSchema(builder.longType());
                break;
            case FLOAT:
                schema = builder.floatType();
                break;
            case DOUBLE:
                schema = builder.doubleType();
                break;
            case VARCHAR:
                schema = builder.stringType();
                break;
            case BINARY:
            case VARBINARY:
                schema = builder.bytesType();
                break;
            case DECIMAL:
                DecimalType dt = (DecimalType) flinkType.getLogicalType();
                LogicalTypes.Decimal avroType = LogicalTypes.decimal(dt.getPrecision(), dt.getScale());
                int fixedSize = minBytesForPrecision[dt.getPrecision()];
                // Need to avoid naming conflict for the fixed fields
                String name;
                if (namespace.equals("")) {
                    name = recordName + ".fixed";
                } else {
                    name = namespace + recordName + ".fixed";
                }
                schema = avroType.addToSchema(SchemaBuilder.fixed(name).size(fixedSize));
                break;
            default:
                throw new IncompatibleSchemaException(String.format("Unsupported type %s", flinkType.toString()), null);
        }
    } else if (flinkType instanceof CollectionDataType) {
        if (type == LogicalTypeRoot.ARRAY) {
            CollectionDataType cdt = (CollectionDataType) flinkType;
            DataType elementType = cdt.getElementDataType();
            schema = builder.array().items(sqlType2AvroSchema(elementType, elementType.getLogicalType().isNullable(), recordName, namespace));
        } else {
            throw new IncompatibleSchemaException("Pulsar only support collection as array", null);
        }
    } else if (flinkType instanceof KeyValueDataType) {
        KeyValueDataType kvType = (KeyValueDataType) flinkType;
        DataType keyType = kvType.getKeyDataType();
        DataType valueType = kvType.getValueDataType();
        if (!(keyType instanceof AtomicDataType) || keyType.getLogicalType().getTypeRoot() != LogicalTypeRoot.VARCHAR) {
            throw new IncompatibleSchemaException("Pulsar only support string key map", null);
        }
        schema = builder.map().values(sqlType2AvroSchema(valueType, valueType.getLogicalType().isNullable(), recordName, namespace));
    } else if (flinkType instanceof FieldsDataType) {
        FieldsDataType fieldsDataType = (FieldsDataType) flinkType;
        String childNamespace = namespace.equals("") ? recordName : namespace + "." + recordName;
        SchemaBuilder.FieldAssembler<Schema> fieldsAssembler = builder.record(recordName).namespace(namespace).fields();
        RowType rowType = (RowType) fieldsDataType.getLogicalType();

        for (String fieldName : rowType.getFieldNames()) {
            DataType ftype = fieldsDataType.getFieldDataTypes().get(fieldName);
            Schema fieldAvroSchema = sqlType2AvroSchema(ftype, ftype.getLogicalType().isNullable(), fieldName, childNamespace);
            fieldsAssembler.name(fieldName).type(fieldAvroSchema).noDefault();
        }
        schema = fieldsAssembler.endRecord();
    } else {
        throw new IncompatibleSchemaException(String.format("Unexpected type %s", flinkType.toString()), null);
    }

    if (nullable) {
        return Schema.createUnion(schema, NULL_SCHEMA);
    } else {
        return schema;
    }
}
 
Example 7
Source File: LogicalTypeChecks.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public List<String> visit(RowType rowType) {
	return rowType.getFieldNames();
}