Coder Social home page Coder Social logo

opencsv's Introduction

OpenCSV

Based on OpenCSV(3.6) on http://opencsv.sourceforge.net, please visit the address for its original usage. Requires JRE 1.7+.

Additional Features

- Performance and memory optimization by NIO, buffering and the reduction of object creations
- Convert ResultSet to CSV file, refer to CSVWriter.writeAll(<Resuleset>,...) 
- Convert ResultSet to SQL file, refer to SQLWriter.writeAll2SQL(<Resuleset>,...) 
- Convert CSV file to SQL file,  refer to SQLWriter.writeAll2SQL(<CSVFilePath>,...)
- Convert ResultSet/CSV to Oracle SQL*Loader Files(Automatically)
- Support automatically compression if destination file extension is ".zip" or ".gz"
- Support Multi-threads processing to speed up performance, refer to CSVWriter.setAsyncMode(boolean) 

Examples

//Extract ResultSet to CSV file, auto-compress if the fileName extension is ".zip" or ".gz"
//Returns number of records extracted
public int ResultSet2CSV(final ResultSet rs, final String fileName, final String header, final boolean aync) throws Exception {
    try (CSVWriter writer = new CSVWriter(fileName)) {
        //Define fetch size(default as 30000 rows), higher to be faster performance but takes more memory
        ResultSetHelperService.RESULT_FETCH_SIZE=10000;
        //Define MAX extract rows, -1 means unlimited.
        ResultSetHelperService.MAX_FETCH_ROWS=20000;
        writer.setAsyncMode(aync);
        int result = writer.writeAll(rs, true);
        return result - 1;
    }
}

//Extract ResultSet to SQL file, returns number of records extracted
//Parameter "header" defines the extract information to be appended into the file, can be ""
//Parameter "rs" can be null, which helps the engine identifying the column list and data types
public int ResultSet2SQL(final ResultSet rs, final String fileName, final String header, final boolean aync) throws Exception {
    try (SQLWriter writer = new SQLWriter(fileName)) {
        //Define the max line width(default as 1500), which determines if split the SQL stmt as cross-lines
        SQLWriter.maxLineWidth=32767;
        writer.setAsyncMode(aync);
        writer.setFileHead(header);
        int count = writer.writeAll2SQL(rs, "", 1500);
        return count;
    }
}

//Convert CSV file to SQL file, returns number of records extracted
public int CSV2SQL(final ResultSet rs, final String SQLFileName, final String CSVfileName, final String header) throws Exception {
    try (SQLWriter writer = new SQLWriter(SQLFileName)) {
        writer.setFileHead(header);
        return writer.writeAll2SQL(CSVfileName, rs);
    }
}

//Fetch ResultSet into string array, be noted that the cell value can be null
//Parameter "rows" means the number of records to be extracted, -1 means unlimited
public Object[][] fetchResult(final ResultSet rs, final int rows) throws Exception {
    if (rs.getStatement().isClosed() || rs.isClosed()) throw new IOException("Statement is aborted.");
    ArrayList<Object[]> ary = (ArrayList) new ResultSetHelperService(rs).fetchRows(rows);
    return ary.toArray(new Object[][]{});
}

Performance

In async mode, the program spends very few time on converting fields and writing file.
Almost all the time is used on fetching data, so the performance highly depends on network speed.
And as a test result on Oracle database in guest virtual machine, to generate 2.15 million records into a file with 750 MB size, only takes extra 3 seconds on writing, total time cost is 25 seconds.

opencsv's People

Contributors

hyee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.