Search in sources :

Example 11 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project hive by apache.

the class TestDesc method toJson.

private String toJson(Object obj) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    mapper.writeValue(out, obj);
    return out.toString();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 12 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project hive by apache.

the class LlapStatusServiceDriver method outputJson.

public void outputJson(PrintWriter writer) throws LlapStatusCliException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
    mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
    mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
    try {
        writer.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(appStatusBuilder));
    } catch (IOException e) {
        LOG.warn("Failed to create JSON", e);
        throw new LlapStatusCliException(ExitCode.LLAP_JSON_GENERATION_ERROR, "Failed to create JSON", e);
    }
}
Also used : IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 13 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project crate by crate.

the class PingTaskTest method testSuccessfulPingTaskRun.

@Test
public void testSuccessfulPingTaskRun() throws Exception {
    testServer = new HttpTestServer(18080, false);
    testServer.run();
    PingTask task = new PingTask(clusterService, clusterIdService, extendedNodeInfo, "http://localhost:18080/", Settings.EMPTY);
    task.run();
    assertThat(testServer.responses.size(), is(1));
    task.run();
    assertThat(testServer.responses.size(), is(2));
    for (long i = 0; i < testServer.responses.size(); i++) {
        String json = testServer.responses.get((int) i);
        Map<String, String> map = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();
        try {
            //convert JSON string to Map
            map = mapper.readValue(json, new TypeReference<HashMap<String, String>>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        assertThat(map, hasKey("kernel"));
        assertThat(map.get("kernel"), is(notNullValue()));
        assertThat(map, hasKey("cluster_id"));
        assertThat(map.get("cluster_id"), is(notNullValue()));
        assertThat(map, hasKey("master"));
        assertThat(map.get("master"), is(notNullValue()));
        assertThat(map, hasKey("ping_count"));
        assertThat(map.get("ping_count"), is(notNullValue()));
        Map<String, Long> pingCountMap;
        pingCountMap = mapper.readValue(map.get("ping_count"), new TypeReference<Map<String, Long>>() {
        });
        assertThat(pingCountMap.get("success"), is(i));
        assertThat(pingCountMap.get("failure"), is(0L));
        if (task.getHardwareAddress() != null) {
            assertThat(map, hasKey("hardware_address"));
            assertThat(map.get("hardware_address"), is(notNullValue()));
        }
        assertThat(map, hasKey("crate_version"));
        assertThat(map.get("crate_version"), is(notNullValue()));
        assertThat(map, hasKey("java_version"));
        assertThat(map.get("java_version"), is(notNullValue()));
    }
}
Also used : HashMap(java.util.HashMap) HttpTestServer(io.crate.http.HttpTestServer) TypeReference(org.codehaus.jackson.type.TypeReference) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 14 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class ESSink method doSerialize.

private void doSerialize(Event event) throws IOException {
    Map<String, String> headers = event.getHeaders();
    String content = null;
    String serviceName = "";
    if (!headers.containsKey("InsTime")) {
        long insTime = System.currentTimeMillis();
        headers.put("InsTime", String.valueOf(insTime));
    }
    if (headers.containsKey("ServiceName")) {
        serviceName = headers.get("ServiceName");
        int pos = serviceName.indexOf(".");
        if (pos > 0) {
            serviceName = serviceName.substring(0, pos);
        }
    }
    ObjectMapper objectMapper = new ObjectMapper();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator jgen = null;
    try {
        jgen = objectMapper.getJsonFactory().createJsonGenerator(baos, JsonEncoding.UTF8);
        jgen.writeStartObject();
        for (String headerKey : headers.keySet()) {
            String headerValue = headers.get(headerKey);
            if (headerValue != null && !headerValue.isEmpty()) {
                jgen.writeStringField(headerKey, headerValue);
            }
        }
        content = new String(event.getBody());
        content = content.replace('\t', ' ').replace('\n', ' ');
        if (content.length() > maxContentLength) {
            content = content.substring(0, maxContentLength - 15) + "<..truncated..>";
        }
        jgen.writeStringField("body", content);
        jgen.writeEndObject();
        jgen.flush();
        jgen = null;
        //outputStream.write((baos.toString() + "\n").getBytes());
        String curIndexName = getCurrentIndexName(serviceName.toLowerCase());
        //LOG.info("index: (" + curIndexName + "," + indexType + ") source: " + baos.toString());
        esThreadRequest.sourceList.add(baos.toString());
        esThreadRequest.indexNameList.add(curIndexName);
        esThreadRequest.indexTypeList.add(indexType);
        if (esThreadRequest.sourceList.size() >= bulkNum) {
            submitESRequest(esThreadRequest);
            esThreadRequest = new ESClientThread.ESThreadRequest();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    LOG.info("ES sink process: " + content + " " + headers.get("IP") + " " + headers.get("Level") + " " + headers.get("RPCName"));
}
Also used : JsonGenerator(org.codehaus.jackson.JsonGenerator) String(java.lang.String) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 15 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class TestClient method main.

public static void main(String[] args) {
    Options options = new Options();
    Option option = new Option("t", "type", true, "the type of this request: query/add/del/get/graph");
    option.setRequired(true);
    options.addOption(option);
    option = new Option("f", "field", true, "required if add field or del field");
    option.setRequired(false);
    options.addOption(option);
    option = new Option("h", "help", false, "show help");
    options.addOption(option);
    CommandLineParser parser = new GnuParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Parse command line failed.");
        e.printStackTrace();
        return;
    }
    if (commandLine.hasOption('h')) {
        new HelpFormatter().printHelp("LogsysProxy Client", options, true);
        return;
    }
    String reqType = commandLine.getOptionValue('t');
    String fieldName = commandLine.getOptionValue("f");
    if (!reqType.equals("query") && !reqType.equals("add") && !reqType.equals("del") && !reqType.equals("get") && !reqType.equals("graph")) {
        System.out.println("Invalid type " + reqType);
        return;
    }
    if ((reqType.equals("add") || reqType.equals("del")) && (fieldName == null || fieldName.isEmpty())) {
        System.out.println("No fieldname specified.");
        return;
    }
    try {
        Socket socket = new Socket("localhost", 30150);
        //�ֽ������
        OutputStream os = socket.getOutputStream();
        org.msec.LogsysReq req = new org.msec.LogsysReq();
        if (reqType.equals("query")) {
            req.queryLogReq = new org.msec.LogsysReq.QueryLogReq();
            setRequest(req.queryLogReq);
        }
        if (reqType.equals("add")) {
            req.modifyFieldsReq = new org.msec.LogsysReq.ModifyFieldsReq();
            req.modifyFieldsReq.setAppName("App");
            req.modifyFieldsReq.setFieldName(fieldName);
            req.modifyFieldsReq.setFieldType("String");
            req.modifyFieldsReq.setOperator("ADD");
        }
        if (reqType.equals("del")) {
            req.modifyFieldsReq = new org.msec.LogsysReq.ModifyFieldsReq();
            req.modifyFieldsReq.setAppName("TestApp");
            req.modifyFieldsReq.setFieldName(fieldName);
            req.modifyFieldsReq.setFieldType("String");
            req.modifyFieldsReq.setOperator("DEL");
        }
        if (reqType.equals("get")) {
            req.getFieldsReq = new org.msec.LogsysReq.GetFieldsReq();
            req.getFieldsReq.setAppName("TestApp");
        }
        if (reqType.equals("graph")) {
            req.callGraphReq = new org.msec.LogsysReq.CallGraphReq();
            req.callGraphReq.setAppName("JavaSample.Jecho");
            //req.callGraphReq.addFilterField(new LogField("RPCName", "", "rpc814"));
            req.callGraphReq.setStartDate("2016-07-04");
            req.callGraphReq.setEndDate("2016-07-04");
            req.callGraphReq.setStartTime("00:00:00");
            req.callGraphReq.setEndTime("23:59:00");
        }
        ObjectMapper objectMapper = new ObjectMapper();
        String reqJsonStr = objectMapper.writeValueAsString(req);
        String reqJsonLen = String.format("%-10d", reqJsonStr.length());
        System.out.println(reqJsonLen + reqJsonStr);
        os.write(reqJsonLen.getBytes());
        os.write(reqJsonStr.getBytes());
        //�ر������
        socket.shutdownOutput();
        InputStream is = socket.getInputStream();
        byte[] buff = new byte[1024];
        int readLen = 0;
        //ѭ����ȡ
        while ((readLen = is.read(buff)) > 0) {
            System.out.println(new String(buff));
        }
        is.close();
        os.close();
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Socket(java.net.Socket) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

ObjectMapper (org.codehaus.jackson.map.ObjectMapper)356 IOException (java.io.IOException)75 Test (org.junit.Test)58 JsonNode (org.codehaus.jackson.JsonNode)47 ArrayList (java.util.ArrayList)43 HashMap (java.util.HashMap)43 Test (org.testng.annotations.Test)37 Map (java.util.Map)33 List (java.util.List)25 StringWriter (java.io.StringWriter)22 File (java.io.File)21 JSONObject (org.json.JSONObject)18 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)15 Version (org.codehaus.jackson.Version)14 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 JsonFactory (org.codehaus.jackson.JsonFactory)13 JSONObject (org.codehaus.jettison.json.JSONObject)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)11