lincoln-lil commented on a change in pull request #9075:  [FLINK-10245][hbase] 
Add an upsert table sink factory for HBase
URL: https://github.com/apache/flink/pull/9075#discussion_r302347191
 
 

 ##########
 File path: 
flink-connectors/flink-hbase/src/main/java/org/apache/flink/addons/hbase/HBaseUpsertSinkFunction.java
 ##########
 @@ -0,0 +1,257 @@
+/*
+ * 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.addons.hbase;
+
+import org.apache.flink.addons.hbase.util.HBaseConfigurationUtil;
+import org.apache.flink.addons.hbase.util.HBaseReadWriteHelper;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.state.FunctionInitializationContext;
+import org.apache.flink.runtime.state.FunctionSnapshotContext;
+import org.apache.flink.runtime.util.ExecutorThreadFactory;
+import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
+import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
+import org.apache.flink.types.Row;
+import org.apache.flink.util.StringUtils;
+
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.TableNotFoundException;
+import org.apache.hadoop.hbase.client.BufferedMutator;
+import org.apache.hadoop.hbase.client.BufferedMutatorParams;
+import org.apache.hadoop.hbase.client.Connection;
+import org.apache.hadoop.hbase.client.ConnectionFactory;
+import org.apache.hadoop.hbase.client.Delete;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/**
+ * The upsert sink for HBase.
+ *
+ * <p>This class leverage {@link BufferedMutator} to buffer multiple
+ * {@link org.apache.hadoop.hbase.client.Mutation Mutations} before sending 
the requests to cluster.
+ * The buffering strategy can be configured by {@code 
bufferFlushMaxSizeInBytes},
+ * {@code bufferFlushMaxMutations} and {@code bufferFlushIntervalMillis}.</p>
+ */
+public class HBaseUpsertSinkFunction
+               extends RichSinkFunction<Tuple2<Boolean, Row>>
+               implements CheckpointedFunction, 
BufferedMutator.ExceptionListener {
+
+       private static final long serialVersionUID = 1L;
+       private static final Logger LOG = 
LoggerFactory.getLogger(HBaseUpsertSinkFunction.class);
+
+       private final String hTableName;
+       private final HBaseTableSchema schema;
+       private final byte[] serializedConfig;
+
+       private final long bufferFlushMaxSizeInBytes;
+       private final long bufferFlushMaxMutations;
+       private final long bufferFlushIntervalMillis;
+
+       private transient HBaseReadWriteHelper helper;
+
+       private transient Connection connection;
+       private transient BufferedMutator mutator;
+
+       private transient ScheduledExecutorService executor;
+       private transient ScheduledFuture scheduledFuture;
+       private transient AtomicLong numPendingRequests;
+
+       private transient volatile boolean closed = false;
+
+       /**
+        * This is set from inside the {@link 
BufferedMutator.ExceptionListener} if a {@link Throwable}
+        * was thrown.
+        *
+        * <p>Errors will be checked and rethrown before processing each input 
element, and when the sink is closed.
+        */
+       private final AtomicReference<Throwable> failureThrowable = new 
AtomicReference<>();
+
+       public HBaseUpsertSinkFunction(
+                       String hTableName,
+                       HBaseTableSchema schema,
+                       org.apache.hadoop.conf.Configuration conf,
+                       long bufferFlushMaxSizeInBytes,
+                       long bufferFlushMaxMutations,
+                       long bufferFlushIntervalMillis) {
+               checkArgument(schema.getRowKeyName().isPresent(), 
"HBaseUpsertSinkFunction requires rowkey is set.");
+               this.hTableName = hTableName;
+               this.schema = schema;
+               // Configuration is not serializable
+               this.serializedConfig = 
HBaseConfigurationUtil.serializeConfiguration(conf);
+               this.bufferFlushMaxSizeInBytes = bufferFlushMaxSizeInBytes;
+               this.bufferFlushMaxMutations = bufferFlushMaxMutations;
+               this.bufferFlushIntervalMillis = bufferFlushIntervalMillis;
+       }
+
+       @Override
+       public void open(Configuration parameters) throws Exception {
+               LOG.info("start open ...");
+               org.apache.hadoop.conf.Configuration config = 
prepareRuntimeConfiguration();
+               try {
+                       this.helper = new HBaseReadWriteHelper(schema);
+                       this.numPendingRequests = new AtomicLong(0);
+
+                       if (null == connection) {
+                               this.connection = 
ConnectionFactory.createConnection(config);
+                       }
+                       // co BufferedMutatorExample-04-Params Create a 
parameter instance, set the table name and custom listener reference.
 
 Review comment:
   refine comments

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to