Running complete notebooks Zeppelin API https via java

here is a small code snippet example to allow any java user to login into zeppelin and run complete notebooks.

 

this is the main function

public void zeppelinApiRunNotebook() throws ClientProtocolException, IOException, AuthenticationException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException

{

LogUtil.logUtilWriteInfo(“”);

DefaultHttpClient httpclient = new DefaultHttpClient();

String cookieId = zeppelinApiLogin(httpclient);

String postData = https://”+ZeppelinURL+“/api/notebook/job/”+ZeppelinNotebookId;

HttpPost postRequest = new HttpPost(postData);

List<NameValuePair> formparams = new ArrayList<NameValuePair>();

formparams.add(new BasicNameValuePair(“JSESSIONID”, cookieId));

postRequest.setEntity(new UrlEncodedFormEntity(formparams, “UTF-8”));

CloseableHttpResponse response1 = httpclient.execute(postRequest);

System.out.println(response1);

response1.close();

httpclient.close();

}

smaller function, to login to zeppelin, and to cookie ID

private static String zeppelinApiLogin(DefaultHttpClient httpclient) throws ClientProtocolException, IOException, AuthenticationException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException

{

String urlLogin = https://&#8221;+ZeppelinURL+“/api/login”;

    HttpPost postRequest = new HttpPost(urlLogin);

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();

    formparams.add(new BasicNameValuePair(“userName”, Prop.getProperty(“user_name”)));

    formparams.add(new BasicNameValuePair(“password”,Prop.getProperty(“password”)));

    postRequest.setEntity(new UrlEncodedFormEntity(formparams, “UTF-8”));

    CloseableHttpResponse response = httpclient.execute(postRequest);

    System.out.println(response);

    response.close();

    return parseSessionID(response);

     

}

private static String parseSessionID(HttpResponse response) {

LogUtil.logUtilWriteDebug(“”);

    String nid= “”;

   

try {

       

        Header[] header = response.getAllHeaders();

        for (int i = 0; i < header.length; i++)

        {

        String value = header[i].getValue();

   

        if (value.contains(“JSESSIONID”)) {

            int index = value.indexOf(“JSESSIONID=”);

            int endIndex = value.indexOf(“;”, index);

            String sessionID = value.substring(

                    index + “JSESSIONID=”.length(), endIndex);

            nid=sessionID;

     

        }

        }

       

    } catch (Exception e) {

    LogUtil.logUtilWriteError(“can’t read cookie from zeppelin login”);

    }

       return nid;

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s