|
18 | 18 |
|
19 | 19 | package com.dtstack.chunjun.connector.hbase;
|
20 | 20 |
|
| 21 | +import com.dtstack.chunjun.connector.hbase.config.HBaseConfig; |
| 22 | + |
21 | 23 | import org.apache.flink.api.java.typeutils.TypeExtractor;
|
22 | 24 | import org.apache.flink.table.api.DataTypes;
|
23 | 25 | import org.apache.flink.table.api.TableSchema;
|
|
34 | 36 | import java.util.Map;
|
35 | 37 | import java.util.Optional;
|
36 | 38 |
|
| 39 | +import static com.dtstack.chunjun.connector.hbase.config.HBaseConfigConstants.MULTI_VERSION_FIXED_COLUMN; |
37 | 40 | import static org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType;
|
38 | 41 |
|
39 | 42 | /** Helps to specify an HBase Table's schema. */
|
@@ -315,12 +318,91 @@ private static DataType getRowDataType(String[] fieldNames, DataType[] fieldType
|
315 | 318 | return DataTypes.ROW(fields);
|
316 | 319 | }
|
317 | 320 |
|
| 321 | + /** |
| 322 | + * Converts this {@link HBaseTableSchema} to {@link DataType}, the fields are consisted of |
| 323 | + * families and rowkey, the order is in the definition order (i.e. calling {@link |
| 324 | + * #addColumn(String, String, Class)} and {@link #setRowKey(String, Class)}). The family field |
| 325 | + * is a composite type which is consisted of qualifiers. |
| 326 | + * |
| 327 | + * @return the {@link DataType} derived from the {@link HBaseTableSchema}. |
| 328 | + */ |
| 329 | + public DataType convertToDataType() { |
| 330 | + String[] familyNames = getFamilyNames(); |
| 331 | + if (rowKeyInfo != null) { |
| 332 | + String[] fieldNames = new String[familyNames.length + 1]; |
| 333 | + DataType[] fieldTypes = new DataType[familyNames.length + 1]; |
| 334 | + for (int i = 0; i < fieldNames.length; i++) { |
| 335 | + if (i == rowKeyInfo.rowKeyIndex) { |
| 336 | + fieldNames[i] = rowKeyInfo.rowKeyName; |
| 337 | + fieldTypes[i] = rowKeyInfo.rowKeyType; |
| 338 | + } else { |
| 339 | + int familyIndex = i < rowKeyInfo.rowKeyIndex ? i : i - 1; |
| 340 | + String family = familyNames[familyIndex]; |
| 341 | + fieldNames[i] = family; |
| 342 | + fieldTypes[i] = |
| 343 | + getRowDataType( |
| 344 | + getQualifierNames(family), getQualifierDataTypes(family)); |
| 345 | + } |
| 346 | + } |
| 347 | + DataTypes.Field[] fields = new DataTypes.Field[fieldNames.length]; |
| 348 | + for (int i = 0; i < fields.length; i++) { |
| 349 | + fields[i] = DataTypes.FIELD(fieldNames[i], fieldTypes[i]); |
| 350 | + } |
| 351 | + return DataTypes.ROW(fields); |
| 352 | + } else { |
| 353 | + String[] fieldNames = new String[familyNames.length]; |
| 354 | + DataType[] fieldTypes = new DataType[familyNames.length]; |
| 355 | + for (int i = 0; i < fieldNames.length; i++) { |
| 356 | + String family = familyNames[i]; |
| 357 | + fieldNames[i] = family; |
| 358 | + fieldTypes[i] = |
| 359 | + getRowDataType(getQualifierNames(family), getQualifierDataTypes(family)); |
| 360 | + } |
| 361 | + DataTypes.Field[] fields = new DataTypes.Field[fieldNames.length]; |
| 362 | + for (int i = 0; i < fields.length; i++) { |
| 363 | + fields[i] = DataTypes.FIELD(fieldNames[i], fieldTypes[i]); |
| 364 | + } |
| 365 | + return DataTypes.ROW(fields); |
| 366 | + } |
| 367 | + } |
| 368 | + |
| 369 | + /** Construct a {@link HBaseTableSchema} from a {@link DataType}. */ |
| 370 | + public static HBaseTableSchema fromDataType(DataType physicalRowType, HBaseConfig conf) { |
| 371 | + HBaseTableSchema hbaseSchema = new HBaseTableSchema(); |
| 372 | + RowType rowType = (RowType) physicalRowType.getLogicalType(); |
| 373 | + for (RowType.RowField field : rowType.getFields()) { |
| 374 | + LogicalType fieldType = field.getType(); |
| 375 | + if (conf.getMode().equalsIgnoreCase(MULTI_VERSION_FIXED_COLUMN)) { |
| 376 | + continue; |
| 377 | + } |
| 378 | + if (fieldType.getTypeRoot() == LogicalTypeRoot.ROW) { |
| 379 | + RowType familyType = (RowType) fieldType; |
| 380 | + String familyName = field.getName(); |
| 381 | + for (RowType.RowField qualifier : familyType.getFields()) { |
| 382 | + hbaseSchema.addColumn( |
| 383 | + familyName, |
| 384 | + qualifier.getName(), |
| 385 | + fromLogicalToDataType(qualifier.getType())); |
| 386 | + } |
| 387 | + } else if (fieldType.getChildren().size() == 0) { |
| 388 | + hbaseSchema.setRowKey(field.getName(), fromLogicalToDataType(fieldType)); |
| 389 | + } else { |
| 390 | + throw new IllegalArgumentException( |
| 391 | + "Unsupported field type '" + fieldType + "' for HBase."); |
| 392 | + } |
| 393 | + } |
| 394 | + return hbaseSchema; |
| 395 | + } |
| 396 | + |
318 | 397 | /** Construct a {@link HBaseTableSchema} from a {@link TableSchema}. */
|
319 |
| - public static HBaseTableSchema fromTableSchema(TableSchema schema) { |
| 398 | + public static HBaseTableSchema fromTableSchema(TableSchema schema, HBaseConfig conf) { |
320 | 399 | HBaseTableSchema hbaseSchema = new HBaseTableSchema();
|
321 | 400 | RowType rowType = (RowType) schema.toPhysicalRowDataType().getLogicalType();
|
322 | 401 | for (RowType.RowField field : rowType.getFields()) {
|
323 | 402 | LogicalType fieldType = field.getType();
|
| 403 | + if (conf.getMode().equalsIgnoreCase(MULTI_VERSION_FIXED_COLUMN)) { |
| 404 | + continue; |
| 405 | + } |
324 | 406 | if (fieldType.getTypeRoot() == LogicalTypeRoot.ROW) {
|
325 | 407 | RowType familyType = (RowType) fieldType;
|
326 | 408 | String familyName = field.getName();
|
|
0 commit comments