Search in sources :

Example 1 with ParagraphResult

use of org.apache.zeppelin.client.ParagraphResult in project zeppelin by apache.

the class ZeppelinClientExample method main.

public static void main(String[] args) throws Exception {
    ClientConfig clientConfig = new ClientConfig("http://localhost:8080");
    ZeppelinClient zClient = new ZeppelinClient(clientConfig);
    String zeppelinVersion = zClient.getVersion();
    System.out.println("Zeppelin version: " + zeppelinVersion);
    String notePath = "/zeppelin_client_examples/note_1";
    String noteId = null;
    try {
        noteId = zClient.createNote(notePath);
        System.out.println("Created note: " + noteId);
        String newNotePath = notePath + "_rename";
        zClient.renameNote(noteId, newNotePath);
        NoteResult renamedNoteResult = zClient.queryNoteResult(noteId);
        System.out.println("Rename note: " + noteId + " name to " + renamedNoteResult.getNotePath());
        String paragraphId = zClient.addParagraph(noteId, "the first paragraph", "%python print('hello world')");
        ParagraphResult paragraphResult = zClient.executeParagraph(noteId, paragraphId);
        System.out.println("Added new paragraph and execute it.");
        System.out.println("Paragraph result: " + paragraphResult);
        String paragraphId2 = zClient.addParagraph(noteId, "the second paragraph", "%python\nimport time\ntime.sleep(5)\nprint('done')");
        zClient.submitParagraph(noteId, paragraphId2);
        zClient.waitUtilParagraphRunning(noteId, paragraphId2);
        zClient.cancelParagraph(noteId, paragraphId2);
        paragraphResult = zClient.waitUtilParagraphFinish(noteId, paragraphId2);
        System.out.println("Added new paragraph, submit it then cancel it");
        System.out.println("Paragraph result: " + paragraphResult);
        NoteResult noteResult = zClient.executeNote(noteId);
        System.out.println("Execute note and the note result: " + noteResult);
        zClient.submitNote(noteId);
        noteResult = zClient.waitUntilNoteFinished(noteId);
        System.out.println("Submit note and the note result: " + noteResult);
    } finally {
        if (noteId != null) {
            zClient.deleteNote(noteId);
            System.out.println("Note " + noteId + " is deleted");
        }
    }
}
Also used : NoteResult(org.apache.zeppelin.client.NoteResult) ZeppelinClient(org.apache.zeppelin.client.ZeppelinClient) ClientConfig(org.apache.zeppelin.client.ClientConfig) ParagraphResult(org.apache.zeppelin.client.ParagraphResult)

Example 2 with ParagraphResult

use of org.apache.zeppelin.client.ParagraphResult in project zeppelin by apache.

the class ZeppelinClientExample2 method main.

public static void main(String[] args) throws Exception {
    ClientConfig clientConfig = new ClientConfig("http://localhost:8080");
    ZeppelinClient zClient = new ZeppelinClient(clientConfig);
    String zeppelinVersion = zClient.getVersion();
    System.out.println("Zeppelin version: " + zeppelinVersion);
    // execute note 2A94M5J1Z paragraph by paragraph
    try {
        ParagraphResult paragraphResult = zClient.executeParagraph("2A94M5J1Z", "20150210-015259_1403135953");
        System.out.println("Execute the 1st spark tutorial paragraph, paragraph result: " + paragraphResult);
        paragraphResult = zClient.executeParagraph("2A94M5J1Z", "20150210-015302_1492795503");
        System.out.println("Execute the 2nd spark tutorial paragraph, paragraph result: " + paragraphResult);
        Map<String, String> parameters = new HashMap<>();
        parameters.put("maxAge", "40");
        paragraphResult = zClient.executeParagraph("2A94M5J1Z", "20150212-145404_867439529", parameters);
        System.out.println("Execute the 3rd spark tutorial paragraph, paragraph result: " + paragraphResult);
        parameters = new HashMap<>();
        parameters.put("marital", "married");
        paragraphResult = zClient.executeParagraph("2A94M5J1Z", "20150213-230422_1600658137", parameters);
        System.out.println("Execute the 4th spark tutorial paragraph, paragraph result: " + paragraphResult);
    } finally {
        // you need to stop interpreter explicitly if you are running paragraph separately.
        zClient.stopInterpreter("2A94M5J1Z", "spark");
    }
    // execute this whole note, this note will run under a didicated interpreter process which will be
    // stopped after note execution.
    Map<String, String> parameters = new HashMap<>();
    parameters.put("maxAge", "40");
    parameters.put("marital", "married");
    NoteResult noteResult = zClient.executeNote("2A94M5J1Z", parameters);
    System.out.println("Execute the spark tutorial note, note result: " + noteResult);
}
Also used : NoteResult(org.apache.zeppelin.client.NoteResult) HashMap(java.util.HashMap) ZeppelinClient(org.apache.zeppelin.client.ZeppelinClient) ClientConfig(org.apache.zeppelin.client.ClientConfig) ParagraphResult(org.apache.zeppelin.client.ParagraphResult)

Example 3 with ParagraphResult

use of org.apache.zeppelin.client.ParagraphResult in project zeppelin by apache.

the class ZeppelinClientIntegrationTest method testSubmitNote.

@Test
public void testSubmitNote() throws Exception {
    try {
        zeppelinClient.submitNote("unknown_id");
        fail("Should fail to submit non-existed note");
    } catch (Exception e) {
        assertTrue(e.getMessage(), e.getMessage().contains("No such note"));
    }
    String noteId = zeppelinClient.createNote("/test/note_4");
    String p0Id = zeppelinClient.addParagraph(noteId, "run sh", "%sh echo 'hello world'");
    zeppelinClient.submitNote(noteId);
    NoteResult noteResult = zeppelinClient.waitUntilNoteFinished(noteId);
    assertEquals(noteId, noteResult.getNoteId());
    assertEquals(false, noteResult.isRunning());
    assertEquals(1, noteResult.getParagraphResultList().size());
    ParagraphResult p0 = noteResult.getParagraphResultList().get(0);
    assertEquals(Status.FINISHED, p0.getStatus());
    assertEquals(1, p0.getResults().size());
    assertEquals("TEXT", p0.getResults().get(0).getType());
    assertEquals("hello world\n", p0.getResults().get(0).getData());
    // update paragraph with dynamic forms
    zeppelinClient.updateParagraph(noteId, p0Id, "run sh", "%sh(form=simple) sleep 5\necho 'hello ${name=abc}'");
    noteResult = zeppelinClient.submitNote(noteId);
    assertEquals(true, noteResult.isRunning());
    noteResult = zeppelinClient.waitUntilNoteFinished(noteId);
    assertEquals(noteId, noteResult.getNoteId());
    assertEquals(false, noteResult.isRunning());
    assertEquals(1, noteResult.getParagraphResultList().size());
    p0 = noteResult.getParagraphResultList().get(0);
    assertEquals(Status.FINISHED, p0.getStatus());
    assertEquals(1, p0.getResults().size());
    assertEquals("TEXT", p0.getResults().get(0).getType());
    assertEquals("hello abc\n", p0.getResults().get(0).getData());
    // execute paragraph with parameters
    Map<String, String> parameters = new HashMap<>();
    parameters.put("name", "zeppelin");
    noteResult = zeppelinClient.executeNote(noteId, parameters);
    assertEquals(noteId, noteResult.getNoteId());
    assertEquals(false, noteResult.isRunning());
    assertEquals(1, noteResult.getParagraphResultList().size());
    p0 = noteResult.getParagraphResultList().get(0);
    assertEquals(Status.FINISHED, p0.getStatus());
    assertEquals(1, p0.getResults().size());
    assertEquals("TEXT", p0.getResults().get(0).getType());
    assertEquals("hello zeppelin\n", p0.getResults().get(0).getData());
    zeppelinClient.addParagraph(noteId, "run sh", "%sh invalid_command");
    zeppelinClient.addParagraph(noteId, "run sh", "%sh pwd");
    zeppelinClient.submitNote(noteId, parameters);
    noteResult = zeppelinClient.waitUntilNoteFinished(noteId);
    assertEquals(noteId, noteResult.getNoteId());
    assertEquals(false, noteResult.isRunning());
    assertEquals(3, noteResult.getParagraphResultList().size());
    p0 = noteResult.getParagraphResultList().get(0);
    assertEquals(Status.FINISHED, p0.getStatus());
    assertEquals(1, p0.getResults().size());
    assertEquals("TEXT", p0.getResults().get(0).getType());
    assertEquals("hello zeppelin\n", p0.getResults().get(0).getData());
    ParagraphResult p1 = noteResult.getParagraphResultList().get(1);
    assertEquals(Status.ERROR, p1.getStatus());
    assertEquals("TEXT", p1.getResults().get(0).getType());
    assertTrue(p1.getResults().get(0).getData(), p1.getResults().get(0).getData().contains("command not found"));
    assertEquals("TEXT", p1.getResults().get(1).getType());
    assertTrue(p1.getResults().get(1).getData(), p1.getResults().get(1).getData().contains("ExitValue"));
    // p2 will be skipped because p1 fails.
    ParagraphResult p2 = noteResult.getParagraphResultList().get(2);
    assertEquals(Status.READY, p2.getStatus());
    assertEquals(0, p2.getResults().size());
}
Also used : NoteResult(org.apache.zeppelin.client.NoteResult) HashMap(java.util.HashMap) ParagraphResult(org.apache.zeppelin.client.ParagraphResult) Test(org.junit.Test)

Example 4 with ParagraphResult

use of org.apache.zeppelin.client.ParagraphResult in project zeppelin by apache.

the class ZeppelinClientIntegrationTest method testSubmitParagraph.

@Test
public void testSubmitParagraph() throws Exception {
    String noteId = zeppelinClient.createNote("/test/note_2");
    String paragraphId = zeppelinClient.addParagraph(noteId, "run sh", "%sh echo 'hello world'");
    zeppelinClient.submitParagraph(noteId, paragraphId);
    ParagraphResult paragraphResult = zeppelinClient.waitUtilParagraphFinish(noteId, paragraphId, 10 * 1000);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(Status.FINISHED, paragraphResult.getStatus());
    assertEquals(1, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertEquals("hello world\n", paragraphResult.getResults().get(0).getData());
    // submit paragraph succeed with dynamic forms
    paragraphId = zeppelinClient.addParagraph(noteId, "run sh", "%sh(form=simple) echo 'hello ${name=abc}'");
    zeppelinClient.submitParagraph(noteId, paragraphId);
    paragraphResult = zeppelinClient.waitUtilParagraphFinish(noteId, paragraphId, 10 * 1000);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(paragraphResult.toString(), Status.FINISHED, paragraphResult.getStatus());
    assertEquals(1, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertEquals("hello abc\n", paragraphResult.getResults().get(0).getData());
    // run paragraph succeed with parameters
    Map<String, String> parameters = new HashMap<>();
    parameters.put("name", "zeppelin");
    zeppelinClient.submitParagraph(noteId, paragraphId, parameters);
    paragraphResult = zeppelinClient.waitUtilParagraphFinish(noteId, paragraphId, 10 * 1000);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(Status.FINISHED, paragraphResult.getStatus());
    assertEquals(1, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertEquals("hello zeppelin\n", paragraphResult.getResults().get(0).getData());
    // run paragraph failed
    paragraphId = zeppelinClient.addParagraph(noteId, "run sh", "%sh invalid_command");
    zeppelinClient.submitParagraph(noteId, paragraphId);
    paragraphResult = zeppelinClient.waitUtilParagraphFinish(noteId, paragraphId, 10 * 1000);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(Status.ERROR, paragraphResult.getStatus());
    assertEquals(2, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertTrue(paragraphResult.getResults().get(0).getData(), paragraphResult.getResults().get(0).getData().contains("command not found"));
    assertEquals("TEXT", paragraphResult.getResults().get(1).getType());
    assertTrue(paragraphResult.getResults().get(1).getData(), paragraphResult.getResults().get(1).getData().contains("ExitValue"));
    // run non-existed interpreter
    paragraphId = zeppelinClient.addParagraph(noteId, "run sh", "%non_existed hello");
    zeppelinClient.submitParagraph(noteId, paragraphId);
    paragraphResult = zeppelinClient.waitUtilParagraphFinish(noteId, paragraphId, 10 * 1000);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(Status.ERROR, paragraphResult.getStatus());
    assertEquals(1, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertTrue(paragraphResult.getResults().get(0).getData(), paragraphResult.getResults().get(0).getData().contains("Interpreter non_existed not found"));
    // run non-existed paragraph
    try {
        zeppelinClient.submitParagraph(noteId, "invalid_paragraph_id");
        fail("Should fail to run non-existed paragraph");
    } catch (Exception e) {
        assertTrue(e.getMessage(), e.getMessage().contains("No such paragraph"));
    }
}
Also used : HashMap(java.util.HashMap) ParagraphResult(org.apache.zeppelin.client.ParagraphResult) Test(org.junit.Test)

Example 5 with ParagraphResult

use of org.apache.zeppelin.client.ParagraphResult in project zeppelin by apache.

the class ZeppelinClientIntegrationTest method testExecuteParagraph.

@Test
public void testExecuteParagraph() throws Exception {
    // run paragraph succeed
    String noteId = zeppelinClient.createNote("/test/note_1");
    String paragraphId = zeppelinClient.addParagraph(noteId, "run sh", "%sh echo 'hello world'");
    ParagraphResult paragraphResult = zeppelinClient.executeParagraph(noteId, paragraphId);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(Status.FINISHED, paragraphResult.getStatus());
    assertEquals(1, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertEquals("hello world\n", paragraphResult.getResults().get(0).getData());
    // run paragraph succeed with dynamic forms
    paragraphId = zeppelinClient.addParagraph(noteId, "run sh", "%sh(form=simple) echo 'hello ${name=abc}'");
    paragraphResult = zeppelinClient.executeParagraph(noteId, paragraphId);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(paragraphResult.toString(), Status.FINISHED, paragraphResult.getStatus());
    assertEquals(1, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertEquals("hello abc\n", paragraphResult.getResults().get(0).getData());
    // run paragraph succeed with parameters
    Map<String, String> parameters = new HashMap<>();
    parameters.put("name", "zeppelin");
    paragraphResult = zeppelinClient.executeParagraph(noteId, paragraphId, parameters);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(Status.FINISHED, paragraphResult.getStatus());
    assertEquals(1, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertEquals("hello zeppelin\n", paragraphResult.getResults().get(0).getData());
    // run paragraph failed
    paragraphId = zeppelinClient.addParagraph(noteId, "run sh", "%sh invalid_command");
    paragraphResult = zeppelinClient.executeParagraph(noteId, paragraphId);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(Status.ERROR, paragraphResult.getStatus());
    assertEquals(2, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertTrue(paragraphResult.getResults().get(0).getData(), paragraphResult.getResults().get(0).getData().contains("command not found"));
    assertEquals("TEXT", paragraphResult.getResults().get(1).getType());
    assertTrue(paragraphResult.getResults().get(1).getData(), paragraphResult.getResults().get(1).getData().contains("ExitValue"));
    // run non-existed interpreter
    paragraphId = zeppelinClient.addParagraph(noteId, "run sh", "%non_existed hello");
    paragraphResult = zeppelinClient.executeParagraph(noteId, paragraphId);
    assertEquals(paragraphId, paragraphResult.getParagraphId());
    assertEquals(Status.ERROR, paragraphResult.getStatus());
    assertEquals(1, paragraphResult.getResults().size());
    assertEquals("TEXT", paragraphResult.getResults().get(0).getType());
    assertTrue(paragraphResult.getResults().get(0).getData(), paragraphResult.getResults().get(0).getData().contains("Interpreter non_existed not found"));
    // run non-existed paragraph
    try {
        zeppelinClient.executeParagraph(noteId, "invalid_paragraph_id");
        fail("Should fail to run non-existed paragraph");
    } catch (Exception e) {
        assertTrue(e.getMessage(), e.getMessage().contains("No such paragraph"));
    }
}
Also used : HashMap(java.util.HashMap) ParagraphResult(org.apache.zeppelin.client.ParagraphResult) Test(org.junit.Test)

Aggregations

ParagraphResult (org.apache.zeppelin.client.ParagraphResult)6 HashMap (java.util.HashMap)5 NoteResult (org.apache.zeppelin.client.NoteResult)4 Test (org.junit.Test)4 ClientConfig (org.apache.zeppelin.client.ClientConfig)2 ZeppelinClient (org.apache.zeppelin.client.ZeppelinClient)2