@Component
public class ZeppelinUtil {
public String createNote(Script record, String zeppelinUrl) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("name", record.getScriptName());
String url = zeppelinUrl + Constant.CREATE_NOTE;
Map<String, Object> headers = new HashMap<>();
JSONObject jsonObject = HttpUtil.httpPostUtil(params, headers, url);
checkResponse(jsonObject);
return jsonObject.getString("body");
}
public String createParagraph(Script record, String zeppelinUrl) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("title", record.getScriptName());
params.put("text", record.getScriptText());
String url = zeppelinUrl + (Constant.CREATE_PARAGRAPH).replace("${NOTE}", record.getNoteId());
Map<String, Object> headers = new HashMap<>();
JSONObject jsonObject = HttpUtil.httpPostUtil(params, headers, url);
checkResponse(jsonObject);
return jsonObject.getString("body");
}
public void updateParagraph(Script record, String zeppelinUrl) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("title", record.getScriptName());
params.put("text", record.getScriptText());
String url = zeppelinUrl + (Constant.UPDATER_PARAGRAPH).replace("${NOTE}", record.getNoteId()) + record.getParagraphId();
Map<String, Object> headers = new HashMap<>();
JSONObject jsonObject = HttpUtil.httpPutUtil(params, headers, url);
checkResponse(jsonObject);
}
public JSONObject execute(Script record, String zeppelinUrl) throws Exception {
Map<String, Object> params = new HashMap<>();
String url = zeppelinUrl + Constant.RUN_PARAGRAPH + record.getNoteId() + "/" + record.getParagraphId();
Map<String, Object> headers = new HashMap<>();
JSONObject jsonObject = HttpUtil.httpPostUtil(params, headers, url);
checkResponse(jsonObject);
return jsonObject.getJSONObject("body");
}
public void deleteNode(String noteId, String zeppelinUrl) throws Exception {
String url = zeppelinUrl + Constant.DELETE_NOTE + noteId;
Map<String, Object> headers = new HashMap<>();
JSONObject jsonObject = HttpUtil.httpDeleteUtil(headers, url);
checkResponse(jsonObject);
}
/**
* Throw exception if the status code is not 200.
*
* @param jsonObject
* @throws Exception
*/
private void checkResponse(JSONObject jsonObject) throws Exception {
if (null == jsonObject) {
throw new Exception(Constant.ERROR_MESSAGE);
}
if (!"OK".equals(jsonObject.getString("status"))) {
String message = jsonObject.getString("message");
throw new Exception(String.format("执行异常, message: %s", message));
}
}
}