use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class S3NotebookRepo method getNote.
private Note getNote(String key) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
S3Object s3object;
try {
s3object = s3client.getObject(new GetObjectRequest(bucketName, key));
} catch (AmazonClientException ace) {
throw new IOException("Unable to retrieve object from S3: " + ace, ace);
}
Note note;
try (InputStream ins = s3object.getObjectContent()) {
String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
note = gson.fromJson(json, Note.class);
}
for (Paragraph p : note.getParagraphs()) {
if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
p.setStatus(Status.ABORT);
}
}
return note;
}
use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class NotebookServerTest method bindAngularObjectToRemoteForParagraphs.
@Test
public void bindAngularObjectToRemoteForParagraphs() throws Exception {
//Given
final String varName = "name";
final String value = "DuyHai DOAN";
final Message messageReceived = new Message(OP.ANGULAR_OBJECT_CLIENT_BIND).put("noteId", "noteId").put("name", varName).put("value", value).put("paragraphId", "paragraphId");
final NotebookServer server = new NotebookServer();
final Notebook notebook = mock(Notebook.class);
final Note note = mock(Note.class, RETURNS_DEEP_STUBS);
when(notebook.getNote("noteId")).thenReturn(note);
final Paragraph paragraph = mock(Paragraph.class, RETURNS_DEEP_STUBS);
when(note.getParagraph("paragraphId")).thenReturn(paragraph);
final RemoteAngularObjectRegistry mdRegistry = mock(RemoteAngularObjectRegistry.class);
final InterpreterGroup mdGroup = new InterpreterGroup("mdGroup");
mdGroup.setAngularObjectRegistry(mdRegistry);
when(paragraph.getCurrentRepl().getInterpreterGroup()).thenReturn(mdGroup);
final AngularObject<String> ao1 = AngularObjectBuilder.build(varName, value, "noteId", "paragraphId");
when(mdRegistry.addAndNotifyRemoteProcess(varName, value, "noteId", "paragraphId")).thenReturn(ao1);
NotebookSocket conn = mock(NotebookSocket.class);
NotebookSocket otherConn = mock(NotebookSocket.class);
final String mdMsg1 = server.serializeMessage(new Message(OP.ANGULAR_OBJECT_UPDATE).put("angularObject", ao1).put("interpreterGroupId", "mdGroup").put("noteId", "noteId").put("paragraphId", "paragraphId"));
server.noteSocketMap.put("noteId", asList(conn, otherConn));
// When
server.angularObjectClientBind(conn, new HashSet<String>(), notebook, messageReceived);
// Then
verify(mdRegistry, never()).addAndNotifyRemoteProcess(varName, value, "noteId", null);
verify(otherConn).send(mdMsg1);
}
use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class ZeppelinRestApiTest method testInsertParagraph.
@Test
public void testInsertParagraph() throws IOException {
Note note = ZeppelinServer.notebook.createNote(anonymous);
String jsonRequest = "{\"title\": \"title1\", \"text\": \"text1\"}";
PostMethod post = httpPost("/notebook/" + note.getId() + "/paragraph", jsonRequest);
LOG.info("testInsertParagraph response\n" + post.getResponseBodyAsString());
assertThat("Test insert method:", post, isAllowed());
post.releaseConnection();
Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
}.getType());
String newParagraphId = (String) resp.get("body");
LOG.info("newParagraphId:=" + newParagraphId);
Note retrNote = ZeppelinServer.notebook.getNote(note.getId());
Paragraph newParagraph = retrNote.getParagraph(newParagraphId);
assertNotNull("Can not find new paragraph by id", newParagraph);
assertEquals("title1", newParagraph.getTitle());
assertEquals("text1", newParagraph.getText());
Paragraph lastParagraph = note.getLastParagraph();
assertEquals(newParagraph.getId(), lastParagraph.getId());
// insert to index 0
String jsonRequest2 = "{\"index\": 0, \"title\": \"title2\", \"text\": \"text2\"}";
PostMethod post2 = httpPost("/notebook/" + note.getId() + "/paragraph", jsonRequest2);
LOG.info("testInsertParagraph response2\n" + post2.getResponseBodyAsString());
assertThat("Test insert method:", post2, isAllowed());
post2.releaseConnection();
Paragraph paragraphAtIdx0 = note.getParagraphs().get(0);
assertEquals("title2", paragraphAtIdx0.getTitle());
assertEquals("text2", paragraphAtIdx0.getText());
ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class ZeppelinRestApiTest method testMoveParagraph.
@Test
public void testMoveParagraph() throws IOException {
Note note = ZeppelinServer.notebook.createNote(anonymous);
Paragraph p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
p.setTitle("title1");
p.setText("text1");
Paragraph p2 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
p2.setTitle("title2");
p2.setText("text2");
note.persist(anonymous);
PostMethod post = httpPost("/notebook/" + note.getId() + "/paragraph/" + p2.getId() + "/move/" + 0, "");
assertThat("Test post method: ", post, isAllowed());
post.releaseConnection();
Note retrNote = ZeppelinServer.notebook.getNote(note.getId());
Paragraph paragraphAtIdx0 = retrNote.getParagraphs().get(0);
assertEquals(p2.getId(), paragraphAtIdx0.getId());
assertEquals(p2.getTitle(), paragraphAtIdx0.getTitle());
assertEquals(p2.getText(), paragraphAtIdx0.getText());
PostMethod post2 = httpPost("/notebook/" + note.getId() + "/paragraph/" + p2.getId() + "/move/" + 10, "");
assertThat("Test post method: ", post2, isBadRequest());
post.releaseConnection();
ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.
the class ZeppelinSparkClusterTest method pySparkAutoConvertOptionTest.
@Test
public void pySparkAutoConvertOptionTest() throws IOException {
// create new note
Note note = ZeppelinServer.notebook.createNote(anonymous);
note.setName("note");
int sparkVersionNumber = getSparkVersionNumber(note);
if (isPyspark() && sparkVersionNumber >= 14) {
// auto_convert enabled from spark 1.4
// run markdown paragraph, again
Paragraph p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
Map config = p.getConfig();
config.put("enabled", true);
p.setConfig(config);
String sqlContextName = "sqlContext";
if (sparkVersionNumber >= 20) {
sqlContextName = "spark";
}
p.setText("%pyspark\nfrom pyspark.sql.functions import *\n" + "print(" + sqlContextName + ".range(0, 10).withColumn('uniform', rand(seed=10) * 3.14).count())");
p.setAuthenticationInfo(anonymous);
note.run(p.getId());
waitForFinish(p);
assertEquals(Status.FINISHED, p.getStatus());
assertEquals("10\n", p.getResult().message().get(0).getData());
}
ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
Aggregations