[ZEPPELIN-4497] Change zeppelin-jupyter-adapter to zeppelin-jupyter-interpreter

This commit is contained in:
Jeff Zhang 2019-12-20 22:52:21 +08:00
parent e7e45a5e13
commit 2efdc1e0e8
15 changed files with 214 additions and 15 deletions

View file

@ -58,7 +58,7 @@
<module>zeppelin-zengine</module>
<module>zeppelin-display</module>
<module>rlang</module>
<module>zeppelin-jupyter-adapter</module>
<module>zeppelin-jupyter-interpreter</module>
<module>kotlin</module>
<module>groovy</module>
<module>spark</module>

View file

@ -43,7 +43,7 @@
<dependencies>
<dependency>
<groupId>org.apache.zeppelin</groupId>
<artifactId>zeppelin-jupyter-adapter</artifactId>
<artifactId>zeppelin-jupyter-interpreter</artifactId>
<version>${project.version}</version>
</dependency>

View file

@ -22,11 +22,11 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.zeppelin.interpreter.BaseZeppelinContext;
import org.apache.zeppelin.interpreter.InterpreterException;
import org.apache.zeppelin.interpreter.JupyterKernelInterpreter;
import org.apache.zeppelin.interpreter.jupyter.proto.ExecuteRequest;
import org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse;
import org.apache.zeppelin.interpreter.jupyter.proto.ExecuteStatus;
import org.apache.zeppelin.interpreter.remote.RemoteInterpreterUtils;
import org.apache.zeppelin.jupyter.JupyterKernelInterpreter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import py4j.GatewayServer;
@ -39,7 +39,9 @@ import java.util.Map;
import java.util.Properties;
/**
* IPython Interpreter for Zeppelin
* IPython Interpreter for Zeppelin. It enhances the JupyterKernelInterpreter by setting up
* communication between JVM and Python process via py4j. So that in IPythonInterpreter
* you can use ZeppelinContext.
*/
public class IPythonInterpreter extends JupyterKernelInterpreter {

View file

@ -21,20 +21,20 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>zeppelin</artifactId>
<artifactId>zeppelin-interpreter-parent</artifactId>
<groupId>org.apache.zeppelin</groupId>
<version>0.9.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
<relativePath>../zeppelin-interpreter-parent/pom.xml</relativePath>
</parent>
<groupId>org.apache.zeppelin</groupId>
<artifactId>zeppelin-jupyter-adapter</artifactId>
<artifactId>zeppelin-jupyter-interpreter</artifactId>
<packaging>jar</packaging>
<version>0.9.0-SNAPSHOT</version>
<name>Zeppelin: Jupyter Adapter</name>
<properties>
<interpreter.name>python</interpreter.name>
<interpreter.name>jupyter</interpreter.name>
<python.py4j.version>0.10.7</python.py4j.version>
<grpc.version>1.15.0</grpc.version>
</properties>
@ -178,6 +178,14 @@
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>

View file

@ -0,0 +1,112 @@
/*
* 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.jupyter;
import org.apache.zeppelin.interpreter.AbstractInterpreter;
import org.apache.zeppelin.interpreter.BaseZeppelinContext;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterException;
import org.apache.zeppelin.interpreter.InterpreterResult;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* Interpreter for Jupyter kernel. It can work for any Jupyter kernel as long as the kernel
* is installed. Specify the kernel name in paragraph properties.
* Run it via `%jupyter(kernel=ipython)`
*
*/
public class JupyterInterpreter extends AbstractInterpreter {
private Map<String, JupyterKernelInterpreter> kernelInterpreterMap = new HashMap<>();
public JupyterInterpreter(Properties properties) {
super(properties);
}
@Override
public BaseZeppelinContext getZeppelinContext() {
return new JupyterZeppelinContext(getInterpreterGroup().getInterpreterHookRegistry(), 1000);
}
@Override
protected InterpreterResult internalInterpret(
String st, InterpreterContext context) throws InterpreterException {
String kernel = context.getLocalProperties().get("kernel");
if (kernel == null) {
return new InterpreterResult(InterpreterResult.Code.ERROR, "No kernel is specified");
}
JupyterKernelInterpreter kernelInterpreter = null;
synchronized (kernelInterpreterMap) {
if (kernelInterpreterMap.containsKey(kernel)) {
kernelInterpreter = kernelInterpreterMap.get(kernel);
} else {
kernelInterpreter = new JupyterKernelInterpreter(kernel, properties);
kernelInterpreter.open();
kernelInterpreterMap.put(kernel, kernelInterpreter);
}
}
return kernelInterpreter.interpret(st, context);
}
@Override
public void open() throws InterpreterException {
// do nothing
}
@Override
public void close() throws InterpreterException {
for (JupyterKernelInterpreter kernelInterpreter : kernelInterpreterMap.values()) {
kernelInterpreter.close();
}
}
@Override
public void cancel(InterpreterContext context) throws InterpreterException {
String kernel = context.getLocalProperties().get("kernel");
if (kernel == null) {
throw new InterpreterException("No kernel is specified");
}
JupyterKernelInterpreter kernelInterpreter = kernelInterpreterMap.get(kernel);
if (kernelInterpreter != null) {
throw new InterpreterException("No such interpreter: " + kernel);
}
kernelInterpreter.cancel(context);
}
@Override
public FormType getFormType() throws InterpreterException {
return FormType.NATIVE;
}
@Override
public int getProgress(InterpreterContext context) throws InterpreterException {
String kernel = context.getLocalProperties().get("kernel");
if (kernel == null) {
throw new InterpreterException("No kernel is specified");
}
JupyterKernelInterpreter kernelInterpreter = kernelInterpreterMap.get(kernel);
if (kernelInterpreter == null) {
throw new InterpreterException("No such interpreter: " + kernel);
}
return kernelInterpreter.getProgress(context);
}
}

View file

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.zeppelin.interpreter;
package org.apache.zeppelin.jupyter;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

View file

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.zeppelin.interpreter;
package org.apache.zeppelin.jupyter;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.ManagedChannelBuilder;
@ -25,6 +25,11 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.zeppelin.interpreter.BaseZeppelinContext;
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.apache.zeppelin.interpreter.jupyter.proto.CancelRequest;
import org.apache.zeppelin.interpreter.jupyter.proto.CompletionRequest;
import org.apache.zeppelin.interpreter.jupyter.proto.CompletionResponse;
@ -52,10 +57,11 @@ import java.util.Map;
import java.util.Properties;
/**
* Jupyter Kernel adapter for Zeppelin. All the jupyter kernel could be used by Zeppelin
* by extending this class.
* Jupyter Kernel Interpreter for Zeppelin. One instance of this class represents one
* Jupyter Kernel. You can enhance the jupyter kernel by extending this class.
* e.g. IPythonInterpreter.
*/
public abstract class JupyterKernelInterpreter extends Interpreter {
public class JupyterKernelInterpreter extends Interpreter {
private static final Logger LOGGER = LoggerFactory.getLogger(JupyterKernelInterpreter.class);
@ -63,6 +69,8 @@ public abstract class JupyterKernelInterpreter extends Interpreter {
protected JupyterKernelClient jupyterKernelClient;
protected BaseZeppelinContext zeppelinContext;
private String kernel;
// working directory of jupyter kernel
protected File kernelWorkDir;
// python executable file for launching the jupyter kernel
@ -71,11 +79,18 @@ public abstract class JupyterKernelInterpreter extends Interpreter {
private InterpreterOutputStream interpreterOutput = new InterpreterOutputStream(LOGGER);
public JupyterKernelInterpreter(String kernel, Properties properties) {
this(properties);
this.kernel = kernel;
}
public JupyterKernelInterpreter(Properties properties) {
super(properties);
}
public abstract String getKernelName();
public String getKernelName() {
return this.kernel;
}
public List<String> getRequiredPackages() {
List<String> requiredPackages = new ArrayList<>();
@ -85,7 +100,9 @@ public abstract class JupyterKernelInterpreter extends Interpreter {
return requiredPackages;
}
public abstract BaseZeppelinContext buildZeppelinContext();
protected BaseZeppelinContext buildZeppelinContext() {
return new JupyterZeppelinContext(null, 1000);
}
@Override
public void open() throws InterpreterException {

View file

@ -0,0 +1,46 @@
/*
* 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.jupyter;
import org.apache.zeppelin.interpreter.BaseZeppelinContext;
import org.apache.zeppelin.interpreter.InterpreterHookRegistry;
import java.util.List;
import java.util.Map;
public class JupyterZeppelinContext extends BaseZeppelinContext {
public JupyterZeppelinContext(InterpreterHookRegistry hooks, int maxResult) {
super(hooks, maxResult);
}
@Override
public Map<String, String> getInterpreterClassMap() {
return null;
}
@Override
public List<Class> getSupportedClasses() {
return null;
}
@Override
public String showData(Object obj, int maxResult) {
return null;
}
}

View file

@ -0,0 +1,14 @@
[
{
"group": "jupyter",
"name": "jupyter",
"className": "org.apache.zeppelin.jupyter.JupyterInterpreter",
"properties": {
},
"editor": {
"language": "text",
"editOnDblClick": false,
"completionSupport": true
}
}
]