Skip to content

Commit 3a3f015

Browse files
libailinzoudaokoulife
authored andcommitted
[Feature-#1886][hbase] New features in sql mode
[Feature-#1886][hbase] New features in sql mode
1 parent bf9458e commit 3a3f015

File tree

20 files changed

+630
-52
lines changed

20 files changed

+630
-52
lines changed

chunjun-connectors/chunjun-connector-hbase-base/src/main/java/com/dtstack/chunjun/connector/hbase/HBaseTableSchema.java

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package com.dtstack.chunjun.connector.hbase;
2020

21+
import com.dtstack.chunjun.connector.hbase.config.HBaseConfig;
22+
2123
import org.apache.flink.api.java.typeutils.TypeExtractor;
2224
import org.apache.flink.table.api.DataTypes;
2325
import org.apache.flink.table.api.TableSchema;
@@ -34,6 +36,7 @@
3436
import java.util.Map;
3537
import java.util.Optional;
3638

39+
import static com.dtstack.chunjun.connector.hbase.config.HBaseConfigConstants.MULTI_VERSION_FIXED_COLUMN;
3740
import static org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType;
3841

3942
/** Helps to specify an HBase Table's schema. */
@@ -315,12 +318,91 @@ private static DataType getRowDataType(String[] fieldNames, DataType[] fieldType
315318
return DataTypes.ROW(fields);
316319
}
317320

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+
318397
/** Construct a {@link HBaseTableSchema} from a {@link TableSchema}. */
319-
public static HBaseTableSchema fromTableSchema(TableSchema schema) {
398+
public static HBaseTableSchema fromTableSchema(TableSchema schema, HBaseConfig conf) {
320399
HBaseTableSchema hbaseSchema = new HBaseTableSchema();
321400
RowType rowType = (RowType) schema.toPhysicalRowDataType().getLogicalType();
322401
for (RowType.RowField field : rowType.getFields()) {
323402
LogicalType fieldType = field.getType();
403+
if (conf.getMode().equalsIgnoreCase(MULTI_VERSION_FIXED_COLUMN)) {
404+
continue;
405+
}
324406
if (fieldType.getTypeRoot() == LogicalTypeRoot.ROW) {
325407
RowType familyType = (RowType) fieldType;
326408
String familyName = field.getName();

chunjun-connectors/chunjun-connector-hbase-base/src/main/java/com/dtstack/chunjun/connector/hbase/config/HBaseConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public class HBaseConfig extends CommonConfig {
5858
private boolean isBinaryRowkey;
5959
private String table;
6060
private int scanCacheSize = 1000;
61+
private int scanBatchSize = -1;
62+
private int maxVersion = Integer.MAX_VALUE;
63+
private String mode = "normal";
6164

6265
// writer
6366
private String nullMode = "SKIP";
@@ -66,6 +69,7 @@ public class HBaseConfig extends CommonConfig {
6669
private long writeBufferSize;
6770
private String rowkeyExpress;
6871
private Integer versionColumnIndex;
72+
private String versionColumnName;
6973
private String versionColumnValue;
7074
private Long ttl;
7175
}

chunjun-connectors/chunjun-connector-hbase-base/src/main/java/com/dtstack/chunjun/connector/hbase/config/HBaseConfigConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ public class HBaseConfigConstants {
3636
public static final long DEFAULT_WRITE_BUFFER_SIZE = 8 * 1024 * 1024L;
3737

3838
public static final boolean DEFAULT_WAL_FLAG = false;
39+
40+
public static final String MULTI_VERSION_FIXED_COLUMN = "multiVersionFixedColumn";
3941
}

chunjun-connectors/chunjun-connector-hbase-base/src/main/java/com/dtstack/chunjun/connector/hbase/converter/HBaseFlatRowConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public Long getVersion(RowData record) {
343343
"version column index out of range: " + versionColumnIndex);
344344
}
345345
if (record.isNullAt(versionColumnIndex)) {
346-
throw new IllegalArgumentException("null verison column!");
346+
throw new IllegalArgumentException("null version column!");
347347
}
348348

349349
timeStampValue = ((ColumnRowData) record).getField(versionColumnIndex).getData();

0 commit comments

Comments
 (0)