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();
}
}
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;
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations