Search in sources :

Example 1 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project flink by apache.

the class AbstractMetricsHandler method getMetricsValues.

private String getMetricsValues(Map<String, String> pathParams, String requestedMetricsList) throws IOException {
    if (requestedMetricsList.isEmpty()) {
        /**
			 * The WebInterface doesn't check whether the list of available metrics was empty. This can lead to a 
			 * request for which the "get" parameter is an empty string.
			 */
        return "";
    }
    MetricStore metricStore = fetcher.getMetricStore();
    synchronized (metricStore) {
        Map<String, String> metrics = getMapFor(pathParams, metricStore);
        if (metrics == null) {
            return "";
        }
        String[] requestedMetrics = requestedMetricsList.split(",");
        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
        gen.writeStartArray();
        for (String requestedMetric : requestedMetrics) {
            Object metricValue = metrics.get(requestedMetric);
            if (metricValue != null) {
                gen.writeStartObject();
                gen.writeStringField("id", requestedMetric);
                gen.writeStringField("value", metricValue.toString());
                gen.writeEndObject();
            }
        }
        gen.writeEndArray();
        gen.close();
        return writer.toString();
    }
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 2 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project flink by apache.

the class JarListHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    try {
        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
        gen.writeStartObject();
        gen.writeStringField("address", queryParams.get(RuntimeMonitorHandler.WEB_MONITOR_ADDRESS_KEY));
        gen.writeArrayFieldStart("files");
        File[] list = jarDir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        });
        for (File f : list) {
            // separate the uuid and the name parts.
            String id = f.getName();
            int startIndex = id.indexOf("_");
            if (startIndex < 0) {
                continue;
            }
            String name = id.substring(startIndex + 1);
            if (name.length() < 5 || !name.endsWith(".jar")) {
                continue;
            }
            gen.writeStartObject();
            gen.writeStringField("id", id);
            gen.writeStringField("name", name);
            gen.writeNumberField("uploaded", f.lastModified());
            gen.writeArrayFieldStart("entry");
            String[] classes = new String[0];
            try {
                JarFile jar = new JarFile(f);
                Manifest manifest = jar.getManifest();
                String assemblerClass = null;
                if (manifest != null) {
                    assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_ASSEMBLER_CLASS);
                    if (assemblerClass == null) {
                        assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_MAIN_CLASS);
                    }
                }
                if (assemblerClass != null) {
                    classes = assemblerClass.split(",");
                }
            } catch (IOException ignored) {
            // we simply show no entries here
            }
            // show every entry class that can be loaded later on.
            for (String clazz : classes) {
                clazz = clazz.trim();
                PackagedProgram program = null;
                try {
                    program = new PackagedProgram(f, clazz, new String[0]);
                } catch (Exception ignored) {
                // ignore jar files which throw an error upon creating a PackagedProgram
                }
                if (program != null) {
                    gen.writeStartObject();
                    gen.writeStringField("name", clazz);
                    String desc = program.getDescription();
                    gen.writeStringField("description", desc == null ? "No description provided" : desc);
                    gen.writeEndObject();
                }
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
        gen.writeEndArray();
        gen.writeEndObject();
        gen.close();
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException("Failed to fetch jar list: " + e.getMessage(), e);
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) IOException(java.io.IOException) FilenameFilter(java.io.FilenameFilter) PackagedProgram(org.apache.flink.client.program.PackagedProgram) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 3 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project flink by apache.

the class JarPlanHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    try {
        JarActionHandlerConfig config = JarActionHandlerConfig.fromParams(pathParams, queryParams);
        JobGraph graph = getJobGraphAndClassLoader(config).f0;
        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
        gen.writeStartObject();
        gen.writeFieldName("plan");
        gen.writeRawValue(JsonPlanGenerator.generatePlan(graph));
        gen.writeEndObject();
        gen.close();
        return writer.toString();
    } catch (Exception e) {
        return sendError(e);
    }
}
Also used : JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 4 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project flink by apache.

the class JobAccumulatorsHandler method createJobAccumulatorsJson.

public static String createJobAccumulatorsJson(AccessExecutionGraph graph) throws IOException {
    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
    StringifiedAccumulatorResult[] allAccumulators = graph.getAccumulatorResultsStringified();
    gen.writeStartObject();
    gen.writeArrayFieldStart("job-accumulators");
    // empty for now
    gen.writeEndArray();
    gen.writeArrayFieldStart("user-task-accumulators");
    for (StringifiedAccumulatorResult acc : allAccumulators) {
        gen.writeStartObject();
        gen.writeStringField("name", acc.getName());
        gen.writeStringField("type", acc.getType());
        gen.writeStringField("value", acc.getValue());
        gen.writeEndObject();
    }
    gen.writeEndArray();
    gen.writeEndObject();
    gen.close();
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) StringifiedAccumulatorResult(org.apache.flink.runtime.accumulators.StringifiedAccumulatorResult) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 5 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project flink by apache.

the class JobManagerConfigHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
    gen.writeStartArray();
    for (String key : config.keySet()) {
        gen.writeStartObject();
        gen.writeStringField("key", key);
        // Mask key values which contain sensitive information
        if (key.toLowerCase().contains("password")) {
            String value = config.getString(key, null);
            if (value != null) {
                value = "******";
            }
            gen.writeStringField("value", value);
        } else {
            gen.writeStringField("value", config.getString(key, null));
        }
        gen.writeEndObject();
    }
    gen.writeEndArray();
    gen.close();
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)174 StringWriter (java.io.StringWriter)75 IOException (java.io.IOException)48 JsonFactory (com.fasterxml.jackson.core.JsonFactory)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 Map (java.util.Map)16 OutputStream (java.io.OutputStream)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 JsonParser (com.fasterxml.jackson.core.JsonParser)10 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 OutputStreamWriter (java.io.OutputStreamWriter)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 TaskManagerLocation (org.apache.flink.runtime.taskmanager.TaskManagerLocation)6 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)6 ExecutionState (org.apache.flink.runtime.execution.ExecutionState)5 AccessExecutionVertex (org.apache.flink.runtime.executiongraph.AccessExecutionVertex)5 RemoteSession (org.apache.jackrabbit.oak.remote.RemoteSession)5