You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/05/16 05:22:58 UTC

[GitHub] [flink] wuchong commented on a change in pull request #12178: [FLINK-17028][hbase] Introduce a new HBase connector with new property keys

wuchong commented on a change in pull request #12178:
URL: https://github.com/apache/flink/pull/12178#discussion_r426120300



##########
File path: flink-connectors/flink-connector-hbase/src/main/java/org/apache/flink/connector/hbase/util/HBaseSerde.java
##########
@@ -0,0 +1,371 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.connector.hbase.util;
+
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import org.apache.hadoop.hbase.client.Delete;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.util.Bytes;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Arrays;
+
+import static org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getPrecision;
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/**
+ * Utilities for HBase serialization and deserialization.
+ */
+public class HBaseSerde implements Serializable {
+
+	private static final long serialVersionUID = 1L;
+	private static final byte[] EMPTY_BYTES = new byte[]{};
+
+	// row key index in output row
+	private final int rowkeyIndex;
+
+	// family keys
+	private final byte[][] families;
+	// qualifier keys
+	private final byte[][][] qualifiers;
+
+	private final int fieldLength;
+
+	private GenericRowData reusedRow;
+	private GenericRowData[] reusedFamilyRows;
+
+	private final FieldEncoder keyEncoder;
+	private final FieldDecoder keyDecoder;
+	private final FieldEncoder[][] qualifierEncoders;
+	private final FieldDecoder[][] qualifierDecoders;
+
+	public HBaseSerde(HBaseTableSchema hbaseSchema) {
+		this.families = hbaseSchema.getFamilyKeys();
+		this.rowkeyIndex = hbaseSchema.getRowKeyIndex();
+		LogicalType rowkeyType = hbaseSchema.getRowKeyDataType().map(DataType::getLogicalType).orElse(null);
+
+		// field length need take row key into account if it exists.
+		checkArgument(rowkeyIndex != -1 && rowkeyType != null, "row key is not set.");
+		this.fieldLength = families.length + 1;
+
+		// prepare output rows
+		this.reusedRow = new GenericRowData(fieldLength);
+		this.reusedFamilyRows = new GenericRowData[families.length];
+
+		// row key should never be null
+		this.keyEncoder = createFieldEncoder(rowkeyType);
+		this.keyDecoder = createFieldDecoder(rowkeyType);
+
+		this.qualifiers = new byte[families.length][][];
+		this.qualifierEncoders = new FieldEncoder[families.length][];
+		this.qualifierDecoders = new FieldDecoder[families.length][];
+		String[] familyNames = hbaseSchema.getFamilyNames();
+		for (int f = 0; f < families.length; f++) {
+			this.qualifiers[f] = hbaseSchema.getQualifierKeys(familyNames[f]);
+			DataType[] dataTypes = hbaseSchema.getQualifierDataTypes(familyNames[f]);
+			this.qualifierEncoders[f] = Arrays.stream(dataTypes)
+				.map(DataType::getLogicalType)
+				.map(HBaseSerde::createNullableFieldEncoder)
+				.toArray(FieldEncoder[]::new);
+			this.qualifierDecoders[f] = Arrays.stream(dataTypes)
+				.map(DataType::getLogicalType)
+				.map(HBaseSerde::createNullableFieldDecoder)
+				.toArray(FieldDecoder[]::new);
+			this.reusedFamilyRows[f] = new GenericRowData(dataTypes.length);
+		}
+	}
+
+	/**
+	 * Returns an instance of Put that writes record to HBase table.
+	 *
+	 * @return The appropriate instance of Put for this use case.
+	 */
+	public @Nullable Put createPutMutation(RowData row) {
+		byte[] rowkey = keyEncoder.encode(row, rowkeyIndex);
+		if (rowkey.length == 0) {
+			// drop dirty records, rowkey shouldn't be zero length
+			return null;
+		}
+		// upsert
+		Put put = new Put(rowkey);
+		for (int i = 0; i < fieldLength; i++) {
+			if (i != rowkeyIndex) {
+				int f = i > rowkeyIndex ? i - 1 : i;
+				// get family key
+				byte[] familyKey = families[f];
+				RowData familyRow = row.getRow(i, qualifiers[f].length);
+				for (int q = 0; q < this.qualifiers[f].length; q++) {
+					// get quantifier key
+					byte[] qualifier = qualifiers[f][q];
+					// serialize value
+					byte[] value = qualifierEncoders[f][q].encode(familyRow, q);
+					put.addColumn(familyKey, qualifier, value);
+				}
+			}
+		}
+		return put;
+	}
+
+	/**
+	 * Returns an instance of Delete that remove record from HBase table.
+	 *
+	 * @return The appropriate instance of Delete for this use case.
+	 */
+	public @Nullable Delete createDeleteMutation(RowData row) {
+		byte[] rowkey = keyEncoder.encode(row, rowkeyIndex);
+		if (rowkey.length == 0) {
+			// drop dirty records, rowkey shouldn't be zero length
+			return null;

Review comment:
       I don't think we should throw exception here by default. Otherwise, it's meaningless to add this condition. IMO, this is a behavior of the storage (not accept empty key). If there is a need to see the exception, we can add an option in the future.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org