diff --git a/docs/docs/index.md b/docs/docs/index.md index 5fa9c6d19b6..8465e760e25 100644 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -29,6 +29,7 @@ group: nav-right * [tajo](../docs/pleasecontribute.html) * [postgresql, hawq](./interpreter/postgresql.html) * [geode](./interpreter/geode.html) +* [js](../interpreter/js.html) ### Display System diff --git a/docs/docs/interpreter/js.md b/docs/docs/interpreter/js.md new file mode 100644 index 00000000000..27173904719 --- /dev/null +++ b/docs/docs/interpreter/js.md @@ -0,0 +1,159 @@ +--- +layout: page +title: "Javascript Interpreter" +description: "" +group: manual +--- + + +## Javascript Interpreter for Apache Zeppelin + +This Interpreter supports the Java 8 "Nashhorn" javascript engine, and it also exposes some Zeppelin components. For example you can iterate over the current Notebook's runners: + + + + + +### Create Interpreter + +By default Zeppelin creates one `PSQL` instance. You can remove it or create new instances. + +Multiple PSQL instances can be created, each configured to the same or different backend databases. But over time a `Notebook` can have only one PSQL interpreter instance `bound`. That means you _can not_ connect to different databases in the same `Notebook`. This is a known Zeppelin limitation. + +To create new PSQL instance open the `Interprter` section and click the `+Create` button. Pick a `Name` of your choice and from the `Interpreter` drop-down select `psql`. Then follow the configuration instructions and `Save` the new instance. + +> Note: The `Name` of the instance is used only to distinct the instances while binding them to the `Notebook`. The `Name` is irrelevant inside the `Notebook`. In the `Notebook` you must use `%psql.sql` tag. + +### Bind to Notebook +In the `Notebook` click on the `settings` icon in the top right corner. The select/deselect the interpreters to be bound with the `Notebook`. + +### Configuration +You can modify the configuration of the PSQL from the `Interpreter` section. The PSQL interpreter expenses the following properties: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Property NameDescriptionDefault Value
postgresql.urlJDBC URL to connect to jdbc:postgresql://localhost:5432
postgresql.userJDBC user namegpadmin
postgresql.passwordJDBC password
postgresql.driver.nameJDBC driver name. In this version the driver name is fixed and should not be changedorg.postgresql.Driver
postgresql.max.resultMax number of SQL result to display to prevent the browser overload1000
+ + +### How to use +``` +Tip: Use (CTRL + .) for SQL auto-completion. +``` +#### DDL and SQL commands + +Start the paragraphs with the full `%psql.sql` prefix tag! The short notation: `%psql` would still be able run the queries but the syntax highlighting and the auto-completions will be disabled. + +You can use the standard CREATE / DROP / INSERT commands to create or modify the data model: + +```sql +%psql.sql +drop table if exists mytable; +create table mytable (i int); +insert into mytable select generate_series(1, 100); +``` + +Then in a separate paragraph run the query. + +```sql +%psql.sql +select * from mytable; +``` + +> Note: You can have multiple queries in the same paragraph but only the result from the first is displayed. [[1](https://issues.apache.org/jira/browse/ZEPPELIN-178)], [[2](https://issues.apache.org/jira/browse/ZEPPELIN-212)]. + +For example, this will execute both queries but only the count result will be displayed. If you revert the order of the queries the mytable content will be shown instead. + +```sql +%psql.sql +select count(*) from mytable; +select * from mytable; +``` + +#### PSQL command line tools + +Use the Shell Interpreter (`%sh`) to access the command line [PSQL](http://www.postgresql.org/docs/9.4/static/app-psql.html) interactively: + +```bash +%sh +psql -h phd3.localdomain -U gpadmin -p 5432 < + + + + 4.0.0 + + + zeppelin + org.apache.zeppelin + 0.6.0-incubating-SNAPSHOT + .. + + + org.apache.zeppelin + zeppelin-js + jar + 0.6.0-incubating-SNAPSHOT + Zeppelin: JS interpreter + http://zeppelin.incubator.apache.org + + + + ${project.groupId} + zeppelin-interpreter + ${project.version} + provided + + + + org.slf4j + slf4j-api + + + + org.slf4j + slf4j-log4j12 + + + + junit + junit + test + + + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.7 + + true + + + + + maven-enforcer-plugin + 1.3.1 + + + enforce + none + + + + + + maven-dependency-plugin + 2.8 + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/../../interpreter/js + false + false + true + runtime + + + + copy-artifact + package + + copy + + + ${project.build.directory}/../../interpreter/js + false + false + true + runtime + + + ${project.groupId} + ${project.artifactId} + ${project.version} + ${project.packaging} + + + + + + + + + + diff --git a/js/src/main/java/org/apache/zeppelin/js/JsInterpreter.java b/js/src/main/java/org/apache/zeppelin/js/JsInterpreter.java new file mode 100644 index 00000000000..440892449e6 --- /dev/null +++ b/js/src/main/java/org/apache/zeppelin/js/JsInterpreter.java @@ -0,0 +1,123 @@ +/* + * 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.js; + +import java.io.StringReader; +import java.io.StringWriter; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.apache.zeppelin.interpreter.Interpreter; +import org.apache.zeppelin.interpreter.InterpreterContext; +import org.apache.zeppelin.interpreter.InterpreterResult; +import org.apache.zeppelin.scheduler.Scheduler; +import org.apache.zeppelin.scheduler.SchedulerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Javascript interpreter for Zeppelin. + * + * @author Leonardo Foderaro + * + * + */ +public class JsInterpreter extends Interpreter { + Logger logger = LoggerFactory.getLogger(JsInterpreter.class); + int commandTimeOut = 600000; + + ScriptEngine engine; + + static { + Interpreter.register("js", JsInterpreter.class.getName()); + } + + public JsInterpreter(Properties property) { + super(property); + + engine = new ScriptEngineManager().getEngineByName("nashorn"); + + } + + @Override + public void open() { + } + + @Override + public void close() { + } + + @Override + public InterpreterResult interpret(String cmd, + InterpreterContext contextInterpreter) { + logger.info("Run js command '" + cmd + "'"); + long start = System.currentTimeMillis(); + + StringWriter sw = new StringWriter(); + + String result; + + engine.getContext().setWriter(sw); + + try { + Object o = engine.eval(new StringReader(cmd)); + + if (o != null) { + result = sw.toString() + "\n\r" + o.toString(); + } else { + result = sw.toString(); + } + + return new InterpreterResult(InterpreterResult.Code.SUCCESS, result.toString()); + } catch (ScriptException e) { + return new InterpreterResult(InterpreterResult.Code.ERROR, e.getMessage()); + } + + } + + @Override + public void cancel(InterpreterContext context) { + } + + @Override + public FormType getFormType() { + return FormType.SIMPLE; + } + + @Override + public int getProgress(InterpreterContext context) { + return 0; + } + + @Override + public Scheduler getScheduler() { + return SchedulerFactory.singleton().createOrGetFIFOScheduler( + JsInterpreter.class.getName() + this.hashCode()); + } + + @Override + public List completion(String buf, int cursor) { + return null; + } + +} diff --git a/pom.xml b/pom.xml index 02f161fbc7f..cbbeef8f660 100755 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,7 @@ markdown angular shell + js hive phoenix geode