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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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");
}
}
}
Aggregations