Search in sources :

Example 51 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project Java-Tutorial by gpcodervn.

the class ApacheCsvWriterExample method main.

public static void main(String[] args) throws IOException {
    String csvFile = "data/data.csv";
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(csvFile));
        CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader("id", "code", "name"))) {
        csvPrinter.printRecord("1", "US", "United States");
        csvPrinter.printRecord("2", "VN", "Viet Nam");
        csvPrinter.printRecord("3", "AU", "Australia");
        csvPrinter.flush();
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 52 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project thingsboard by thingsboard.

the class CassandraDbHelper method dumpCfIfExists.

public static Path dumpCfIfExists(KeyspaceMetadata ks, Session session, String cfName, String[] columns, String[] defaultValues, String dumpPrefix, boolean printHeader) throws Exception {
    if (ks.getTable(cfName) != null) {
        Path dumpFile = Files.createTempFile(dumpPrefix, null);
        Files.deleteIfExists(dumpFile);
        CSVFormat csvFormat = CSV_DUMP_FORMAT;
        if (printHeader) {
            csvFormat = csvFormat.withHeader(columns);
        }
        try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(dumpFile), csvFormat)) {
            Statement stmt = new SimpleStatement("SELECT * FROM " + cfName);
            stmt.setFetchSize(1000);
            ResultSet rs = session.execute(stmt);
            Iterator<Row> iter = rs.iterator();
            while (iter.hasNext()) {
                Row row = iter.next();
                if (row != null) {
                    dumpRow(row, columns, defaultValues, csvPrinter);
                }
            }
        }
        return dumpFile;
    } else {
        return null;
    }
}
Also used : Path(java.nio.file.Path) CSVPrinter(org.apache.commons.csv.CSVPrinter) CSVFormat(org.apache.commons.csv.CSVFormat)

Example 53 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project webapp by elimu-ai.

the class SoundCsvExportController method handleRequest.

@RequestMapping(value = "/sounds.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<Sound> sounds = soundDao.readAllOrderedByUsage();
    logger.info("sounds.size(): " + sounds.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "value_ipa", "value_sampa", "audio_id", "diacritic", "sound_type", "usage_count");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (Sound sound : sounds) {
        Long audioId = null;
        if (sound.getAudio() != null) {
            audioId = sound.getAudio().getId();
        }
        csvPrinter.printRecord(sound.getId(), sound.getValueIpa(), sound.getValueSampa(), audioId, sound.isDiacritic(), sound.getSoundType(), sound.getUsageCount());
        csvPrinter.flush();
    }
    String csvFileContent = stringWriter.toString();
    response.setContentType("text/csv");
    byte[] bytes = csvFileContent.getBytes();
    response.setContentLength(bytes.length);
    try {
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        logger.error(ex);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) StringWriter(java.io.StringWriter) Sound(ai.elimu.model.content.Sound) CSVFormat(org.apache.commons.csv.CSVFormat) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 54 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project webapp by elimu-ai.

the class NumberCsvExportController method handleRequest.

@RequestMapping(value = "/numbers.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<Number> numbers = numberDao.readAllOrdered();
    logger.info("numbers.size(): " + numbers.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "value", "symbol", "word_ids", "word_texts");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (Number number : numbers) {
        logger.info("number.getValue(): \"" + number.getValue() + "\"");
        JSONArray wordIdsJsonArray = new JSONArray();
        int index = 0;
        for (Word word : number.getWords()) {
            wordIdsJsonArray.put(index, word.getId());
            index++;
        }
        JSONArray wordTextsJsonArray = new JSONArray();
        index = 0;
        for (Word word : number.getWords()) {
            wordTextsJsonArray.put(index, word.getText());
            index++;
        }
        csvPrinter.printRecord(number.getId(), number.getValue(), number.getSymbol(), wordIdsJsonArray, wordTextsJsonArray);
        csvPrinter.flush();
    }
    String csvFileContent = stringWriter.toString();
    response.setContentType("text/csv");
    byte[] bytes = csvFileContent.getBytes();
    response.setContentLength(bytes.length);
    try {
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        logger.error(ex);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Word(ai.elimu.model.content.Word) Number(ai.elimu.model.content.Number) StringWriter(java.io.StringWriter) JSONArray(org.json.JSONArray) CSVFormat(org.apache.commons.csv.CSVFormat) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 55 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project webapp by elimu-ai.

the class LetterSoundCorrespondenceCsvExportController method handleRequest.

@RequestMapping(value = "/letter-sound-correspondences.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<LetterSoundCorrespondence> letterSoundCorrespondences = letterSoundCorrespondenceDao.readAllOrderedByUsage();
    logger.info("letterSoundCorrespondences.size(): " + letterSoundCorrespondences.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "letter_ids", "letter_texts", "sound_ids", "sound_values_ipa", "usage_count");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (LetterSoundCorrespondence letterSoundCorrespondence : letterSoundCorrespondences) {
        logger.info("letterSoundCorrespondence.getId(): \"" + letterSoundCorrespondence.getId() + "\"");
        JSONArray letterIdsJsonArray = new JSONArray();
        int index = 0;
        for (Letter letter : letterSoundCorrespondence.getLetters()) {
            letterIdsJsonArray.put(index, letter.getId());
            index++;
        }
        JSONArray letterTextsJsonArray = new JSONArray();
        index = 0;
        for (Letter letter : letterSoundCorrespondence.getLetters()) {
            letterTextsJsonArray.put(index, letter.getText());
            index++;
        }
        JSONArray soundIdsJsonArray = new JSONArray();
        index = 0;
        for (Sound sound : letterSoundCorrespondence.getSounds()) {
            soundIdsJsonArray.put(index, sound.getId());
            index++;
        }
        JSONArray soundValuesIpaJsonArray = new JSONArray();
        index = 0;
        for (Sound sound : letterSoundCorrespondence.getSounds()) {
            soundValuesIpaJsonArray.put(index, sound.getValueIpa());
            index++;
        }
        csvPrinter.printRecord(letterSoundCorrespondence.getId(), letterIdsJsonArray, letterTextsJsonArray, soundIdsJsonArray, soundValuesIpaJsonArray, letterSoundCorrespondence.getUsageCount());
        csvPrinter.flush();
    }
    String csvFileContent = stringWriter.toString();
    response.setContentType("text/csv");
    byte[] bytes = csvFileContent.getBytes();
    response.setContentLength(bytes.length);
    try {
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        logger.error(ex);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Letter(ai.elimu.model.content.Letter) StringWriter(java.io.StringWriter) JSONArray(org.json.JSONArray) CSVFormat(org.apache.commons.csv.CSVFormat) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

CSVPrinter (org.apache.commons.csv.CSVPrinter)71 IOException (java.io.IOException)25 CSVFormat (org.apache.commons.csv.CSVFormat)20 OutputStreamWriter (java.io.OutputStreamWriter)17 StringWriter (java.io.StringWriter)14 Writer (java.io.Writer)13 ArrayList (java.util.ArrayList)12 FileOutputStream (java.io.FileOutputStream)11 Test (org.junit.Test)11 FileWriter (java.io.FileWriter)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 BufferedWriter (java.io.BufferedWriter)6 File (java.io.File)5 PrintWriter (java.io.PrintWriter)5 JSONArray (org.json.JSONArray)5 Map (java.util.Map)4 Word (ai.elimu.model.content.Word)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Letter (ai.elimu.model.content.Letter)2 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)2