use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class NotebookServer method broadcastSpellExecution.
private void broadcastSpellExecution(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws IOException {
final String paragraphId = (String) fromMessage.get("id");
if (paragraphId == null) {
return;
}
String noteId = getOpenNoteId(conn);
if (!hasParagraphWriterPermission(conn, notebook, noteId, userAndRoles, fromMessage.principal, "write")) {
return;
}
String text = (String) fromMessage.get("paragraph");
String title = (String) fromMessage.get("title");
Status status = Status.valueOf((String) fromMessage.get("status"));
Map<String, Object> params = (Map<String, Object>) fromMessage.get("params");
Map<String, Object> config = (Map<String, Object>) fromMessage.get("config");
final Note note = notebook.getNote(noteId);
Paragraph p = setParagraphUsingMessage(note, fromMessage, paragraphId, text, title, params, config);
p.setResult(fromMessage.get("results"));
p.setErrorMessage((String) fromMessage.get("errorMessage"));
p.setStatusWithoutNotification(status);
addNewParagraphIfLastParagraphIsExecuted(note, p);
if (!persistNoteWithAuthInfo(conn, note, p)) {
return;
}
// broadcast to other clients only
broadcastExcept(note.getId(), new Message(OP.RUN_PARAGRAPH_USING_SPELL).put("paragraph", p), conn);
}
use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class NotebookServer method onGetParagraphRunners.
@Override
public void onGetParagraphRunners(String noteId, String paragraphId, RemoteWorksEventListener callback) {
Notebook notebookIns = notebook();
List<InterpreterContextRunner> runner = new LinkedList<>();
if (notebookIns == null) {
LOG.info("intepreter request notebook instance is null");
callback.onFinished(notebookIns);
}
try {
Note note = notebookIns.getNote(noteId);
if (note != null) {
if (paragraphId != null) {
Paragraph paragraph = note.getParagraph(paragraphId);
if (paragraph != null) {
runner.add(paragraph.getInterpreterContextRunner());
}
} else {
for (Paragraph p : note.getParagraphs()) {
runner.add(p.getInterpreterContextRunner());
}
}
}
callback.onFinished(runner);
} catch (NullPointerException e) {
LOG.warn(e.getMessage());
callback.onError();
}
}
use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class ZeppelinRestApiTest method testRunParagraphWithParams.
@Test
public void testRunParagraphWithParams() throws IOException, InterruptedException {
LOG.info("testRunParagraphWithParams");
// Create note to run test.
Note note = ZeppelinServer.notebook.createNote(anonymous);
assertNotNull("can't create new note", note);
note.setName("note for run test");
Paragraph paragraph = note.addParagraph(AuthenticationInfo.ANONYMOUS);
Map config = paragraph.getConfig();
config.put("enabled", true);
paragraph.setConfig(config);
paragraph.setText("%spark\nval param = z.input(\"param\").toString\nprintln(param)");
note.persist(anonymous);
String noteId = note.getId();
note.runAll();
// wait until job is finished or timeout.
int timeout = 1;
while (!paragraph.isTerminated()) {
Thread.sleep(1000);
if (timeout++ > 120) {
LOG.info("testRunParagraphWithParams timeout job.");
break;
}
}
// Call Run paragraph REST API
PostMethod postParagraph = httpPost("/notebook/job/" + noteId + "/" + paragraph.getId(), "{\"params\": {\"param\": \"hello\", \"param2\": \"world\"}}");
assertThat("test paragraph run:", postParagraph, isAllowed());
postParagraph.releaseConnection();
Thread.sleep(1000);
Note retrNote = ZeppelinServer.notebook.getNote(noteId);
Paragraph retrParagraph = retrNote.getParagraph(paragraph.getId());
Map<String, Object> params = retrParagraph.settings.getParams();
assertEquals("hello", params.get("param"));
assertEquals("world", params.get("param2"));
//cleanup
ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class ZeppelinRestApiTest method testexportNote.
@Test
public void testexportNote() throws IOException {
LOG.info("testexportNote");
Note note = ZeppelinServer.notebook.createNote(anonymous);
assertNotNull("can't create new note", note);
note.setName("source note for export");
Paragraph paragraph = note.addParagraph(AuthenticationInfo.ANONYMOUS);
Map config = paragraph.getConfig();
config.put("enabled", true);
paragraph.setConfig(config);
paragraph.setText("%md This is my new paragraph in my new note");
note.persist(anonymous);
String sourceNoteId = note.getId();
// Call export Note REST API
GetMethod get = httpGet("/notebook/export/" + sourceNoteId);
LOG.info("testNoteExport \n" + get.getResponseBodyAsString());
assertThat("test note export method:", get, isAllowed());
Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
}.getType());
String exportJSON = (String) resp.get("body");
assertNotNull("Can not find new notejson", exportJSON);
LOG.info("export JSON:=" + exportJSON);
ZeppelinServer.notebook.removeNote(sourceNoteId, anonymous);
get.releaseConnection();
}
use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class ZeppelinRestApiTest method testGetNoteInfo.
@Test
public void testGetNoteInfo() throws IOException {
LOG.info("testGetNoteInfo");
// Create note to get info
Note note = ZeppelinServer.notebook.createNote(anonymous);
assertNotNull("can't create new note", note);
note.setName("note");
Paragraph paragraph = note.addParagraph(AuthenticationInfo.ANONYMOUS);
Map config = paragraph.getConfig();
config.put("enabled", true);
paragraph.setConfig(config);
String paragraphText = "%md This is my new paragraph in my new note";
paragraph.setText(paragraphText);
note.persist(anonymous);
String sourceNoteId = note.getId();
GetMethod get = httpGet("/notebook/" + sourceNoteId);
LOG.info("testGetNoteInfo \n" + get.getResponseBodyAsString());
assertThat("test note get method:", get, isAllowed());
Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
}.getType());
assertNotNull(resp);
assertEquals("OK", resp.get("status"));
Map<String, Object> body = (Map<String, Object>) resp.get("body");
List<Map<String, Object>> paragraphs = (List<Map<String, Object>>) body.get("paragraphs");
assertTrue(paragraphs.size() > 0);
assertEquals(paragraphText, paragraphs.get(0).get("text"));
//
ZeppelinServer.notebook.removeNote(sourceNoteId, anonymous);
}
Aggregations