Search in sources :

Example 61 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project dhis2-core by dhis2.

the class V2_33_5__Update_job_parameters_with_system_setting_values method migrate.

@Override
public void migrate(final Context context) throws Exception {
    // 1. Fetch data from SystemSettings if present
    int trackerPageSize = 0;
    int eventPageSize = 0;
    int dataValuesPageSize = 0;
    String sql = "SELECT value FROM systemsetting WHERE name = '" + TRACKER_PROGRAM_SYNC_PAGE_SIZE + "';";
    try (Statement stmt = context.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(sql)) {
        if (rs.next()) {
            trackerPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
        }
    }
    sql = "SELECT value FROM systemsetting WHERE name = '" + EVENT_PROGRAM_SYNC_PAGE_SIZE + "';";
    try (Statement stmt = context.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(sql)) {
        if (rs.next()) {
            eventPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
        }
    }
    sql = "SELECT value FROM systemsetting WHERE name = '" + DATA_VALUES_SYNC_PAGE_SIZE + "';";
    try (Statement stmt = context.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(sql)) {
        if (rs.next()) {
            dataValuesPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
        }
    }
    log.info("Following values were found in SystemSettings: trackerPageSize: " + trackerPageSize + ", eventPageSize: " + eventPageSize + ", dataValuePageSize: " + dataValuesPageSize);
    // 2. Update jobParameters with data from SystemSettings
    if (trackerPageSize > 0 || eventPageSize > 0 || dataValuesPageSize > 0) {
        Map<Integer, JobParameters> updatedJobParameters = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();
        mapper.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().allowIfBaseType(JobParameters.class).build());
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        JavaType resultingJavaType = mapper.getTypeFactory().constructType(JobParameters.class);
        ObjectReader reader = mapper.readerFor(resultingJavaType);
        ObjectWriter writer = mapper.writerFor(resultingJavaType);
        if (trackerPageSize > 0) {
            sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.TRACKER_PROGRAMS_DATA_SYNC.name() + "';";
            try (Statement stmt = context.getConnection().createStatement();
                ResultSet rs = stmt.executeQuery(sql)) {
                while (rs.next()) {
                    TrackerProgramsDataSynchronizationJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
                    jobparams.setPageSize(trackerPageSize);
                    updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
                }
            }
        }
        if (eventPageSize > 0) {
            sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.EVENT_PROGRAMS_DATA_SYNC.name() + "';";
            try (Statement stmt = context.getConnection().createStatement();
                ResultSet rs = stmt.executeQuery(sql)) {
                while (rs.next()) {
                    EventProgramsDataSynchronizationJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
                    jobparams.setPageSize(eventPageSize);
                    updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
                }
            }
        }
        sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.META_DATA_SYNC.name() + "';";
        try (Statement stmt = context.getConnection().createStatement();
            ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                MetadataSyncJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
                if (trackerPageSize > 0) {
                    jobparams.setTrackerProgramPageSize(trackerPageSize);
                }
                if (eventPageSize > 0) {
                    jobparams.setEventProgramPageSize(eventPageSize);
                }
                if (dataValuesPageSize > 0) {
                    jobparams.setDataValuesPageSize(dataValuesPageSize);
                }
                updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
            }
        }
        for (Map.Entry<Integer, JobParameters> jobParams : updatedJobParameters.entrySet()) {
            try (PreparedStatement ps = context.getConnection().prepareStatement("UPDATE jobconfiguration SET jsonbjobparameters = ? where  jobconfigurationid = ?;")) {
                PGobject pg = new PGobject();
                pg.setType("jsonb");
                pg.setValue(writer.writeValueAsString(jobParams.getValue()));
                ps.setObject(1, pg);
                ps.setInt(2, jobParams.getKey());
                ps.execute();
            }
        }
    }
    // 3. Remove entries from systemsetting table
    try (Statement stmt = context.getConnection().createStatement()) {
        stmt.executeUpdate("DELETE FROM systemsetting WHERE name IN ('" + TRACKER_PROGRAM_SYNC_PAGE_SIZE + "', '" + EVENT_PROGRAM_SYNC_PAGE_SIZE + "', '" + DATA_VALUES_SYNC_PAGE_SIZE + "');");
    }
}
Also used : HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) PreparedStatement(java.sql.PreparedStatement) EventProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.EventProgramsDataSynchronizationJobParameters) PGobject(org.postgresql.util.PGobject) JavaType(com.fasterxml.jackson.databind.JavaType) TrackerProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.TrackerProgramsDataSynchronizationJobParameters) ResultSet(java.sql.ResultSet) MetadataSyncJobParameters(org.hisp.dhis.scheduling.parameters.MetadataSyncJobParameters) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JobParameters(org.hisp.dhis.scheduling.JobParameters) EventProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.EventProgramsDataSynchronizationJobParameters) MetadataSyncJobParameters(org.hisp.dhis.scheduling.parameters.MetadataSyncJobParameters) TrackerProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.TrackerProgramsDataSynchronizationJobParameters) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 62 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project hive by apache.

the class HiveJsonWriter method write.

/**
 * Given an Object and an ObjectInspector, convert the structure into a JSON
 * text string.
 *
 * @param o The object to convert
 * @param objInspector The ObjectInspector describing the object
 * @return A String containing the JSON text
 * @throws SerDeException The object cannot be transformed
 */
public String write(final Object o, final ObjectInspector objInspector) throws SerDeException {
    final JsonNode rootNode = walkObjectGraph(objInspector, o, rootStructFieldNames);
    LOG.debug("Create JSON tree from Object tree: {}", rootNode);
    try {
        final ObjectWriter ow = isEnabled(Feature.PRETTY_PRINT) ? this.mapper.writerWithDefaultPrettyPrinter() : this.mapper.writer();
        return ow.writeValueAsString(rootNode);
    } catch (JsonProcessingException e) {
        throw new SerDeException(e);
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 63 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project uPortal by Jasig.

the class AnalyticsIncorporationComponentEventSerializationTest method testMixinNoCopy.

@Test
public void testMixinNoCopy() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();
    mapper.addMixInAnnotations(Object.class, PortletRenderExecutionEventFilterMixIn.class);
    final FilterProvider filterProvider = new SimpleFilterProvider().addFilter(PortletRenderExecutionEventFilterMixIn.FILTER_NAME, SimpleBeanPropertyFilter.filterOutAllExcept("fname", "executionTimeNano", "parameters"));
    final ObjectWriter portletEventWriter = mapper.writer(filterProvider);
    final String result = portletEventWriter.writeValueAsString(createEvent());
    assertEquals("{\"@c\":\".PortletRenderExecutionEvent\",\"fname\":\"fname1\",\"executionTimeNano\":123450000,\"parameters\":{}}", result);
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Test(org.junit.Test)

Example 64 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project uPortal by Jasig.

the class JpaStatisticalSummaryTest method testStorelessUnivariateStatistic.

public void testStorelessUnivariateStatistic(StorelessUnivariateStatistic sus, double expected) throws Exception {
    assertEquals(expected, sus.getResult(), 0.1);
    final ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();
    // Configure Jackson to just use fields
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);
    mapper.addMixInAnnotations(Object.class, IgnoreTypeMixIn.class);
    final FilterProvider filters = new SimpleFilterProvider().addFilter("storedDataFilter", SimpleBeanPropertyFilter.serializeAllExcept("storedData"));
    final ObjectWriter ssWriter = mapper.writer(filters);
    final ObjectReader ssReader = mapper.reader(sus.getClass());
    final String susString = ssWriter.writeValueAsString(sus);
    System.out.println(susString);
    final StorelessUnivariateStatistic newSus = ssReader.readValue(susString);
    assertEquals(expected, newSus.getResult(), 0.1);
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) StorelessUnivariateStatistic(org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)

Example 65 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project xm-ms-entity by xm-online.

the class EntityToCsvConverterUtils method toCsv.

/**
 * Writes entities to csv file.
 * @param o the object which serialize to csv
 * @param schema the csv schema
 * @return byte array of csv file
 */
public static byte[] toCsv(Object o, CsvSchema schema) {
    if (o == null) {
        log.warn("Passed empty object for serialize, therefore return empty byte array which represents csv file");
        return new byte[0];
    }
    CsvMapper mapper = createDefaultCsvMapper();
    ObjectWriter csvWriter = mapper.writer(schema);
    try {
        return csvWriter.writeValueAsBytes(o);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Exception while writing data to csv file", e);
    }
}
Also used : CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)140 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)61 IOException (java.io.IOException)31 Test (org.junit.Test)30 File (java.io.File)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)15 ArrayList (java.util.ArrayList)12 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)11 JavaType (com.fasterxml.jackson.databind.JavaType)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 StringWriter (java.io.StringWriter)7 Map (java.util.Map)7 JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)6 FileInputStream (java.io.FileInputStream)6