Search in sources :

Example 1 with ClientConfig

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

the class FlinkAdvancedExample2 method main.

public static void main(String[] args) {
    ZSession session = null;
    try {
        ClientConfig clientConfig = new ClientConfig("http://localhost:8080");
        Map<String, String> intpProperties = new HashMap<>();
        session = ZSession.builder().setClientConfig(clientConfig).setInterpreter("flink").setIntpProperties(intpProperties).build();
        // CompositeMessageHandler allow you to add StatementMessageHandler for each statement.
        // otherwise you have to use a global MessageHandler.
        session.start(new CompositeMessageHandler());
        System.out.println("Flink Web UI: " + session.getWeburl());
        System.out.println("-----------------------------------------------------------------------------");
        String initCode = IOUtils.toString(FlinkAdvancedExample.class.getResource("/init_stream.scala"));
        ExecuteResult result = session.execute(initCode);
        System.out.println("Job status: " + result.getStatus() + ", data: " + result.getResults().get(0).getData());
        // run flink ssql
        Map<String, String> localProperties = new HashMap<>();
        localProperties.put("type", "update");
        result = session.submit("ssql", localProperties, "select url, count(1) as pv from log group by url", new MyStatementMessageHandler1());
        session.waitUntilFinished(result.getStatementId());
        result = session.submit("ssql", localProperties, "select upper(url), count(1) as pv from log group by url", new MyStatementMessageHandler2());
        session.waitUntilFinished(result.getStatementId());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            try {
                session.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : CompositeMessageHandler(org.apache.zeppelin.client.websocket.CompositeMessageHandler) HashMap(java.util.HashMap) ClientConfig(org.apache.zeppelin.client.ClientConfig) ExecuteResult(org.apache.zeppelin.client.ExecuteResult) ZSession(org.apache.zeppelin.client.ZSession)

Example 2 with ClientConfig

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

the class HiveExample method main.

public static void main(String[] args) {
    ZSession session = null;
    try {
        ClientConfig clientConfig = new ClientConfig("http://localhost:8080");
        Map<String, String> intpProperties = new HashMap<>();
        session = ZSession.builder().setClientConfig(clientConfig).setInterpreter("hive").setIntpProperties(intpProperties).build();
        session.start(new SimpleMessageHandler());
        // single sql
        ExecuteResult result = session.execute("show databases");
        System.out.println("show database result : " + result.getResults().get(0).getData());
        // multiple sql
        result = session.execute("use tpch_text_5;\nshow tables");
        System.out.println("show tables result: " + result.getResults().get(0).getData());
        // select, you can see the hive sql job progress via SimpleMessageHandler
        result = session.execute("select count(1) from lineitem");
        System.out.println("Result status: " + result.getStatus() + ", data: " + result.getResults().get(0).getData());
        // invalid sql
        result = session.execute("select * from unknown_table");
        System.out.println("Result status: " + result.getStatus() + ", data: " + result.getResults().get(0).getData());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            try {
                session.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ClientConfig(org.apache.zeppelin.client.ClientConfig) ExecuteResult(org.apache.zeppelin.client.ExecuteResult) ZSession(org.apache.zeppelin.client.ZSession) SimpleMessageHandler(org.apache.zeppelin.client.websocket.SimpleMessageHandler)

Example 3 with ClientConfig

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

the class PythonExample method main.

public static void main(String[] args) {
    ZSession session = null;
    try {
        ClientConfig clientConfig = new ClientConfig("http://localhost:8080");
        Map<String, String> intpProperties = new HashMap<>();
        session = ZSession.builder().setClientConfig(clientConfig).setInterpreter("python").setIntpProperties(intpProperties).build();
        session.start(new SimpleMessageHandler());
        // single statement
        ExecuteResult result = session.execute("print('hello world')");
        System.out.println(result.getResults().get(0).getData());
        // multiple statement
        result = session.execute("print('hello world')\nprint('hello world2')");
        System.out.println(result.getResults().get(0).getData());
        // error output
        result = session.execute("1/0");
        System.out.println("Result status: " + result.getStatus() + ", data: " + result.getResults().get(0).getData());
        // matplotlib
        result = session.execute("ipython", "%matplotlib inline\n" + "import matplotlib.pyplot as plt\n" + "plt.plot([1,2,3,4])\n" + "plt.ylabel('some numbers')\n" + "plt.show()");
        System.out.println("Matplotlib result, type: " + result.getResults().get(0).getType() + ", data: " + result.getResults().get(0).getData());
        // show pandas dataframe
        result = session.execute("ipython", "import pandas as pd\n" + "df = pd.DataFrame({'name':['a','b','c'], 'count':[12,24,18]})\n" + "z.show(df)");
        System.out.println("Pandas dataframe result, type: " + result.getResults().get(0).getType() + ", data: " + result.getResults().get(0).getData());
        // streaming output
        result = session.execute("import time\n" + "for i in range(1,10):\n" + "    print(i)\n" + "    time.sleep(1)");
        System.out.println("Python streaming result, type: " + result.getResults().get(0).getType() + ", data: " + result.getResults().get(0).getData());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            try {
                session.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ClientConfig(org.apache.zeppelin.client.ClientConfig) ExecuteResult(org.apache.zeppelin.client.ExecuteResult) ZSession(org.apache.zeppelin.client.ZSession) SimpleMessageHandler(org.apache.zeppelin.client.websocket.SimpleMessageHandler)

Example 4 with ClientConfig

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

the class SparkAdvancedExample method main.

public static void main(String[] args) {
    ZSession session = null;
    try {
        ClientConfig clientConfig = new ClientConfig("http://localhost:8080");
        Map<String, String> intpProperties = new HashMap<>();
        intpProperties.put("spark.master", "local[*]");
        session = ZSession.builder().setClientConfig(clientConfig).setInterpreter("spark").setIntpProperties(intpProperties).build();
        // if MessageHandler is specified, then websocket is enabled.
        // you can get continuous output from Zeppelin via websocket.
        session.start(new SimpleMessageHandler());
        System.out.println("Spark Web UI: " + session.getWeburl());
        String code = "sc.range(1,10).map(e=> {Thread.sleep(2000); e}).sum()";
        System.out.println("Submit code: " + code);
        // use submit to run spark code in non-blocking way.
        ExecuteResult result = session.submit(code);
        System.out.println("Job status: " + result.getStatus());
        while (!result.getStatus().isCompleted()) {
            result = session.queryStatement(result.getStatementId());
            System.out.println("Job status: " + result.getStatus() + ", progress: " + result.getProgress());
            Thread.sleep(1000);
        }
        System.out.println("Job status: " + result.getStatus() + ", data: " + result.getResults().get(0).getData());
        System.out.println("-----------------------------------------------------------------------------");
        System.out.println("Submit code: " + code);
        result = session.submit("sc.range(1,10).map(e=> {Thread.sleep(2000); e}).sum()");
        System.out.println("Job status: " + result.getStatus());
        result = session.waitUntilFinished(result.getStatementId());
        System.out.println("Job status: " + result.getStatus() + ", data: " + result.getResults().get(0).getData());
        System.out.println("-----------------------------------------------------------------------------");
        System.out.println("Submit code: " + code);
        result = session.submit("sc.range(1,10).map(e=> {Thread.sleep(2000); e}).sum()");
        System.out.println("Job status: " + result.getStatus());
        session.waitUntilRunning(result.getStatementId());
        System.out.println("Try to cancel statement: " + result.getStatementId());
        session.cancel(result.getStatementId());
        result = session.waitUntilFinished(result.getStatementId());
        System.out.println("Job status: " + result.getStatus() + ", data: " + result.getResults().get(0).getData());
        System.out.println("-----------------------------------------------------------------------------");
        code = "for(i <- 1 to 10) {\n" + "   Thread.sleep(1000)\n" + "   println(i)\n" + "}";
        System.out.println("Submit code: " + code);
        result = session.execute(code);
        System.out.println("Job status: " + result.getStatus() + ", data: " + result.getResults().get(0).getData());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            try {
                session.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ClientConfig(org.apache.zeppelin.client.ClientConfig) ExecuteResult(org.apache.zeppelin.client.ExecuteResult) ZSession(org.apache.zeppelin.client.ZSession) SimpleMessageHandler(org.apache.zeppelin.client.websocket.SimpleMessageHandler)

Example 5 with ClientConfig

use of org.apache.zeppelin.client.ClientConfig 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)

Aggregations

ClientConfig (org.apache.zeppelin.client.ClientConfig)13 HashMap (java.util.HashMap)10 ExecuteResult (org.apache.zeppelin.client.ExecuteResult)9 ZSession (org.apache.zeppelin.client.ZSession)9 SimpleMessageHandler (org.apache.zeppelin.client.websocket.SimpleMessageHandler)6 ZeppelinClient (org.apache.zeppelin.client.ZeppelinClient)4 NoteResult (org.apache.zeppelin.client.NoteResult)2 ParagraphResult (org.apache.zeppelin.client.ParagraphResult)2 Notebook (org.apache.zeppelin.notebook.Notebook)2 BeforeClass (org.junit.BeforeClass)2 CompositeMessageHandler (org.apache.zeppelin.client.websocket.CompositeMessageHandler)1