GithubHelp home page GithubHelp logo

isabella232 / pivotal-network-pg_csv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloudfoundry-attic/pivotal-network-pg_csv

0.0 0.0 0.0 144 KB

Fast AR/PostgreSQL csv export. Uses pg function 'copy to csv'. Effective on millions rows.

License: MIT License

Ruby 100.00%

pivotal-network-pg_csv's Introduction

PgCsv

Fast AR/PostgreSQL csv export. Uses pg function 'copy to csv'. Effective on millions rows.

Gemfile:

gem 'pg_csv'

Usage:

PgCsv.new(opts).export(to, opts)

'to' is a stream or filename

Options:

:sql        => "select p.* from users u, projects p where p.user_id = u.id order by email limit 10"
:connection => AR.connection
:delimiter  => ["\t", ",", ]
:header     => boolean, use pg header for fields?
:logger     => logger
:columns    => array of column names, ignore :header option
:encoding   => encoding (default is pg_default), list of encodings: http://www.postgresql.org/docs/8.4/static/multibyte.html#CHARSET-TABLE

:temp_file  => boolean, generate throught temp file? final file appears by mv
:temp_dir   => for :temp_file, ex: '/tmp'

:type       => :plain - return full string
            => :gzip  - save file to gzip
            => :stream - save to stream
            => :file - just save to file = default
            => :yield - return each row to block

Examples:

PgCsv.new(:sql => User.good.to_sql).export('a1.csv')
PgCsv.new(:sql => sql).export('a2.gz', :type => :gzip)
PgCsv.new(:sql => sql).export('a3.csv', :temp_file => true)
PgCsv.new(:sql => sql, :type => :plain).export
File.open("a4.csv", 'a'){|f| PgCsv.new(:sql => "select * from users").\
    export(f, :type => :stream) }
PgCsv.new(:sql => sql).export('a5.csv', :delimiter => "\t")
PgCsv.new(:sql => sql).export('a6.csv', :header => true)
PgCsv.new(:sql => sql).export('a7.csv', :columns => %w{id a b c})
PgCsv.new(:sql => sql, :connection => SomeDb.connection, :columns => %w{id a b c}, :delimiter => "|").\
    export('a8.gz', :type => :gzip, :temp_file => true)

# example collect from shards
Zlib::GzipWriter.open('some.gz') do |stream|
  e = PgCsv.new(:sql => sql, :type => :stream)
  ConnectionPool.each_shard do |connection|
    e.export(stream, :connection => connection)
  end
end

# yield example
PgCsv.new(:sql => sql, :type => :yield).export do |row|
  puts row  
end

pivotal-network-pg_csv's People

Contributors

kostya 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.