pg_stream 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +31 -0
- data/LICENSE +22 -0
- data/README.md +76 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/pg_stream/processor.rb +34 -0
- data/lib/pg_stream/stream.rb +40 -0
- data/lib/pg_stream/version.rb +3 -0
- data/lib/pg_stream.rb +5 -0
- data/pg_stream.gemspec +25 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e17576eec6868271a775e28d9faa54b9f9c4a355
|
4
|
+
data.tar.gz: 39d171f0127d2e661257603ea0ad84eec7d53bb1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa07dd59490ed44f222f63087f7fbfdb22f8cf2d3e378087a2530096346c1fc8f40d39a4ac64681c423ea615125292331486bd6a7bb6913516836afd7de60b29
|
7
|
+
data.tar.gz: 7cb30df117098486ed0dc1b7635da9aa8b75b31c28d7505b06cdf2a20f7402f682407c82251a8b02f8da487c04eef7b59886d9072ea1ad20a54bec7e83026887
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pg_stream (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
rake (10.4.2)
|
11
|
+
rspec (3.0.0)
|
12
|
+
rspec-core (~> 3.0.0)
|
13
|
+
rspec-expectations (~> 3.0.0)
|
14
|
+
rspec-mocks (~> 3.0.0)
|
15
|
+
rspec-core (3.0.4)
|
16
|
+
rspec-support (~> 3.0.0)
|
17
|
+
rspec-expectations (3.0.4)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.0.0)
|
20
|
+
rspec-mocks (3.0.4)
|
21
|
+
rspec-support (~> 3.0.0)
|
22
|
+
rspec-support (3.0.4)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
bundler (~> 1.9)
|
29
|
+
pg_stream!
|
30
|
+
rake (~> 10.0)
|
31
|
+
rspec (~> 3.0.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Lumos Labs
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# PgStream
|
2
|
+
|
3
|
+
PgStream allows you to stream data from Postgres or Redshift, and provides a framework to help you process it. Normally, Postgres and Redshift collect the entire result set in memory and then return it to you at once. PgStream allows you to process the result row by row, which may be the only way to handle very large results.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
You must be using PostgreSQL 9.2beta3 or later client libraries. You must be using pg 0.18.2 or later.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'pg_stream'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install pg_stream
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To get started, just pass it a PG::Connection and a query body string:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'pg'
|
31
|
+
|
32
|
+
body = 'SELECT * FROM huge_table;'
|
33
|
+
|
34
|
+
conn = PG::Connection.open(:dbname => 'test')
|
35
|
+
query_stream = PgStream::Stream.new(conn, body)
|
36
|
+
|
37
|
+
# You can get the headers from the Stream object.
|
38
|
+
headers = query_stream.headers
|
39
|
+
```
|
40
|
+
|
41
|
+
You use the Processor to consume the data from the db. The processor will register `before_execute`, `during_execute` and `after_execute` callbacks. The processor yields the `row` and `row_count` to the `during_execute` callback, and the `row_count` to the `after_execute` callback.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
stream_processor = PgStream::Processor.new(query_stream)
|
45
|
+
|
46
|
+
def setup_csv
|
47
|
+
lambda do
|
48
|
+
@csv = CSV.open('some_filepath', 'w')
|
49
|
+
@csv << headers
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
stream_processor.register(
|
54
|
+
{
|
55
|
+
before_execute: setup_csv,
|
56
|
+
during_execute: ->(row, _row_count) { @csv << row },
|
57
|
+
after_execute: ->(_row_count) { @csv.close }
|
58
|
+
}
|
59
|
+
)
|
60
|
+
```
|
61
|
+
|
62
|
+
You may call register multiple times on a single Processor.
|
63
|
+
|
64
|
+
To make the Processor run and yield to your callbacks, just call `execute` on it. It will return the row_count when it is done. Keep in mind this may take seconds or minutes depending on the size of your result, so you will probably want to do this in a background job.
|
65
|
+
|
66
|
+
```
|
67
|
+
row_count = stream_processor.execute
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it ( https://github.com/[my-github-username]/pg_stream/fork )
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pg_stream"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module PgStream
|
2
|
+
class Processor
|
3
|
+
CALLBACK_TYPES = [:before_execute, :during_execute, :after_execute]
|
4
|
+
|
5
|
+
def initialize(stream)
|
6
|
+
@stream = stream
|
7
|
+
@callbacks = CALLBACK_TYPES.map do |type|
|
8
|
+
[type, []]
|
9
|
+
end.to_h
|
10
|
+
@row_count = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def register(args)
|
14
|
+
args.each do |type, function|
|
15
|
+
if CALLBACK_TYPES.include?(type)
|
16
|
+
@callbacks[type] << function
|
17
|
+
else
|
18
|
+
raise "#{type} is not an acceptable callback type. Types include #{CALLBACK_TYPES.join(', ')}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@callbacks
|
22
|
+
end
|
23
|
+
|
24
|
+
def execute
|
25
|
+
@callbacks[:before_execute].each(&:call)
|
26
|
+
@stream.each_row do |row|
|
27
|
+
@row_count += 1
|
28
|
+
@callbacks[:during_execute].each { |y| y.call(row, @row_count) }
|
29
|
+
end
|
30
|
+
@callbacks[:after_execute].each { |y| y.call(@row_count) }
|
31
|
+
@row_count
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module PgStream
|
2
|
+
class Stream
|
3
|
+
def initialize(db_conn, body)
|
4
|
+
@db_conn = db_conn
|
5
|
+
@db_conn.send_query(body)
|
6
|
+
@db_conn.set_single_row_mode
|
7
|
+
end
|
8
|
+
|
9
|
+
def headers
|
10
|
+
@headers ||= get_headers
|
11
|
+
end
|
12
|
+
|
13
|
+
def each_row(&block)
|
14
|
+
headers # ensure that headers has been called so we have the first row
|
15
|
+
return unless @first_row # dont yield any rows if the query returns no results
|
16
|
+
yield(@first_row)
|
17
|
+
|
18
|
+
loop do
|
19
|
+
res = @db_conn.get_result || break
|
20
|
+
res.check
|
21
|
+
res.each_row do |row|
|
22
|
+
yield(row)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def get_headers
|
30
|
+
first_row_result = @db_conn.get_result
|
31
|
+
first_row_result.check
|
32
|
+
|
33
|
+
# values is method on the result object, which can have many rows,
|
34
|
+
# but which will only return one row in single_row_mode
|
35
|
+
@first_row = first_row_result.values[0]
|
36
|
+
|
37
|
+
first_row_result.fields
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/pg_stream.rb
ADDED
data/pg_stream.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pg_stream/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'pg_stream'
|
8
|
+
spec.version = PgStream::VERSION
|
9
|
+
spec.authors = ['Rob Froetscher', 'Andrew Xue']
|
10
|
+
spec.email = ['rfroetscher@lumoslabs.com', 'andrew@lumoslabs.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Stream data from postgres or Redshift}
|
13
|
+
spec.homepage = 'https://github.com/lumoslabs/pg_stream'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = 'exe'
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.9'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0.0'
|
23
|
+
|
24
|
+
spec.add_dependency 'pg', '>= 0.18.2'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_stream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Froetscher
|
8
|
+
- Andrew Xue
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-12-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.9'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.9'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 3.0.0
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 3.0.0
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: pg
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.18.2
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.18.2
|
70
|
+
description:
|
71
|
+
email:
|
72
|
+
- rfroetscher@lumoslabs.com
|
73
|
+
- andrew@lumoslabs.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/pg_stream.rb
|
88
|
+
- lib/pg_stream/processor.rb
|
89
|
+
- lib/pg_stream/stream.rb
|
90
|
+
- lib/pg_stream/version.rb
|
91
|
+
- pg_stream.gemspec
|
92
|
+
homepage: https://github.com/lumoslabs/pg_stream
|
93
|
+
licenses: []
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.2.3
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Stream data from postgres or Redshift
|
115
|
+
test_files: []
|
116
|
+
has_rdoc:
|