embulk-input-bigquery 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8993211376746215694e0c70cee753dd6751444a
4
- data.tar.gz: 70d9952fbd03e1567e6fec62fa8410e02212095f
3
+ metadata.gz: b7f8b54f5c0bf602236407494d3c0bf35257e04d
4
+ data.tar.gz: bd1116a46dc6016dc58cc536d69e1b71c0f80242
5
5
  SHA512:
6
- metadata.gz: 8e8c18cdbc6bcda320cc998ea594e6c20d2679d1c8aebcd85a7557ace7de3d7ce0f04e47685dee947bd591f9a7072ab3900c2c332d1764a9ce859c8ffed69385
7
- data.tar.gz: 2692842d7386fc9d40026a12635017eb477495064ae0d3ecfe1f645645245232f82dc00670cb2f5c305679f4a7eb01e5915c86f60574a75fb00aebb40d504b2f
6
+ metadata.gz: dfcfe921546bc8d89e2df091056247f3a1b1a1eb17590eea4f512d7eb3f8856fe5f778c071b281f75e8349a4af93e2a8a3131aeadbf5b8893acb2bbea93fb547
7
+ data.tar.gz: 9f00dd42dc3219ab1f75a445609cc73419f36c87c4ef5ab98e00954bc990c8560f42f3fc75208037d542ef3767c54b6356932f9bdb68ff45286c0007d4b05e85
data/README.md CHANGED
@@ -4,17 +4,9 @@ This is Embulk input plugin from Bigquery.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
7
+ install it yourself as:
8
8
 
9
- gem 'embulk-input-bigquery'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install embulk-input-bigquery
9
+ $ embulk gem install embulk-input-bigquery
18
10
 
19
11
  ## Usage
20
12
 
@@ -30,3 +22,20 @@ in:
30
22
  out:
31
23
  type: stdout
32
24
  ```
25
+
26
+ If, table name is changeable, then
27
+
28
+ ```
29
+ in:
30
+ type: bigquery
31
+ project: 'project-name'
32
+ keyfile: '/home/hogehoge/bigquery-keyfile.json'
33
+ sql: 'SELECT price,category_id FROM [ecsite.products_<%= params["date"].strftime("%Y%m") %>] GROUP BY category_id'
34
+ erb_params:
35
+ date: "require 'date'; (Date.today - 1)"
36
+ columns:
37
+ - {name: price, type: long}
38
+ - {name: category_id, type: long}
39
+ - {name: month, type: timestamp, format: '%Y-%m', eval: 'require "time"; Time.parse(params["date"]).to_i'}
40
+ ```
41
+
@@ -1,5 +1,6 @@
1
1
  require "embulk/input/bigquery/version"
2
2
  require "google/cloud/bigquery"
3
+ require 'erb'
3
4
 
4
5
  module Embulk
5
6
  module Input
@@ -7,8 +8,27 @@ module Embulk
7
8
  Plugin.register_input('bigquery', self)
8
9
 
9
10
  def self.transaction(config, &control)
11
+ sql = config[:sql]
12
+ params = {}
13
+ unless sql
14
+ sql_erb = config[:sql_erb]
15
+ erb = ERB.new(sql_erb)
16
+ erb_params = config[:erb_params]
17
+ erb_params.each do |k, v|
18
+ params[k] = eval(v)
19
+ end
20
+
21
+ sql = erb.result(binding)
22
+ end
23
+
24
+ task = {
25
+ project: config[:project],
26
+ keyfile: config[:keyfile],
27
+ sql: sql,
28
+ columns: config[:columns],
29
+ params: params
30
+ }
10
31
 
11
- task = {project: config[:project], keyfile: config[:keyfile], sql: config[:sql], columns: config[:columns]}
12
32
  columns = []
13
33
  config[:columns].each_with_index do |c, i|
14
34
  columns << Column.new(i, c['name'], c['type'].to_sym)
@@ -21,12 +41,18 @@ module Embulk
21
41
 
22
42
  def run
23
43
  bq = Google::Cloud::Bigquery.new(project: @task[:project], keyfile: @task[:keyfile])
44
+ params = @task[:params]
24
45
  rows = bq.query(@task[:sql])
25
46
  rows.each do |row|
26
47
  columns = []
27
48
  @task[:columns].each do |c|
28
- columns << row[c['name']]
49
+ val = row[c['name']]
50
+ if c['eval']
51
+ val = eval(c['eval'], binding)
52
+ end
53
+ columns << val
29
54
  end
55
+
30
56
  @page_builder.add(columns)
31
57
  end
32
58
  @page_builder.finish
@@ -1,7 +1,7 @@
1
1
  module Embulk
2
2
  module Input
3
3
  module Bigquery
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-input-bigquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takeru Narita