felixcheung commented on a change in pull request #3422: [ZEPPELIN-4273] 
Support Flink 1.9 for Flink Interpreter
URL: https://github.com/apache/zeppelin/pull/3422#discussion_r311374621
 
 

 ##########
 File path: 
flink/src/main/java/org/apache/zeppelin/flink/FlinkSqlInterrpeter.java
 ##########
 @@ -0,0 +1,227 @@
+/*
+ * 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.zeppelin.flink;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.zeppelin.flink.sql.SqlCommandParser;
+import org.apache.zeppelin.flink.sql.SqlInfo;
+import org.apache.zeppelin.flink.sql.SqlLists;
+import org.apache.zeppelin.interpreter.Interpreter;
+import org.apache.zeppelin.interpreter.InterpreterContext;
+import org.apache.zeppelin.interpreter.InterpreterException;
+import org.apache.zeppelin.interpreter.InterpreterResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.regex.Matcher;
+
+public abstract class FlinkSqlInterrpeter extends Interpreter {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(FlinkSqlInterrpeter.class);
+
+  protected FlinkInterpreter flinkInterpreter;
+  protected TableEnvironment tbenv;
+
+  public FlinkSqlInterrpeter(Properties properties) {
+    super(properties);
+  }
+
+  @Override
+  public void open() throws InterpreterException {
+    flinkInterpreter =
+            getInterpreterInTheSameSessionByClassName(FlinkInterpreter.class);
+  }
+
+  @Override
+  public InterpreterResult interpret(String st,
+                                     InterpreterContext context) throws 
InterpreterException {
+    LOGGER.debug("Interpret code: " + st);
+    flinkInterpreter.getZeppelinContext().setInterpreterContext(context);
+    flinkInterpreter.getZeppelinContext().setNoteGui(context.getNoteGui());
+    flinkInterpreter.getZeppelinContext().setGui(context.getGui());
+
+    checkLocalProperties(context.getLocalProperties());
+
+    // set ClassLoader of current Thread to be the ClassLoader of Flink 
scala-shell,
+    // otherwise codegen will fail to find classes defined in scala-shell
+    ClassLoader originClassLoader = 
Thread.currentThread().getContextClassLoader();
+    try {
+      
Thread.currentThread().setContextClassLoader(flinkInterpreter.getFlinkScalaShellLoader());
+      return runSqlList(st, context);
+    } finally {
+      Thread.currentThread().setContextClassLoader(originClassLoader);
+    }
+  }
+
+
+  protected abstract void checkLocalProperties(Map<String, String> 
localProperties)
+          throws InterpreterException;
+
+  private Optional<SqlCommandParser.SqlCommandCall> parse(String stmt) {
+    // normalize
+    stmt = stmt.trim();
+    // remove ';' at the end
+    if (stmt.endsWith(";")) {
+      stmt = stmt.substring(0, stmt.length() - 1).trim();
+    }
+
+    // parse
+    for (SqlCommandParser.SqlCommand cmd : 
SqlCommandParser.SqlCommand.values()) {
+      final Matcher matcher = cmd.pattern.matcher(stmt);
+      if (matcher.matches()) {
+        final String[] groups = new String[matcher.groupCount()];
+        for (int i = 0; i < groups.length; i++) {
+          groups[i] = matcher.group(i + 1);
+        }
+        return cmd.operandConverter.apply(groups)
+                .map((operands) -> new SqlCommandParser.SqlCommandCall(cmd, 
operands));
+      }
+    }
+    return Optional.empty();
+  }
+
+  private InterpreterResult runSqlList(String sql, InterpreterContext context) 
{
+    List<SqlInfo> sqlLists = SqlLists.getSQLList(sql);
+    List<SqlCommandParser.SqlCommandCall> sqlCommands = new ArrayList<>();
+    for (SqlInfo sqlInfo : sqlLists) {
+      Optional<SqlCommandParser.SqlCommandCall> sqlCommand = 
parse(sqlInfo.getSqlContent());
+      if (!sqlCommand.isPresent()) {
+        return new InterpreterResult(InterpreterResult.Code.ERROR, "Invalid 
Sql statement: "
+                + sqlInfo.getSqlContent());
+      }
+      sqlCommands.add(sqlCommand.get());
+    }
+    for (SqlCommandParser.SqlCommandCall sqlCommand : sqlCommands) {
+      try {
+        callCommand(sqlCommand, context);
+        context.out.flush();
+      }  catch (Throwable e) {
+        LOGGER.error("Fail to run sql:" + sqlCommand.operands[0] + "\n"
+                + ExceptionUtils.getStackTrace(e));
 
 Review comment:
   why not `LOGGER.error("Fail to run sql:" + sqlCommand.operands[0], e);`?

----------------------------------------------------------------
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