Skip to content

Commit

Permalink
Parameterized form for sql or md
Browse files Browse the repository at this point in the history
  • Loading branch information
Leemoonsoo committed Aug 27, 2014
1 parent 5928f68 commit 774138b
Show file tree
Hide file tree
Showing 17 changed files with 528 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public void cancel() {
@Override
public void bindValue(String name, Object o) {
}

@Override
public FormType getFormType() {
return FormType.SIMPLE;
}
}
16 changes: 11 additions & 5 deletions spark/src/main/java/com/nflabs/zeppelin/spark/SparkRepl.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public SparkContext getSparkContext(){
public SQLContext getSQLContext(){
return sqlc;
}

@Override
public void initialize(){
Settings settings = new Settings();
settings.classpath().value_$eq(System.getProperty("java.class.path"));

PrintStream printStream = new PrintStream(out);
this.interpreter = new SparkILoop(null, new PrintWriter(out));
interpreter.settings_$eq(settings);

Expand All @@ -64,9 +64,9 @@ public void initialize(){
// redirect stdout
intp.interpret("@transient var _binder = new java.util.HashMap[String, Object]()");
Map<String, Object> binder = (Map<String, Object>) getValue("_binder");
binder.put("out", new PrintStream(out));
binder.put("out", printStream);
//intp.interpret("System.setOut(_binder.get(\"out\").asInstanceOf[java.io.PrintStream])");
intp.interpret("Console.setOut(_binder.get(\"out\").asInstanceOf[java.io.PrintStream])");
//intp.interpret("Console.setOut(_binder.get(\"out\").asInstanceOf[java.io.PrintStream])");

intp.interpret("@transient val sc = com.nflabs.zeppelin.spark.SparkRepl.interpreter.createSparkContext()\n");
intp.interpret("import org.apache.spark.SparkContext._");
Expand Down Expand Up @@ -104,11 +104,12 @@ public ReplResult interpret(String line){

public ReplResult interpret(String [] lines){
synchronized(this){
intp.interpret("Console.setOut(_binder.get(\"out\").asInstanceOf[java.io.PrintStream])");
out.reset();
sc.setJobGroup(jobGroup, "Zeppelin", false);
Code r = null;
String incomplete = "";
for(String s : lines) {
for(String s : lines) {
scala.tools.nsc.interpreter.Results.Result res = intp.interpret(incomplete+s);
r = getResultCode(res);

Expand Down Expand Up @@ -160,6 +161,11 @@ private Code getResultCode(scala.tools.nsc.interpreter.Results.Result r){
public void destroy() {

}

@Override
public FormType getFormType() {
return FormType.NATIVE;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,9 @@ public void bindValue(String name, Object o) {

}

@Override
public FormType getFormType() {
return FormType.SIMPLE;
}

}
12 changes: 12 additions & 0 deletions spark/src/test/java/com/nflabs/zeppelin/spark/SparkReplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Properties;

Expand Down Expand Up @@ -37,6 +38,17 @@ public void testBasicRepl() {
repl.interpret("val ver = sc.version");
assertNotNull(repl.getValue("ver"));
assertEquals("HELLO\n", repl.interpret("println(\"HELLO\")").message());
/*
repl.interpret("@transient var _binder = new java.util.HashMap[String, Object]()");
Map<String, Object> binder = (Map<String, Object>) repl.getValue("_binder");
binder.put("out", new PrintStream(new ByteArrayOutputStream()));
System.err.println(repl.interpret("System.setOut(_binder.get(\"out\").asInstanceOf[java.io.PrintStream])").message());
System.err.println(repl.interpret("Console.setOut(_binder.get(\"out\").asInstanceOf[java.io.PrintStream])").message());
//System.err.println(repl.interpret("def println(s:Any){ _binder.get(\"out\").asInstanceOf[java.io.PrintStream]).println(s)}").message());
//System.err.println(repl.interpret("def print(s:Any){ _binder.get(\"out\").asInstanceOf[java.io.PrintStream]).print(s)}").message());
*
*/

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
Expand All @@ -15,7 +14,6 @@

import com.nflabs.zeppelin.conf.ZeppelinConfiguration;
import com.nflabs.zeppelin.conf.ZeppelinConfiguration.ConfVars;
import com.nflabs.zeppelin.repl.Repl;
import com.nflabs.zeppelin.repl.ReplFactory;
import com.nflabs.zeppelin.scheduler.Scheduler;
import com.nflabs.zeppelin.scheduler.SchedulerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import java.util.Map;
import java.util.Properties;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nflabs.zeppelin.notebook.form.Form;
import com.nflabs.zeppelin.notebook.form.Input;
import com.nflabs.zeppelin.repl.Repl;
import com.nflabs.zeppelin.repl.ReplResult;
import com.nflabs.zeppelin.repl.Repl.FormType;
import com.nflabs.zeppelin.scheduler.Job;
import com.nflabs.zeppelin.scheduler.JobListener;
import com.nflabs.zeppelin.server.form.Form;

/**
* execution unit
Expand Down Expand Up @@ -108,11 +109,20 @@ protected Object jobRun() throws Throwable {
logger().error("Can not find interpreter name "+repl);
throw new RuntimeException("Can not find interpreter for "+getRequiredReplName());
}

String script = getScriptBody();
// inject form
form.clearForms();
repl.bindValue("form", form);
logger().info("RUN : "+getScriptBody());
ReplResult ret = repl.interpret(getScriptBody());
if(repl.getFormType()==FormType.NATIVE) {
form.clearForms();
repl.bindValue("form", form); // user code will dynamically create inputs
} else if(repl.getFormType()==FormType.SIMPLE){
String scriptBody = getScriptBody();
Map<String, Input> inputs = Input.extractSimpleQueryParam(scriptBody); // inputs will be built from script body
form.setForms(inputs);
script = Input.getSimpleQuery(form.getParams(), scriptBody);
}
logger().info("RUN : "+script);
ReplResult ret = repl.interpret(script);
return ret;
}

Expand All @@ -128,4 +138,5 @@ private Logger logger(){
return logger;
}


}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public void cancel() {
@Override
public void bindValue(String name, Object o) {
}

@Override
public FormType getFormType() {
return FormType.SIMPLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Properties;

import com.nflabs.zeppelin.repl.Repl.FormType;

public class MockRepl2 extends Repl{

public MockRepl2(Properties property) {
Expand Down Expand Up @@ -33,4 +35,9 @@ public void cancel() {
@Override
public void bindValue(String name, Object o) {
}

@Override
public FormType getFormType() {
return FormType.SIMPLE;
}
}
13 changes: 8 additions & 5 deletions zeppelin-web/app/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,19 @@ function Paragraph(notebook, data){
if(data.form.forms){
for(var name in data.form.forms){
var form = data.form.forms[name];
var value = form.value;
var value = form.defaultValue;

if(data.form.params[name]){
value = data.form.params[name];
}

var html = "";
if(form.type=="INPUT"){

// for the defail, see Input.java

if(form.options && false){
// not implemented yet
} else {
formEl.append(name + ' : <input name="'+name+'" value="'+value+'"></input>');
formEl.children('[name="'+name+'"]').on('change', function(formName){
var name = formName;
Expand All @@ -260,8 +265,6 @@ function Paragraph(notebook, data){
paragraph.run();
}
}(name));
} else {
console.log("Unsupported form type %o", form);
}

}
Expand Down Expand Up @@ -468,7 +471,7 @@ function Table(config, columnNames, rows, listener){

this.target.children(".tableDisplay").empty();

if(this.config.mode==="line"){
if(this.config && this.config.mode==="line"){
this.target.children(".tableDisplay").append("<svg></svg>");

var xColIndex = 0;
Expand Down
5 changes: 5 additions & 0 deletions zeppelin-web/app/styles/notebook.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
.paragraph .control {

}

.paragraph .form {
margin-top : 20px;
}

.paragraph .editor {
width : 100%;
height: 20px;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.nflabs.zeppelin.server.form;
package com.nflabs.zeppelin.notebook.form;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Form {
Map<String, Object> params = new HashMap<String, Object>(); // actual values from client
Map<String, Base> forms = new TreeMap<String, Base>();

public static enum Type {
INPUT
}
Map<String, Input> forms = new TreeMap<String, Input>();


public Form(){

}
Expand All @@ -24,14 +21,24 @@ public Map<String, Object> getParams(){
return params;
}



public Map<String, Input> getForms() {
return forms;
}

public void setForms(Map<String, Input> forms) {
this.forms = forms;
}

public Object input(String id, Object defaultValue) {
// first find values from client and then use defualt
Object value = params.get(id);
if(value == null) {
value = defaultValue;
}

forms.put(id, new Input(value));
forms.put(id, new Input(id, defaultValue));
return value;
}

Expand All @@ -40,6 +47,6 @@ public Object input(String id) {
}

public void clearForms(){
this.forms = new TreeMap<String, Base>();
this.forms = new TreeMap<String, Input>();
}
}
Loading

0 comments on commit 774138b

Please sign in to comment.