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



##########
File path: 
flink-end-to-end-tests/flink-end-to-end-tests-hbase/src/test/java/org/apache/flink/tests/util/hbase/SQLClientHBaseITCase.java
##########
@@ -0,0 +1,191 @@
+/*
+ * 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.tests.util.hbase;
+
+import org.apache.flink.api.common.time.Deadline;
+import org.apache.flink.tests.util.TestUtils;
+import org.apache.flink.tests.util.cache.DownloadCache;
+import org.apache.flink.tests.util.categories.PreCommit;
+import org.apache.flink.tests.util.categories.TravisGroup1;
+import org.apache.flink.tests.util.flink.ClusterController;
+import org.apache.flink.tests.util.flink.FlinkResource;
+import org.apache.flink.tests.util.flink.FlinkResourceSetup;
+import org.apache.flink.tests.util.flink.LocalStandaloneFlinkResourceFactory;
+import org.apache.flink.tests.util.flink.SQLJobSubmission;
+import org.apache.flink.util.FileUtils;
+import org.apache.flink.util.TestLogger;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertThat;
+
+/**
+ * End-to-end test for the HBase connectors.
+ */
+@Category(value = {TravisGroup1.class, PreCommit.class})
+public class SQLClientHBaseITCase extends TestLogger {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(SQLClientHBaseITCase.class);
+
+       private static final String HBASE_E2E_SQL = "hbase_e2e.sql";
+
+       @Rule
+       public final HBaseResource hbase;
+
+       @Rule
+       public final FlinkResource flink = new 
LocalStandaloneFlinkResourceFactory()
+               .create(FlinkResourceSetup.builder().build());
+
+       @Rule
+       public final TemporaryFolder tmp = new TemporaryFolder();
+
+       @ClassRule
+       public static final DownloadCache DOWNLOAD_CACHE = DownloadCache.get();
+
+       private static final Path sqlToolBoxJar = 
TestUtils.getResourceJar(".*SqlToolbox.jar");
+       private static final Path sqlConnectorHBaseJar = 
TestUtils.getResourceJar(".*hbase.jar");
+       private List<Path> hadoopClasspathJars;
+
+       public SQLClientHBaseITCase() {
+               this.hbase = HBaseResource.get();
+       }
+
+       @Before
+       public void before() throws Exception {
+               DOWNLOAD_CACHE.before();
+               Path tmpPath = tmp.getRoot().toPath();
+               LOG.info("The current temporary path: {}", tmpPath);
+
+               // Prepare all hadoop jars under HADOOP_CLASSPATH, use 
yarn.classpath which contains all hadoop jars
+               Path currentModulePath = Paths.get("").toAbsolutePath();
+               Path flinkHomePath = 
currentModulePath.getParent().getParent().toAbsolutePath();
+               Path hadoopClasspath = Paths.get(flinkHomePath.toString(), 
"/flink-yarn-tests/target/yarn.classpath");
+               File hadoopClasspathFile = new File(hadoopClasspath.toString());
+
+               if (!hadoopClasspathFile.exists()) {
+                       throw new FileNotFoundException(HBASE_E2E_SQL);

Review comment:
       Not found `yarn.classpath`?
   

##########
File path: 
flink-end-to-end-tests/flink-end-to-end-tests-hbase/src/main/java/org/apache/flink/tests/util/hbase/LocalStandaloneHBaseResource.java
##########
@@ -0,0 +1,203 @@
+/*
+ * 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.tests.util.hbase;
+
+import org.apache.flink.tests.util.AutoClosableProcess;
+import org.apache.flink.tests.util.CommandLineWrapper;
+import org.apache.flink.tests.util.activation.OperatingSystemRestriction;
+import org.apache.flink.tests.util.cache.DownloadCache;
+import org.apache.flink.util.OperatingSystem;
+
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+/**
+ * {@link HBaseResource} that downloads hbase and set up a local hbase cluster.
+ */
+public class LocalStandaloneHBaseResource implements HBaseResource {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(LocalStandaloneHBaseResource.class);
+
+       private final TemporaryFolder tmp = new TemporaryFolder();
+
+       private final DownloadCache downloadCache = DownloadCache.get();
+       private Path hbaseDir;
+
+       LocalStandaloneHBaseResource() {
+               OperatingSystemRestriction.forbid(
+                       String.format("The %s relies on UNIX utils and shell 
scripts.", getClass().getSimpleName()),
+                       OperatingSystem.WINDOWS);
+       }
+
+       private static String getHBaseDownloadUrl() {
+               return 
"https://archive.apache.org/dist/hbase/1.4.3/hbase-1.4.3-bin.tar.gz";;
+       }
+
+       @Override
+       public void before() throws Exception {
+               tmp.create();
+               downloadCache.before();
+
+               this.hbaseDir = 
tmp.newFolder("hbase").toPath().toAbsolutePath();
+               setupHBaseDist();
+               setupHBaseCluster();
+       }
+
+       private void setupHBaseDist() throws IOException {
+               final Path downloadDirectory = 
tmp.newFolder("getOrDownload").toPath();
+               final Path hbaseArchive = 
downloadCache.getOrDownload(getHBaseDownloadUrl(), downloadDirectory);
+
+               LOG.info("HBase localtion: {}", hbaseDir.toAbsolutePath());
+               AutoClosableProcess.runBlocking(CommandLineWrapper
+                       .tar(hbaseArchive)
+                       .extract()
+                       .zipped()
+                       .strip(1)
+                       .targetDir(hbaseDir)
+                       .build());
+
+               LOG.info("Configure {} as hbase.tmp.dir", 
hbaseDir.toAbsolutePath());
+               final String tmpDirConfig = 
"<configuration><property><name>hbase.tmp.dir</name><value>" + hbaseDir + 
"</value></property></configuration>";
+               Files.write(hbaseDir.resolve(Paths.get("conf", 
"hbase-site.xml")), tmpDirConfig.getBytes());
+       }
+
+       private void setupHBaseCluster() throws IOException {
+               LOG.info("Starting HBase cluster");
+               AutoClosableProcess.runBlocking(
+                       hbaseDir.resolve(Paths.get("bin", 
"start-hbase.sh")).toString());
+
+               while (!isHBaseRunning()) {
+                       try {
+                               LOG.info("Waiting for HBase to start");
+                               Thread.sleep(500L);
+                       } catch (InterruptedException e) {
+                               Thread.currentThread().interrupt();
+                               break;
+                       }
+               }
+       }
+
+       @Override
+       public void afterTestSuccess() {
+               try {
+                       LOG.info("Stopping HBase Cluster");
+                       AutoClosableProcess.runBlocking(
+                               hbaseDir.resolve(Paths.get("bin", 
"hbase-daemon.sh")).toString(),
+                               "stop",
+                               "master");
+
+                       while (isHBaseRunning()) {
+                               try {
+                                       LOG.info("Waiting for HBase to stop");
+                                       Thread.sleep(500L);
+                               } catch (InterruptedException e) {
+                                       Thread.currentThread().interrupt();
+                                       break;
+                               }
+                       }
+
+               } catch (IOException ioe) {
+                       LOG.warn("Error while shutting down hbase.", ioe);
+               }
+               downloadCache.afterTestSuccess();
+               tmp.delete();
+       }
+
+       private static boolean isHBaseRunning() {
+               try {
+                       final AtomicBoolean atomicHMasterStarted = new 
AtomicBoolean(false);
+                       queryHMasterStatus(line -> 
atomicHMasterStarted.compareAndSet(false, line.contains("HMaster")));
+                       return atomicHMasterStarted.get();
+               } catch (IOException ioe) {
+                       return false;
+               }
+       }
+
+       private static void queryHMasterStatus(final Consumer<String> 
stdoutProcessor) throws IOException {
+               AutoClosableProcess
+                       .create("jps")
+                       .setStdoutProcessor(stdoutProcessor)
+                       .runBlocking();
+       }
+
+       @Override
+       public void createTable(String tableName, String... columnFamilies) 
throws IOException {
+               final String createTable = String.format("create '%s',", 
tableName) +
+                       Arrays.stream(columnFamilies)
+                               .map(cf -> String.format("{NAME=>'%s'}", cf))
+                               .collect(Collectors.joining(","));
+
+               executeHBaseShell(createTable);
+       }
+
+       @Override
+       public List<String> scanTable(String tableName) throws IOException {
+               final List<String> result = new ArrayList<>();
+               executeHBaseShell(String.format("scan '%s'", tableName), line 
-> {
+                       if (line.contains("value=")) {
+                               result.add(line);
+                       }
+               });
+               return result;
+       }
+
+       @Override
+       public void putData(String tableName, String rowKey, String 
columnFamily, String columnQualifier, String value) throws IOException {
+               executeHBaseShell(
+                       String.format("put '%s','%s','%s:%s','%s'", tableName, 
rowKey, columnFamily, columnQualifier, value));
+       }
+
+       private void executeHBaseShell(String cmd) throws IOException {
+               executeHBaseShell(cmd, line -> {
+               });
+       }
+
+       private void executeHBaseShell(String cmd, Consumer<String> 
stdoutProcessor) throws IOException {
+               try (AutoClosableProcess autoClosableProcess = 
AutoClosableProcess
+                       .create(hbaseDir.resolve(Paths.get("bin", 
"hbase")).toString(), "shell")
+                       .setStdoutProcessor(stdoutProcessor)
+                       .runNonBlocking()) {
+
+                       try (PrintStream printStream = new 
PrintStream(autoClosableProcess.getProcess().getOutputStream(), true, 
StandardCharsets.UTF_8.name())) {
+                               LOG.info("Executing hbase shell: {}", cmd);
+                               printStream.println(cmd);
+                       }
+
+                       try {
+                               autoClosableProcess.getProcess().waitFor();
+                       } catch (InterruptedException e) {
+                               Thread.currentThread().interrupt();
+                       }
+               }
+       }

Review comment:
       Simplify to 
   
   ```java
   AutoClosableProcess
        .create(hbaseDir.resolve(Paths.get("bin", "hbase")).toString(), "shell")
        .setStdoutProcessor(stdoutProcessor)
        .setStdInputs(cmd)
        .runBlocking();
   ```




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to