Search in sources :

Example 1 with SimpleMessageHandler

use of org.apache.zeppelin.client.websocket.SimpleMessageHandler 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 2 with SimpleMessageHandler

use of org.apache.zeppelin.client.websocket.SimpleMessageHandler 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 3 with SimpleMessageHandler

use of org.apache.zeppelin.client.websocket.SimpleMessageHandler 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 4 with SimpleMessageHandler

use of org.apache.zeppelin.client.websocket.SimpleMessageHandler in project zeppelin by apache.

the class PrestoExample 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("presto").setIntpProperties(intpProperties).build();
        session.start(new SimpleMessageHandler());
        // single sql
        ExecuteResult result = session.execute("show schemas");
        System.out.println("show schemas 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
        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 5 with SimpleMessageHandler

use of org.apache.zeppelin.client.websocket.SimpleMessageHandler in project zeppelin by apache.

the class ZSessionIntegrationTest method testZSession_Python.

@Test
public void testZSession_Python() throws Exception {
    Map<String, String> intpProperties = new HashMap<>();
    intpProperties.put("zeppelin.python.gatewayserver_address", "127.0.0.1");
    ZSession session = ZSession.builder().setClientConfig(clientConfig).setInterpreter("python").setIntpProperties(intpProperties).build();
    try {
        session.start(new SimpleMessageHandler());
        assertNull(session.getWeburl());
        assertNotNull(session.getNoteId());
        // python
        // ExecuteResult result = session.execute("1+1");
        // assertEquals(result.toString(), Status.FINISHED, result.getStatus());
        // assertEquals(1, result.getResults().size());
        // assertEquals("TEXT", result.getResults().get(0).getType());
        // assertTrue(result.getResults().get(0).getData(), result.getResults().get(0).getData().contains("2"));
        // 
        // // python
        // result = session.execute("1/0");
        // assertEquals(result.toString(), Status.ERROR, result.getStatus());
        // assertEquals(1, result.getResults().size());
        // assertEquals("TEXT", result.getResults().get(0).getType());
        // assertTrue(result.getResults().get(0).getData(), result.getResults().get(0).getData().contains("ZeroDivisionError"));
        // for loop
        ExecuteResult result = session.execute("import time\n" + "for i in range(1,10):\n" + "\tprint(i)\n" + "\ttime.sleep(1)");
        assertEquals(result.toString(), Status.FINISHED, result.getStatus());
        assertEquals(1, result.getResults().size());
        assertEquals("TEXT", result.getResults().get(0).getType());
        Map<String, String> localProperties = new HashMap<>();
        // contains whitespace
        localProperties.put("key 1", "hello world");
        // contains comma
        localProperties.put("key,2", "a,b");
        result = session.execute("1+1", localProperties);
        assertEquals(result.toString(), Status.FINISHED, result.getStatus());
        assertEquals(1, result.getResults().size());
        assertEquals("TEXT", result.getResults().get(0).getType());
    } finally {
        session.stop();
    }
}
Also used : HashMap(java.util.HashMap) ExecuteResult(org.apache.zeppelin.client.ExecuteResult) ZSession(org.apache.zeppelin.client.ZSession) SimpleMessageHandler(org.apache.zeppelin.client.websocket.SimpleMessageHandler) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)9 ZSession (org.apache.zeppelin.client.ZSession)9 SimpleMessageHandler (org.apache.zeppelin.client.websocket.SimpleMessageHandler)9 ExecuteResult (org.apache.zeppelin.client.ExecuteResult)8 ClientConfig (org.apache.zeppelin.client.ClientConfig)6 Test (org.junit.Test)3 IOException (java.io.IOException)1