Search in sources :

Example 6 with SimpleMessageHandler

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

the class RExample 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("r").setIntpProperties(intpProperties).build();
        session.start(new SimpleMessageHandler());
        // single statement
        ExecuteResult result = session.execute("bare <- c(1, 2.5, 4)\n" + "print(bare)");
        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());
        // R plotting
        result = session.execute("ir", "pairs(iris)");
        System.out.println("R plotting result, type: " + result.getResults().get(0).getType() + ", data: " + result.getResults().get(0).getData());
        // ggplot2
        result = session.execute("ir", "library(ggplot2)\n" + "ggplot(mpg, aes(displ, hwy, colour = class)) + \n" + "  geom_point()");
        System.out.println("ggplot2 plotting result, type: " + result.getResults().get(0).getType() + ", data: " + result.getResults().get(0).getData());
        // googlevis
        result = session.execute("ir", "library(googleVis)\n" + "df=data.frame(country=c(\"US\", \"GB\", \"BR\"), \n" + "              val1=c(10,13,14), \n" + "              val2=c(23,12,32))\n" + "Bar <- gvisBarChart(df)\n" + "print(Bar, tag = 'chart')");
        System.out.println("googlevis plotting 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 7 with SimpleMessageHandler

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

the class FlinkAdvancedExample 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();
        // if MessageHandler is specified, then websocket is enabled.
        // you can get continuous output from Zeppelin via websocket.
        session.start(new SimpleMessageHandler());
        System.out.println("Flink Web UI: " + session.getWeburl());
        String code = "benv.fromElements(1,2,3,4,5,6,7,8,9,10).map(e=> {Thread.sleep(1000); e}).print()";
        System.out.println("Submit code: " + code);
        // use submit to run flink 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("benv.fromElements(1,2,3,4,5,6,7,8,9,10).map(e=> {Thread.sleep(1000); e}).print()");
        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("-----------------------------------------------------------------------------");
        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());
        System.out.println("-----------------------------------------------------------------------------");
        String initCode = IOUtils.toString(FlinkAdvancedExample.class.getResource("/init_stream.scala"));
        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");
        session.waitUntilFinished(result.getStatementId());
        result = session.submit("ssql", localProperties, "select url, count(1) as pv from log group by url");
        session.waitUntilRunning(result.getStatementId());
        Thread.sleep(10 * 1000);
        System.out.println("Try to cancel statement: " + result.getStatementId());
        session.cancel(result.getStatementId());
        session.waitUntilFinished(result.getStatementId());
        System.out.println("Job status: " + result.getStatus());
    } 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 8 with SimpleMessageHandler

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

the class ZSessionIntegrationTest method testZSessionCleanup.

@Test
public void testZSessionCleanup() 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());
        assertTrue(notebook.getNotesInfo().size() > 0);
        Thread.sleep(30 * 1000);
        assertEquals(0, notebook.getNotesInfo().size());
        try {
            session.execute("1/0");
            fail("Should fail to execute code after session is stopped");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } finally {
        try {
            session.stop();
            fail("Should fail to stop session after it is stopped");
        } catch (Exception e) {
            e.printStackTrace();
            assertTrue(e.getMessage().contains("No such session"));
        }
    }
}
Also used : HashMap(java.util.HashMap) ZSession(org.apache.zeppelin.client.ZSession) SimpleMessageHandler(org.apache.zeppelin.client.websocket.SimpleMessageHandler) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with SimpleMessageHandler

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

the class ZSessionIntegrationTest method testZSession_Flink_Submit.

@Test
public void testZSession_Flink_Submit() throws Exception {
    Map<String, String> intpProperties = new HashMap<>();
    intpProperties.put("FLINK_HOME", flinkHome);
    ZSession session = ZSession.builder().setClientConfig(clientConfig).setInterpreter("flink").setIntpProperties(intpProperties).build();
    try {
        session.start(new SimpleMessageHandler());
        assertNotNull(session.getWeburl());
        assertNotNull(session.getNoteId());
        // scala
        ExecuteResult result = session.submit("val data = benv.fromElements(1, 2, 3)\ndata.collect()");
        result = session.waitUntilFinished(result.getStatementId());
        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("1, 2, 3"));
        // sql
        result = session.submit(getInitStreamScript(200));
        result = session.waitUntilFinished(result.getStatementId());
        assertEquals(result.toString(), Status.FINISHED, result.getStatus());
        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");
        assertFalse("Status is: " + result.getStatus().toString(), result.getStatus().isCompleted());
        result = session.waitUntilFinished(result.getStatementId());
        assertEquals(result.toString(), Status.FINISHED, result.getStatus());
        // cancel
        result = session.submit("ssql", localProperties, "select url, count(1) as pv from log group by url");
        assertFalse("Status is: " + result.getStatus().toString(), result.getStatus().isCompleted());
        result = session.waitUntilRunning(result.getStatementId());
        session.cancel(result.getStatementId());
        assertEquals(result.toString(), Status.RUNNING, result.getStatus());
        result = session.waitUntilFinished(result.getStatementId());
        assertEquals(result.toString(), Status.ABORT, result.getStatus());
    } 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