embulk-filter-unpivot 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c352fdf1a5783a814f232d2f55b2c022773efea0
4
+ data.tar.gz: f35ef1645e26bb9b792ca4d81fc2023517f0d76c
5
+ SHA512:
6
+ metadata.gz: 4ccddd4b316e27a62ab9f9a2791ffe4788983bbbdf61ed3c025fa77e4ed865315c9e703e0ee9ed237e65872b82bb7652055ad9c4fa356fe62cab4504834bda44
7
+ data.tar.gz: 982258ee75bdf68317ecb9fd054450ae1abf1e92a999938332cbad45b7ac509efc466e38d2d7e9ae4a92cfc923169a6eac0ecb1d91848d2837d55de814249c18
@@ -0,0 +1,5 @@
1
+ *~
2
+ /pkg/
3
+ /tmp/
4
+ /.bundle/
5
+ /Gemfile.lock
@@ -0,0 +1 @@
1
+ jruby-9.1.5.0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org/'
2
+ gemspec
@@ -0,0 +1,21 @@
1
+
2
+ MIT License
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # Unpivot filter plugin for Embulk
2
+
3
+ [Embulk](https://github.com/embulk/embulk/) filter plugin to nomalize fields.
4
+
5
+ 「横持ち」のテーブル構造から「縦持ち」のテーブルに変換するembulkフィルタ。
6
+
7
+ ## Overview
8
+
9
+ * **Plugin type**: filter
10
+
11
+ 設備が1列に並んでいる不動産物件データを縦持ちに変換する例
12
+
13
+ * 変換元物件データ
14
+
15
+ |物件ID|家賃|ペット可|駐車場付き|バス・トイレ別|エアコン|
16
+ |---|---|---|---|---|---|
17
+ |1|8.5|0|0|1|1|
18
+ |2|5.5|0|0|0|0|
19
+ |3|10.0|1|0|1|1|
20
+ |4|15.5|0|1|1|1|
21
+
22
+ * 設備コード表
23
+
24
+ |設備ID|設備|
25
+ |---|---|
26
+ |1|ペット可|
27
+ |2|駐車場付き|
28
+ |3|バス・トイレ別|
29
+ |4|エアコン|
30
+
31
+ *↓*
32
+
33
+ * 変換後データ
34
+
35
+ |物件ID|設備ID|
36
+ |---|---|
37
+ |1|3|
38
+ |1|4|
39
+ |3|1|
40
+ |3|3|
41
+ |3|4|
42
+ |4|2|
43
+ |4|3|
44
+ |4|4|
45
+
46
+ ## Configuration
47
+
48
+ - **outer_key**: normalization record id field name(string, required)
49
+ - **inner_key**: master id field name (string, required)  
50
+ - **columns**: master key/value set (array, default: [])  
51
+ - **additional**: additional field (array, default: [])
52
+
53
+ ## Example
54
+
55
+ ```
56
+ filters:
57
+ - type: column
58
+ add_columns:
59
+      - {name: 設備ID, type: long, default: 0}
60
+      - {name: created_at, type: timestamp, src: '物件情報入力日'}
61
+ - {name: updated_at, type: timestamp, src: '物件情報更新日'}
62
+ - type: unpivot
63
+    outer_key: 物件ID
64
+    inner_key: 設備ID
65
+ additional: [created_at, updated_at]
66
+ columns:
67
+      - {name: 'ペット可', id: 1}
68
+      - {name: '駐車場付き', id: 2}
69
+      - {name: 'バス・トイレ別', id: 3}
70
+      - {name: 'エアコン', id: 4}
71
+ ```
72
+
73
+ ## Build
74
+
75
+ ```
76
+ $ rake
77
+ ```
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task default: :build
@@ -0,0 +1,20 @@
1
+
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "embulk-filter-unpivot"
4
+ spec.version = "0.1.0"
5
+ spec.authors = ["takemi.ohama"]
6
+ spec.summary = "Unpivot filter plugin for Embulk"
7
+ spec.description = "Unpivot"
8
+ spec.email = ["engineer@ietty.co.jp"]
9
+ spec.licenses = ["MIT"]
10
+ # TODO set this: spec.homepage = "https://github.com/YOUR_NAME/embulk-filter-unpivot"
11
+
12
+ spec.files = `git ls-files`.split("\n") + Dir["classpath/*.jar"]
13
+ spec.test_files = spec.files.grep(%r{^(test|spec)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ #spec.add_dependency 'YOUR_GEM_DEPENDENCY', ['~> YOUR_GEM_DEPENDENCY_VERSION']
17
+ spec.add_development_dependency 'embulk', ['>= 0.8.18']
18
+ spec.add_development_dependency 'bundler', ['>= 1.10.6']
19
+ spec.add_development_dependency 'rake', ['>= 10.0']
20
+ end
@@ -0,0 +1,82 @@
1
+ #require 'pry'
2
+ #require 'pry-nav'
3
+
4
+ module Embulk
5
+ module Filter
6
+
7
+ class Unpivot < FilterPlugin
8
+ Plugin.register_filter("unpivot", self)
9
+
10
+ def self.transaction(config, in_schema, &control)
11
+
12
+ task = {
13
+ "outer_key" => config.param("outer_key", :string),
14
+ "inner_key" => config.param("inner_key", :string),
15
+ "columns" => config.param("columns", :array, default: []),
16
+ "additional" => config.param("additional", :array, default: []),
17
+ }
18
+
19
+ out_columns = [
20
+ Column.new(nil, task["outer_key"], :string),
21
+ Column.new(nil, task["inner_key"], :string),
22
+ ]
23
+
24
+ task["additional"].each do |name|
25
+ col = in_schema.find { |sch| sch.name == name }
26
+ col.index = nil
27
+ out_columns.push(col)
28
+ end
29
+
30
+ yield(task, out_columns)
31
+ end
32
+
33
+ def init
34
+ # initialization code:
35
+ @idx_outer = get_index(task["outer_key"], in_schema)
36
+ @idx_inner = get_index(task["inner_key"], in_schema)
37
+ @indexes = {}
38
+ task["additional"].each do |name|
39
+ @indexes[name] = get_index(name, in_schema)
40
+ end
41
+ task["columns"].each do |target|
42
+ @indexes[target["name"]] = get_index(target["name"], in_schema)
43
+ end
44
+ end
45
+
46
+ def close
47
+ end
48
+
49
+ def add(page)
50
+ page.each do |record|
51
+ new_record = []
52
+ new_record.push(record[@idx_outer])
53
+ new_record.push(record[@idx_inner])
54
+
55
+ task["additional"].each do |name|
56
+ new_record.push(record[@indexes[name]])
57
+ end
58
+
59
+ task["columns"].each do |target|
60
+ index = @indexes[target["name"]]
61
+ next if record[index] == 0 or record[index] == ''
62
+ new_record[1] = target["id"]
63
+ page_builder.add(new_record)
64
+ end
65
+ end
66
+ end
67
+
68
+ def finish
69
+ page_builder.finish
70
+ end
71
+
72
+ private
73
+
74
+ def get_index(name, schema)
75
+ col = schema.select{|c| c.name == name }
76
+ col[0].index
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: embulk-filter-unpivot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - takemi.ohama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: embulk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.18
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.18
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.10.6
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.10.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Unpivot
56
+ email:
57
+ - engineer@ietty.co.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".ruby-version"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - embulk-filter-unpivot.gemspec
69
+ - lib/embulk/filter/unpivot.rb
70
+ homepage:
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.5.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Unpivot filter plugin for Embulk
94
+ test_files: []