Example usage for com.google.common.io CharSink write

List of usage examples for com.google.common.io CharSink write

Introduction

In this page you can find the example usage for com.google.common.io CharSink write.

Prototype

public void write(CharSequence chars) throws IOException 

Source Link

Usage

From source file:net.thangbui.cql_exporter.TableExporter.java

static void genDML(SchemaExporter generator, TableMetadata table, File file)
        throws IOException, ExecutionException, InterruptedException {
    String tableName = Utils.escapeReservedWord(table.getName());
    String keyspaceName = table.getKeyspace().getName();
    CharSink charSink = Files.asCharSink(file, Charset.defaultCharset(), FileWriteMode.APPEND);

    System.out.printf("-----------------------------------------------" + Main.LINE_SEPARATOR);
    System.out.printf("Extract from %s.%s" + Main.LINE_SEPARATOR, keyspaceName, tableName);

    if (generator.truncate) {
        charSink.write(QueryBuilder.truncate(keyspaceName, tableName).getQueryString());
    }//w ww  . ja va 2  s  . c om

    Row firstRow = getFirstRow(generator.session, tableName, keyspaceName);

    if (firstRow == null) {
        return;
    }

    final List<String> colNames = new ArrayList();
    final List<TypeCodec> typeCodecs = new ArrayList();
    List<ColumnDefinitions.Definition> definitions = firstRow.getColumnDefinitions().asList();

    for (ColumnDefinitions.Definition definition : definitions) {
        String colName = definition.getName();
        DataType type = definition.getType();
        colNames.add(Utils.escapeReservedWord(colName));
        Object object = firstRow.getObject(colName);
        typeCodecs.add(object != null ? CODEC_REGISTRY.codecFor(type, object) : CODEC_REGISTRY.codecFor(type));
    }

    String prefix = "INSERT INTO " + keyspaceName + "." + tableName + " (" + Joiner.on(',').join(colNames)
            + ") VALUES (";
    String postfix = generator.merge ? ") IF NOT EXISTS;" : ");";

    long totalNoOfRows = getTotalNoOfRows(generator.session, tableName, keyspaceName);
    System.out.printf("Total number of record: %s" + Main.LINE_SEPARATOR, totalNoOfRows);

    int count = 0;
    Select select = QueryBuilder.select().all().from(keyspaceName, tableName).allowFiltering();
    select.setFetchSize(SchemaExporter.FETCH_SIZE);
    ResultSet resultSet = generator.session.execute(select);
    Iterator<Row> iterator = resultSet.iterator();

    System.out.printf("Start write \"%s\" data DML to %s" + Main.LINE_SEPARATOR, tableName,
            file.getCanonicalPath());

    int noOfStep = getNoOfStep(totalNoOfRows);
    long step = totalNoOfRows / noOfStep;
    int stepCount = 0;

    while (iterator.hasNext()) {
        Row next = iterator.next();
        String statement = generateInsertFromRow(typeCodecs, prefix, postfix, next) + Main.LINE_SEPARATOR;
        charSink.write(statement);
        count++;
        if (totalNoOfRows > SchemaExporter.FETCH_SIZE && count > stepCount * step) {
            float v = (float) count / (float) totalNoOfRows * 100;
            System.out.printf("Done %.2f%%" + Main.LINE_SEPARATOR, v);
            stepCount++;
        }
    }
    System.out.printf("Done exporting \"%s\", total number of records exported: %s" + Main.LINE_SEPARATOR,
            tableName, count);
}

From source file:uk.ac.ebi.fg.annotare2.magetabcheck.CheckListGenerator.java

private void generateMarkdown(final File out) throws IOException {
    if (!out.exists()) {
        File parent = out.getParentFile();
        if (!parent.exists() && !parent.mkdirs()) {
            throw new IllegalStateException("Can't create directories " + parent);
        }/* w  w w  . j a  v a 2  s.c o m*/
    }
    CharSink sink = Files.asCharSink(out, Charsets.UTF_8);
    sink.write(markdown());
}

From source file:com.google.googlejavaformat.java.Formatter.java

/**
 * Format the given input (a Java compilation unit) into the output stream.
 *
 * @throws FormatterException if the input cannot be parsed
 *///from  w w w.  j  a  va2s.  co  m
public void formatSource(CharSource input, CharSink output) throws FormatterException, IOException {
    // TODO(cushon): proper support for streaming input/output. Input may
    // not be feasible (parsing) but output should be easier.
    output.write(formatSource(input.read()));
}