Search in sources :

Example 6 with CsvWriter

use of com.univocity.parsers.csv.CsvWriter in project conquery by bakdata.

the class AdminProcessor method getPermissionOverviewAsCSV.

/**
 * Renders the permission overview for certain {@link User} in form of a CSV.
 */
public String getPermissionOverviewAsCSV(Collection<User> users) {
    StringWriter sWriter = new StringWriter();
    CsvWriter writer = config.getCsv().createWriter(sWriter);
    List<String> scope = config.getAuthorizationRealms().getOverviewScope();
    // Header
    writeAuthOverviewHeader(writer, scope);
    // Body
    for (User user : users) {
        writeAuthOverviewUser(writer, scope, user, storage, config);
    }
    return sWriter.toString();
}
Also used : User(com.bakdata.conquery.models.auth.entities.User) StringWriter(java.io.StringWriter) CsvWriter(com.univocity.parsers.csv.CsvWriter)

Example 7 with CsvWriter

use of com.univocity.parsers.csv.CsvWriter in project droid by digital-preservation.

the class CsvItemWriter method open.

@Override
public void open(final Writer writer) {
    final CsvWriterSettings csvWriterSettings = new CsvWriterSettings();
    csvWriterSettings.setQuoteAllFields(true);
    CsvFormat format = new CsvFormat();
    // following Unix convention on line separators as previously
    format.setLineSeparator("\n");
    csvWriterSettings.setFormat(format);
    csvWriter = new CsvWriter(writer, csvWriterSettings);
    if (headers == null) {
        headers = HEADERS;
    }
    csvWriter.writeHeaders(headers);
}
Also used : CsvWriter(com.univocity.parsers.csv.CsvWriter) CsvWriterSettings(com.univocity.parsers.csv.CsvWriterSettings) CsvFormat(com.univocity.parsers.csv.CsvFormat)

Example 8 with CsvWriter

use of com.univocity.parsers.csv.CsvWriter in project hillview by vmware.

the class CsvFileTest method csvWriterTest.

@Test
public void csvWriterTest() throws IOException {
    // The Csv writer (Univocity) we were using had a bug,
    // reproduced with this test.
    String[] data = new String[] { "", null };
    CsvWriterSettings settings = new CsvWriterSettings();
    CsvFormat format = new CsvFormat();
    settings.setFormat(format);
    settings.setEmptyValue("\"\"");
    settings.setNullValue("");
    String fileName = "tmp.csv";
    Writer fw = new FileWriter(fileName);
    CsvWriter writer = new CsvWriter(fw, settings);
    writer.writeRow(data);
    writer.close();
    fw.close();
    File file = new File(fileName);
    if (file.exists()) {
        @SuppressWarnings("unused") boolean ignored = file.delete();
    }
}
Also used : CsvWriter(com.univocity.parsers.csv.CsvWriter) FileWriter(java.io.FileWriter) CsvFileWriter(org.hillview.storage.CsvFileWriter) CsvWriterSettings(com.univocity.parsers.csv.CsvWriterSettings) CsvFormat(com.univocity.parsers.csv.CsvFormat) File(java.io.File) FileWriter(java.io.FileWriter) CsvWriter(com.univocity.parsers.csv.CsvWriter) CsvFileWriter(org.hillview.storage.CsvFileWriter) Writer(java.io.Writer) BaseTest(org.hillview.test.BaseTest) Test(org.junit.Test)

Example 9 with CsvWriter

use of com.univocity.parsers.csv.CsvWriter in project drill by apache.

the class TextRecordWriter method startNewSchema.

@Override
public void startNewSchema(BatchSchema schema) throws IOException {
    // wrap up the current file
    cleanup();
    // open a new file for writing data with new schema
    Path fileName = new Path(location, String.format("%s_%s%s", prefix, fileNumberIndex, extension));
    try {
        // Drill text writer does not support partitions, so only one file can be created
        // and thus only one location should be deleted in case of abort
        // to ensure that our writer was the first to create output file,
        // we create empty output file first and fail if file exists
        cleanUpLocation = storageStrategy.createFileAndApply(fs, fileName);
        // since empty output file will be overwritten (some file systems may restrict append option)
        // we need to re-apply file permission
        DataOutputStream fos = fs.create(fileName);
        storageStrategy.applyToFile(fs, fileName);
        logger.debug("Created file: {}.", fileName);
        // increment file number index
        fileNumberIndex++;
        this.writer = new CsvWriter(fos, writerSettings);
    } catch (IOException e) {
        throw new IOException(String.format("Unable to create file: %s.", fileName), e);
    }
    if (writerSettings.isHeaderWritingEnabled()) {
        writer.writeHeaders(StreamSupport.stream(schema.spliterator(), false).map(MaterializedField::getName).collect(Collectors.toList()));
    }
}
Also used : Path(org.apache.hadoop.fs.Path) CsvWriter(com.univocity.parsers.csv.CsvWriter) DataOutputStream(java.io.DataOutputStream) MaterializedField(org.apache.drill.exec.record.MaterializedField) IOException(java.io.IOException)

Aggregations

CsvWriter (com.univocity.parsers.csv.CsvWriter)9 CsvWriterSettings (com.univocity.parsers.csv.CsvWriterSettings)7 CsvFormat (com.univocity.parsers.csv.CsvFormat)3 File (java.io.File)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 User (com.bakdata.conquery.models.auth.entities.User)1 PolicyStats (com.github.benmanes.caffeine.cache.simulator.policy.PolicyStats)1 CsvParser (com.univocity.parsers.csv.CsvParser)1 CsvParserSettings (com.univocity.parsers.csv.CsvParserSettings)1 java.awt (java.awt)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StandardCharsets (java.nio.charset.StandardCharsets)1 DecimalFormat (java.text.DecimalFormat)1 java.util (java.util)1