Search in sources :

Example 1 with NotebookImportDeserializer

use of org.apache.zeppelin.notebook.NotebookImportDeserializer in project zeppelin by apache.

the class MongoNotebookRepo method documentToNote.

/**
   * Convert document to note
   */
private Note documentToNote(Document doc) {
    // document to JSON
    String json = doc.toJson();
    // JSON to note
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    Note note = gson.fromJson(json, Note.class);
    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Job.Status.PENDING || p.getStatus() == Job.Status.RUNNING) {
            p.setStatus(Job.Status.ABORT);
        }
        List<ApplicationState> appStates = p.getAllApplicationStates();
        if (appStates != null) {
            for (ApplicationState app : appStates) {
                if (app.getStatus() != ApplicationState.Status.ERROR) {
                    app.setStatus(ApplicationState.Status.UNLOADED);
                }
            }
        }
    }
    return note;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Note(org.apache.zeppelin.notebook.Note) ApplicationState(org.apache.zeppelin.notebook.ApplicationState) Gson(com.google.gson.Gson) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) Paragraph(org.apache.zeppelin.notebook.Paragraph)

Example 2 with NotebookImportDeserializer

use of org.apache.zeppelin.notebook.NotebookImportDeserializer in project zeppelin by apache.

the class S3NotebookRepo method getNote.

private Note getNote(String key) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    S3Object s3object;
    try {
        s3object = s3client.getObject(new GetObjectRequest(bucketName, key));
    } catch (AmazonClientException ace) {
        throw new IOException("Unable to retrieve object from S3: " + ace, ace);
    }
    Note note;
    try (InputStream ins = s3object.getObjectContent()) {
        String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
        note = gson.fromJson(json, Note.class);
    }
    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
            p.setStatus(Status.ABORT);
        }
    }
    return note;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) AmazonClientException(com.amazonaws.AmazonClientException) Note(org.apache.zeppelin.notebook.Note) Gson(com.google.gson.Gson) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Date(java.util.Date) Paragraph(org.apache.zeppelin.notebook.Paragraph)

Example 3 with NotebookImportDeserializer

use of org.apache.zeppelin.notebook.NotebookImportDeserializer in project zeppelin by apache.

the class AzureNotebookRepo method getNote.

private Note getNote(String noteId) throws IOException {
    InputStream ins = null;
    try {
        CloudFileDirectory dir = rootDir.getDirectoryReference(noteId);
        CloudFile file = dir.getFileReference("note.json");
        ins = file.openRead();
    } catch (URISyntaxException | StorageException e) {
        String msg = String.format("Error reading notebook %s from Azure storage", noteId);
        LOG.error(msg, e);
        throw new IOException(msg, e);
    }
    String json = IOUtils.toString(ins, conf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_ENCODING));
    ins.close();
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    Note note = gson.fromJson(json, Note.class);
    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Job.Status.PENDING || p.getStatus() == Job.Status.RUNNING) {
            p.setStatus(Job.Status.ABORT);
        }
    }
    return note;
}
Also used : CloudFileDirectory(com.microsoft.azure.storage.file.CloudFileDirectory) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) Date(java.util.Date) Paragraph(org.apache.zeppelin.notebook.Paragraph) CloudFile(com.microsoft.azure.storage.file.CloudFile) Note(org.apache.zeppelin.notebook.Note) StorageException(com.microsoft.azure.storage.StorageException)

Example 4 with NotebookImportDeserializer

use of org.apache.zeppelin.notebook.NotebookImportDeserializer in project zeppelin by apache.

the class VFSNotebookRepo method getNote.

private Note getNote(FileObject noteDir) throws IOException {
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }
    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    if (!noteJson.exists()) {
        throw new IOException(noteJson.getName().toString() + " not found");
    }
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    FileContent content = noteJson.getContent();
    InputStream ins = content.getInputStream();
    String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
    ins.close();
    Note note = gson.fromJson(json, Note.class);
    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
            p.setStatus(Status.ABORT);
        }
        List<ApplicationState> appStates = p.getAllApplicationStates();
        if (appStates != null) {
            for (ApplicationState app : appStates) {
                if (app.getStatus() != ApplicationState.Status.ERROR) {
                    app.setStatus(ApplicationState.Status.UNLOADED);
                }
            }
        }
    }
    return note;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) ApplicationState(org.apache.zeppelin.notebook.ApplicationState) Gson(com.google.gson.Gson) IOException(java.io.IOException) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) Date(java.util.Date) Paragraph(org.apache.zeppelin.notebook.Paragraph) FileContent(org.apache.commons.vfs2.FileContent) Note(org.apache.zeppelin.notebook.Note) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 Note (org.apache.zeppelin.notebook.Note)4 NotebookImportDeserializer (org.apache.zeppelin.notebook.NotebookImportDeserializer)4 Paragraph (org.apache.zeppelin.notebook.Paragraph)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Date (java.util.Date)3 ApplicationState (org.apache.zeppelin.notebook.ApplicationState)2 AmazonClientException (com.amazonaws.AmazonClientException)1 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 StorageException (com.microsoft.azure.storage.StorageException)1 CloudFile (com.microsoft.azure.storage.file.CloudFile)1 CloudFileDirectory (com.microsoft.azure.storage.file.CloudFileDirectory)1 URISyntaxException (java.net.URISyntaxException)1 FileContent (org.apache.commons.vfs2.FileContent)1 FileObject (org.apache.commons.vfs2.FileObject)1