embulk-parser-json 0.0.1
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 +7 -0
- data/.gitignore +10 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +2 -0
- data/embulk-parser-json.gemspec +22 -0
- data/lib/embulk/parser/json.rb +64 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6aa19505fd68d24404a80cf3b403ce606e96debf
|
4
|
+
data.tar.gz: bf8f54eeb5a03754bec3264c2f468922e0dd2a0d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e1a69df98a31380ccc7672b477d9ef71af8d1f08becc449b81bf4fadc2cce1ac85ce789bfbd6dd3ce3c053e9e10d6ad7953592659c4b556ed7ae3d22da5038c1
|
7
|
+
data.tar.gz: 0fa843f6faf5a62007e80d22817636777ea02408f5c666806d97740a9b5923a143fe14a81aa36cead2c5517ec625e06b5c491d88812b86fc7f2cd0dbf7b166a0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 TODO: Write your name
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Json parser plugin for Embulk
|
2
|
+
|
3
|
+
Parser plugin for [Embulk](https://github.com/embulk/embulk).
|
4
|
+
|
5
|
+
Read data from input as json and fetch each entries by [jsonpath](http://goessner.net/articles/JsonPath/) to output.
|
6
|
+
|
7
|
+
## Overview
|
8
|
+
|
9
|
+
* **Plugin type**: parser
|
10
|
+
* **Load all or nothing**: yes
|
11
|
+
* **Resume supported**: no
|
12
|
+
|
13
|
+
|
14
|
+
## Configuration
|
15
|
+
|
16
|
+
```yaml
|
17
|
+
parser:
|
18
|
+
type: json
|
19
|
+
root: $.response.station
|
20
|
+
schema:
|
21
|
+
- {name: name, type: string}
|
22
|
+
- {name: next, type: string}
|
23
|
+
- {name: prev, type: string}
|
24
|
+
- {name: distance, type: string}
|
25
|
+
- {name: lat, type: double, path: x}
|
26
|
+
- {name: lng, type: double, path: y}
|
27
|
+
- {name: line, type: string}
|
28
|
+
- {name: postal, type: string}
|
29
|
+
```
|
30
|
+
|
31
|
+
- **type**: specify this plugin as `json`
|
32
|
+
- **root**: root property to start fetching each entries, specify in [jsonpath](http://goessner.net/articles/JsonPath/) style, required
|
33
|
+
- **schema**: specify the attribute of table and data type, required
|
34
|
+
|
35
|
+
|
36
|
+
## Example
|
37
|
+
|
38
|
+
```json
|
39
|
+
{
|
40
|
+
"result" : "success",
|
41
|
+
"students" : [
|
42
|
+
{ "name" : "John", "age" : 10 },
|
43
|
+
{ "name" : "Paul", "age" : 16 },
|
44
|
+
{ "name" : "George", "age" : 17 },
|
45
|
+
{ "name" : "Ringo", "age" : 18 }
|
46
|
+
]
|
47
|
+
}
|
48
|
+
```
|
49
|
+
|
50
|
+
### Simple schema
|
51
|
+
|
52
|
+
You can iterate "students" node by the following condifuration:
|
53
|
+
|
54
|
+
```yaml
|
55
|
+
root: $.students
|
56
|
+
schema:
|
57
|
+
- {name: name, type: string}
|
58
|
+
- {name: age, type: long}
|
59
|
+
```
|
60
|
+
|
61
|
+
### Handle more complicated json
|
62
|
+
|
63
|
+
|
64
|
+
If you want to handle more complicated json like as follows:
|
65
|
+
|
66
|
+
```json
|
67
|
+
{
|
68
|
+
"result" : "success",
|
69
|
+
"students" : [
|
70
|
+
{ "names" : ["John", "Lennon"], "age" : 10 },
|
71
|
+
{ "names" : ["Paul", "Maccartney"], "age" : 10 }
|
72
|
+
]
|
73
|
+
}
|
74
|
+
```
|
75
|
+
|
76
|
+
you can specify jsonpath to also **path** in schema section:
|
77
|
+
|
78
|
+
```yaml
|
79
|
+
root: $.students
|
80
|
+
schema:
|
81
|
+
- {name: firstName, type: string, path: "names[0]"}
|
82
|
+
- {name: lastName, type: string, path: "names[1]"}
|
83
|
+
```
|
84
|
+
|
85
|
+
In this case, names[0] will be firstName of schema and names[1] will be lastName.
|
86
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "embulk-parser-json"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Takuma kanari"]
|
9
|
+
spec.email = ["chemtrails.t@gmail.com"]
|
10
|
+
spec.summary = %q{Embulk parser plugin for json}
|
11
|
+
spec.description = %q{Json parser plugin is Embulk plugin to fetch entries in json format.}
|
12
|
+
spec.homepage = "https://github.com/takumakanari/embulk-parser-json"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "jsonpath", "~> 0.5"
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.0"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "jsonpath"
|
2
|
+
|
3
|
+
module Embulk
|
4
|
+
module Parser
|
5
|
+
|
6
|
+
class JsonParserPlugin < ParserPlugin
|
7
|
+
Plugin.register_parser("json", self)
|
8
|
+
|
9
|
+
def self.transaction(config, &control)
|
10
|
+
task = {
|
11
|
+
:schema => config.param("schema", :array),
|
12
|
+
:root => config.param("root", :string)
|
13
|
+
}
|
14
|
+
columns = task[:schema].each_with_index.map do |c, i|
|
15
|
+
Column.new(i, c["name"], c["type"].to_sym)
|
16
|
+
end
|
17
|
+
yield(task, columns)
|
18
|
+
end
|
19
|
+
|
20
|
+
def run(file_input)
|
21
|
+
rootPath = JsonPath.new(@task["root"])
|
22
|
+
schema = @task["schema"]
|
23
|
+
while file = file_input.next_file
|
24
|
+
rootPath.on(file.read).flatten.each do |e|
|
25
|
+
@page_builder.add(make_record(schema, e))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
@page_builder.finish
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def make_record(schema, e)
|
34
|
+
schema.map do |c|
|
35
|
+
name = c["name"]
|
36
|
+
path = c["path"]
|
37
|
+
val = path.nil? ? e[name] : find_by_path(e, path)
|
38
|
+
|
39
|
+
v = val.nil? ? "" : val
|
40
|
+
type = c["type"]
|
41
|
+
case type
|
42
|
+
when "string"
|
43
|
+
v
|
44
|
+
when "long"
|
45
|
+
v.to_i
|
46
|
+
when "double"
|
47
|
+
v.to_f
|
48
|
+
when "boolean"
|
49
|
+
["yes", "true", "1"].include?(v.downcase)
|
50
|
+
when "timestamp"
|
51
|
+
v.empty? ? nil : Time.strptime(v, c["format"])
|
52
|
+
else
|
53
|
+
raise "Unsupported type #{type}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def find_by_path(e, path)
|
59
|
+
JsonPath.on(e, path).first
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: embulk-parser-json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takuma kanari
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jsonpath
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.5'
|
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.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
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: Json parser plugin is Embulk plugin to fetch entries in json format.
|
56
|
+
email:
|
57
|
+
- chemtrails.t@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- embulk-parser-json.gemspec
|
68
|
+
- lib/embulk/parser/json.rb
|
69
|
+
homepage: https://github.com/takumakanari/embulk-parser-json
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.2.2
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: Embulk parser plugin for json
|
93
|
+
test_files: []
|