embulk-filter-json_key 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/LICENSE.txt +21 -0
- data/README.md +126 -0
- data/build.gradle +75 -0
- data/example/add_keys.yml +29 -0
- data/example/data.tsv +7 -0
- data/example/drop_keys.yml +26 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +6 -0
- data/gradlew +164 -0
- data/gradlew.bat +90 -0
- data/lib/embulk/filter/json_key.rb +3 -0
- data/src/main/java/org/embulk/filter/json_key/ColumnVisitorImpl.java +100 -0
- data/src/main/java/org/embulk/filter/json_key/FilteredPageOutput.java +60 -0
- data/src/main/java/org/embulk/filter/json_key/JsonKeyFilter.java +242 -0
- data/src/main/java/org/embulk/filter/json_key/JsonKeyFilterPlugin.java +69 -0
- data/src/test/java/org/embulk/filter/json_key/TestAddKeyFilter.java +30 -0
- data/src/test/java/org/embulk/filter/json_key/TestDropKeyFilter.java +30 -0
- data/src/test/java/org/embulk/filter/json_key/TestJsonKeyFilterPlugin.java +5 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 10ca802406bca013bdbab6acb7e5905398f6c6a9
|
4
|
+
data.tar.gz: 0e3539b540784dac7bea4a6d4c955e913910f4ba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3edb3f9854921e03142c802bad6e4b68b55449aef42d41f6fade97b2e39c637dcf955adade9e8aa623797849e44f1ca85065335eb69445fe2a19f30b35b050c1
|
7
|
+
data.tar.gz: 79a62c4ffef8cd77a64f5cf41ae4bdc3caa5e408508e5cac8b501cc6c726f50b0be7543667cfdbfea5e07603b4d6b194240e75a873000f69244571903b32804b
|
data/.gitignore
ADDED
data/LICENSE.txt
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# Json Key filter plugin for Embulk
|
2
|
+
|
3
|
+
Filtering keys from JSON objects.
|
4
|
+
|
5
|
+
## Overview
|
6
|
+
|
7
|
+
* **Plugin type**: filter
|
8
|
+
|
9
|
+
## Configuration
|
10
|
+
|
11
|
+
- **column**: column name of JSON (string, required)
|
12
|
+
- **nested_key_delimiter**: delimiter to express nested JSON keys (string, required)
|
13
|
+
- **add_keys**: JSON keys to add (array of hash, optional)
|
14
|
+
- **key**: key name to add (string)
|
15
|
+
- **value**: value of the key (anything)
|
16
|
+
- **drop_keys**: JSON keys to drop (array of string, optional)
|
17
|
+
- **key**: key name to add (string)
|
18
|
+
|
19
|
+
## Example Config
|
20
|
+
|
21
|
+
```yaml
|
22
|
+
filters:
|
23
|
+
- type: json_key
|
24
|
+
column: json_payload
|
25
|
+
nested_key_delimiter: "."
|
26
|
+
add_keys:
|
27
|
+
- {key: k1} # value is null
|
28
|
+
- {key: k2.k2, value: test_string}
|
29
|
+
- {key: k3.3, value: {test: test_object}}
|
30
|
+
drop_keys:
|
31
|
+
- {key: k5}
|
32
|
+
- {key: k6.k6-1}
|
33
|
+
- {key: k7.3}
|
34
|
+
```
|
35
|
+
|
36
|
+
## Example(add_keys)
|
37
|
+
Say input.tsv is as follows:
|
38
|
+
|
39
|
+
```
|
40
|
+
json_payload
|
41
|
+
{"phone_numbers":"1-276-220-7263","profile":{"like_words":["maiores","eum","aut"],"anniversary":{"voluptatem":"dolor","et":"ullam"}}}
|
42
|
+
{"phone_numbers":"553.980.4072","profile":{"like_words":["nobis","ad","est"],"anniversary": null}}
|
43
|
+
{"phone_numbers":"267-437-9081","profile":{"like_words":["itaque","aut","in"],"anniversary":{"eveniet":"in","id":"sit"}}}
|
44
|
+
{"phone_numbers":"639.217.7325","profile":{"like_words":["a","molestiae","iure"],"anniversary":{"non":"harum","dolorem":"provident"}}}
|
45
|
+
{"phone_numbers":"590.289.2473","profile":{"like_words":["quisquam","quasi","a"],"anniversary":{"ducimus":"veritatis","vel":"in"}}}
|
46
|
+
{"phone_numbers":"(196) 116-8976","profile":{"like_words":["maxime","ad","sunt"],"anniversary":{"molestiae":"architecto","temporibus":"quia"}}}
|
47
|
+
```
|
48
|
+
```yaml
|
49
|
+
filters:
|
50
|
+
- type: json_key
|
51
|
+
column: json_payload
|
52
|
+
nested_key_delimiter: "."
|
53
|
+
add_keys:
|
54
|
+
- {key: add_key1}
|
55
|
+
- {key: add_key2, value: add_value2}
|
56
|
+
- {key: add_key3, value: 3}
|
57
|
+
- {key: add_key4, value: [1,2,3,4]}
|
58
|
+
- {key: add_key5, value: {key1: value, key2: 2, key3: {key: value}}}
|
59
|
+
- {key: profile.add_key6, value: add_value6}
|
60
|
+
```
|
61
|
+
|
62
|
+
The result is:
|
63
|
+
|
64
|
+
```
|
65
|
+
{"phone_numbers":"1-276-220-7263","profile":{"like_words":["maiores","eum","aut"],"anniversary":{"voluptatem":"dolor","et":"ullam"},"add_key6":"add_value6"},"add_key1":null,"add_key2":"add_value2","add_key3":3,"add_key4":[1,2,3,4],"add_key5":{"key1":"value","key2":2,"key3":{"key":"value"}}}
|
66
|
+
{"phone_numbers":"553.980.4072","profile":{"like_words":["nobis","ad","est"],"anniversary":null,"add_key6":"add_value6"},"add_key1":null,"add_key2":"add_value2","add_key3":3,"add_key4":[1,2,3,4],"add_key5":{"key1":"value","key2":2,"key3":{"key":"value"}}}
|
67
|
+
{"phone_numbers":"267-437-9081","profile":{"like_words":["itaque","aut","in"],"anniversary":{"eveniet":"in","id":"sit"},"add_key6":"add_value6"},"add_key1":null,"add_key2":"add_value2","add_key3":3,"add_key4":[1,2,3,4],"add_key5":{"key1":"value","key2":2,"key3":{"key":"value"}}}
|
68
|
+
{"phone_numbers":"639.217.7325","profile":{"like_words":["a","molestiae","iure"],"anniversary":{"non":"harum","dolorem":"provident"},"add_key6":"add_value6"},"add_key1":null,"add_key2":"add_value2","add_key3":3,"add_key4":[1,2,3,4],"add_key5":{"key1":"value","key2":2,"key3":{"key":"value"}}}
|
69
|
+
{"phone_numbers":"590.289.2473","profile":{"like_words":["quisquam","quasi","a"],"anniversary":{"ducimus":"veritatis","vel":"in"},"add_key6":"add_value6"},"add_key1":null,"add_key2":"add_value2","add_key3":3,"add_key4":[1,2,3,4],"add_key5":{"key1":"value","key2":2,"key3":{"key":"value"}}}
|
70
|
+
{"phone_numbers":"(196) 116-8976","profile":{"like_words":["maxime","ad","sunt"],"anniversary":{"molestiae":"architecto","temporibus":"quia"},"add_key6":"add_value6"},"add_key1":null,"add_key2":"add_value2","add_key3":3,"add_key4":[1,2,3,4],"add_key5":{"key1":"value","key2":2,"key3":{"key":"value"}}}
|
71
|
+
```
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
### Example(drop_keys)
|
76
|
+
Say input.tsv is as follows:
|
77
|
+
|
78
|
+
```
|
79
|
+
json_payload
|
80
|
+
{"phone_numbers":"1-276-220-7263","profile":{"like_words":["maiores","eum","aut"],"anniversary":{"voluptatem":"dolor","et":"ullam"}}}
|
81
|
+
{"phone_numbers":"553.980.4072","profile":{"like_words":["nobis","ad","est"],"anniversary": null}}
|
82
|
+
{"phone_numbers":"267-437-9081","profile":{"like_words":["itaque","aut","in"],"anniversary":{"eveniet":"in","id":"sit"}}}
|
83
|
+
{"phone_numbers":"639.217.7325","profile":{"like_words":["a","molestiae","iure"],"anniversary":{"non":"harum","dolorem":"provident"}}}
|
84
|
+
{"phone_numbers":"590.289.2473","profile":{"like_words":["quisquam","quasi","a"],"anniversary":{"ducimus":"veritatis","vel":"in"}}}
|
85
|
+
{"phone_numbers":"(196) 116-8976","profile":{"like_words":["maxime","ad","sunt"],"anniversary":{"molestiae":"architecto","temporibus":"quia"}}}
|
86
|
+
```
|
87
|
+
```yaml
|
88
|
+
filters:
|
89
|
+
- type: json_key
|
90
|
+
column: json_payload
|
91
|
+
nested_key_delimiter: "."
|
92
|
+
drop_keys:
|
93
|
+
- {key: phone_numbers}
|
94
|
+
- {key: profile.like_words.2}
|
95
|
+
- {key: profile.anniversary.voluptatem}
|
96
|
+
```
|
97
|
+
|
98
|
+
The result is:
|
99
|
+
|
100
|
+
```
|
101
|
+
{"profile":{"like_words":["maiores","eum"],"anniversary":{"et":"ullam"}}}
|
102
|
+
{"profile":{"like_words":["nobis","ad"],"anniversary":null}}
|
103
|
+
{"profile":{"like_words":["itaque","aut"],"anniversary":{"eveniet":"in","id":"sit"}}}
|
104
|
+
{"profile":{"like_words":["a","molestiae"],"anniversary":{"non":"harum","dolorem":"provident"}}}
|
105
|
+
{"profile":{"like_words":["quisquam","quasi"],"anniversary":{"ducimus":"veritatis","vel":"in"}}}
|
106
|
+
{"profile":{"like_words":["maxime","ad"],"anniversary":{"molestiae":"architecto","temporibus":"quia"}}}
|
107
|
+
```
|
108
|
+
|
109
|
+
## TODO
|
110
|
+
|
111
|
+
- **add_keys** is overwriting existing values. need to **overwrite** option.
|
112
|
+
- **add_keys** cannot add keys array nodes.
|
113
|
+
- write tests
|
114
|
+
|
115
|
+
## Run Example
|
116
|
+
|
117
|
+
```
|
118
|
+
$ ./gradlew classpath
|
119
|
+
$ embulk run -Ilib example config.yml
|
120
|
+
```
|
121
|
+
|
122
|
+
## Build
|
123
|
+
|
124
|
+
```
|
125
|
+
$ ./gradlew gem # -t to watch change of files and rebuild continuously
|
126
|
+
```
|
data/build.gradle
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
plugins {
|
2
|
+
id "com.jfrog.bintray" version "1.1"
|
3
|
+
id "com.github.jruby-gradle.base" version "0.1.5"
|
4
|
+
id "java"
|
5
|
+
}
|
6
|
+
import com.github.jrubygradle.JRubyExec
|
7
|
+
repositories {
|
8
|
+
mavenCentral()
|
9
|
+
jcenter()
|
10
|
+
}
|
11
|
+
configurations {
|
12
|
+
provided
|
13
|
+
}
|
14
|
+
|
15
|
+
version = "0.0.1"
|
16
|
+
sourceCompatibility = 1.7
|
17
|
+
targetCompatibility = 1.7
|
18
|
+
|
19
|
+
dependencies {
|
20
|
+
compile "org.embulk:embulk-core:0.7.5"
|
21
|
+
provided "org.embulk:embulk-core:0.7.5"
|
22
|
+
// compile "YOUR_JAR_DEPENDENCY_GROUP:YOUR_JAR_DEPENDENCY_MODULE:YOUR_JAR_DEPENDENCY_VERSION"
|
23
|
+
testCompile "junit:junit:4.+"
|
24
|
+
}
|
25
|
+
|
26
|
+
task classpath(type: Copy, dependsOn: ["jar"]) {
|
27
|
+
doFirst { file("classpath").deleteDir() }
|
28
|
+
from (configurations.runtime - configurations.provided + files(jar.archivePath))
|
29
|
+
into "classpath"
|
30
|
+
}
|
31
|
+
clean { delete "classpath" }
|
32
|
+
|
33
|
+
task gem(type: JRubyExec, dependsOn: ["gemspec", "classpath"]) {
|
34
|
+
jrubyArgs "-rrubygems/gem_runner", "-eGem::GemRunner.new.run(ARGV)", "build"
|
35
|
+
script "${project.name}.gemspec"
|
36
|
+
doLast { ant.move(file: "${project.name}-${project.version}.gem", todir: "pkg") }
|
37
|
+
}
|
38
|
+
|
39
|
+
task gemPush(type: JRubyExec, dependsOn: ["gem"]) {
|
40
|
+
jrubyArgs "-rrubygems/gem_runner", "-eGem::GemRunner.new.run(ARGV)", "push"
|
41
|
+
script "pkg/${project.name}-${project.version}.gem"
|
42
|
+
}
|
43
|
+
|
44
|
+
task "package"(dependsOn: ["gemspec", "classpath"]) << {
|
45
|
+
println "> Build succeeded."
|
46
|
+
println "> You can run embulk with '-L ${file(".").absolutePath}' argument."
|
47
|
+
}
|
48
|
+
|
49
|
+
task gemspec {
|
50
|
+
ext.gemspecFile = file("${project.name}.gemspec")
|
51
|
+
inputs.file "build.gradle"
|
52
|
+
outputs.file gemspecFile
|
53
|
+
doLast { gemspecFile.write($/
|
54
|
+
Gem::Specification.new do |spec|
|
55
|
+
spec.name = "${project.name}"
|
56
|
+
spec.version = "${project.version}"
|
57
|
+
spec.authors = ["Civitaspo"]
|
58
|
+
spec.summary = %[Json Key filter plugin for Embulk]
|
59
|
+
spec.description = %[Json Key]
|
60
|
+
spec.email = ["civitaspo@gmail.com"]
|
61
|
+
spec.licenses = ["MIT"]
|
62
|
+
spec.homepage = "https://github.com/civitaspo/embulk-filter-json_key"
|
63
|
+
|
64
|
+
spec.files = `git ls-files`.split("\n") + Dir["classpath/*.jar"]
|
65
|
+
spec.test_files = spec.files.grep(%r"^(test|spec)/")
|
66
|
+
spec.require_paths = ["lib"]
|
67
|
+
|
68
|
+
#spec.add_dependency 'YOUR_GEM_DEPENDENCY', ['~> YOUR_GEM_DEPENDENCY_VERSION']
|
69
|
+
spec.add_development_dependency 'bundler', ['~> 1.0']
|
70
|
+
spec.add_development_dependency 'rake', ['>= 10.0']
|
71
|
+
end
|
72
|
+
/$)
|
73
|
+
}
|
74
|
+
}
|
75
|
+
clean { delete "${project.name}.gemspec" }
|
@@ -0,0 +1,29 @@
|
|
1
|
+
in:
|
2
|
+
type: file
|
3
|
+
path_prefix: example/data.tsv
|
4
|
+
parser:
|
5
|
+
type: csv
|
6
|
+
delimiter: "\t"
|
7
|
+
charset: UTF-8
|
8
|
+
newline: CRLF
|
9
|
+
null_string: 'NULL'
|
10
|
+
skip_header_lines: 1
|
11
|
+
comment_line_marker: '#'
|
12
|
+
columns:
|
13
|
+
- {name: json_payload, type: string}
|
14
|
+
|
15
|
+
filters:
|
16
|
+
- type: json_key
|
17
|
+
column: json_payload
|
18
|
+
nested_key_delimiter: "."
|
19
|
+
add_keys:
|
20
|
+
- {key: add_key1}
|
21
|
+
- {key: add_key2, value: add_value2}
|
22
|
+
- {key: add_key3, value: 3}
|
23
|
+
- {key: add_key4, value: [1,2,3,4]}
|
24
|
+
- {key: add_key5, value: {key1: value, key2: 2, key3: {key: value}}}
|
25
|
+
- {key: profile.add_key6, value: add_value6}
|
26
|
+
|
27
|
+
out:
|
28
|
+
type: stdout
|
29
|
+
|
data/example/data.tsv
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
json_payload
|
2
|
+
{"phone_numbers":"1-276-220-7263","profile":{"like_words":["maiores","eum","aut"],"anniversary":{"voluptatem":"dolor","et":"ullam"}}}
|
3
|
+
{"phone_numbers":"553.980.4072","profile":{"like_words":["nobis","ad","est"],"anniversary": null}}
|
4
|
+
{"phone_numbers":"267-437-9081","profile":{"like_words":["itaque","aut","in"],"anniversary":{"eveniet":"in","id":"sit"}}}
|
5
|
+
{"phone_numbers":"639.217.7325","profile":{"like_words":["a","molestiae","iure"],"anniversary":{"non":"harum","dolorem":"provident"}}}
|
6
|
+
{"phone_numbers":"590.289.2473","profile":{"like_words":["quisquam","quasi","a"],"anniversary":{"ducimus":"veritatis","vel":"in"}}}
|
7
|
+
{"phone_numbers":"(196) 116-8976","profile":{"like_words":["maxime","ad","sunt"],"anniversary":{"molestiae":"architecto","temporibus":"quia"}}}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
in:
|
2
|
+
type: file
|
3
|
+
path_prefix: example/data.tsv
|
4
|
+
parser:
|
5
|
+
type: csv
|
6
|
+
delimiter: "\t"
|
7
|
+
charset: UTF-8
|
8
|
+
newline: CRLF
|
9
|
+
null_string: 'NULL'
|
10
|
+
skip_header_lines: 1
|
11
|
+
comment_line_marker: '#'
|
12
|
+
columns:
|
13
|
+
- {name: json_payload, type: string}
|
14
|
+
|
15
|
+
filters:
|
16
|
+
- type: json_key
|
17
|
+
column: json_payload
|
18
|
+
nested_key_delimiter: "."
|
19
|
+
drop_keys:
|
20
|
+
- {key: phone_numbers}
|
21
|
+
- {key: profile.like_words.2}
|
22
|
+
- {key: profile.anniversary.voluptatem}
|
23
|
+
|
24
|
+
out:
|
25
|
+
type: stdout
|
26
|
+
|
Binary file
|
data/gradlew
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
##############################################################################
|
4
|
+
##
|
5
|
+
## Gradle start up script for UN*X
|
6
|
+
##
|
7
|
+
##############################################################################
|
8
|
+
|
9
|
+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
10
|
+
DEFAULT_JVM_OPTS=""
|
11
|
+
|
12
|
+
APP_NAME="Gradle"
|
13
|
+
APP_BASE_NAME=`basename "$0"`
|
14
|
+
|
15
|
+
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
16
|
+
MAX_FD="maximum"
|
17
|
+
|
18
|
+
warn ( ) {
|
19
|
+
echo "$*"
|
20
|
+
}
|
21
|
+
|
22
|
+
die ( ) {
|
23
|
+
echo
|
24
|
+
echo "$*"
|
25
|
+
echo
|
26
|
+
exit 1
|
27
|
+
}
|
28
|
+
|
29
|
+
# OS specific support (must be 'true' or 'false').
|
30
|
+
cygwin=false
|
31
|
+
msys=false
|
32
|
+
darwin=false
|
33
|
+
case "`uname`" in
|
34
|
+
CYGWIN* )
|
35
|
+
cygwin=true
|
36
|
+
;;
|
37
|
+
Darwin* )
|
38
|
+
darwin=true
|
39
|
+
;;
|
40
|
+
MINGW* )
|
41
|
+
msys=true
|
42
|
+
;;
|
43
|
+
esac
|
44
|
+
|
45
|
+
# For Cygwin, ensure paths are in UNIX format before anything is touched.
|
46
|
+
if $cygwin ; then
|
47
|
+
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
|
48
|
+
fi
|
49
|
+
|
50
|
+
# Attempt to set APP_HOME
|
51
|
+
# Resolve links: $0 may be a link
|
52
|
+
PRG="$0"
|
53
|
+
# Need this for relative symlinks.
|
54
|
+
while [ -h "$PRG" ] ; do
|
55
|
+
ls=`ls -ld "$PRG"`
|
56
|
+
link=`expr "$ls" : '.*-> \(.*\)$'`
|
57
|
+
if expr "$link" : '/.*' > /dev/null; then
|
58
|
+
PRG="$link"
|
59
|
+
else
|
60
|
+
PRG=`dirname "$PRG"`"/$link"
|
61
|
+
fi
|
62
|
+
done
|
63
|
+
SAVED="`pwd`"
|
64
|
+
cd "`dirname \"$PRG\"`/" >&-
|
65
|
+
APP_HOME="`pwd -P`"
|
66
|
+
cd "$SAVED" >&-
|
67
|
+
|
68
|
+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
69
|
+
|
70
|
+
# Determine the Java command to use to start the JVM.
|
71
|
+
if [ -n "$JAVA_HOME" ] ; then
|
72
|
+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
73
|
+
# IBM's JDK on AIX uses strange locations for the executables
|
74
|
+
JAVACMD="$JAVA_HOME/jre/sh/java"
|
75
|
+
else
|
76
|
+
JAVACMD="$JAVA_HOME/bin/java"
|
77
|
+
fi
|
78
|
+
if [ ! -x "$JAVACMD" ] ; then
|
79
|
+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
80
|
+
|
81
|
+
Please set the JAVA_HOME variable in your environment to match the
|
82
|
+
location of your Java installation."
|
83
|
+
fi
|
84
|
+
else
|
85
|
+
JAVACMD="java"
|
86
|
+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
87
|
+
|
88
|
+
Please set the JAVA_HOME variable in your environment to match the
|
89
|
+
location of your Java installation."
|
90
|
+
fi
|
91
|
+
|
92
|
+
# Increase the maximum file descriptors if we can.
|
93
|
+
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
|
94
|
+
MAX_FD_LIMIT=`ulimit -H -n`
|
95
|
+
if [ $? -eq 0 ] ; then
|
96
|
+
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
97
|
+
MAX_FD="$MAX_FD_LIMIT"
|
98
|
+
fi
|
99
|
+
ulimit -n $MAX_FD
|
100
|
+
if [ $? -ne 0 ] ; then
|
101
|
+
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
102
|
+
fi
|
103
|
+
else
|
104
|
+
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
105
|
+
fi
|
106
|
+
fi
|
107
|
+
|
108
|
+
# For Darwin, add options to specify how the application appears in the dock
|
109
|
+
if $darwin; then
|
110
|
+
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
111
|
+
fi
|
112
|
+
|
113
|
+
# For Cygwin, switch paths to Windows format before running java
|
114
|
+
if $cygwin ; then
|
115
|
+
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
116
|
+
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
117
|
+
|
118
|
+
# We build the pattern for arguments to be converted via cygpath
|
119
|
+
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
120
|
+
SEP=""
|
121
|
+
for dir in $ROOTDIRSRAW ; do
|
122
|
+
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
123
|
+
SEP="|"
|
124
|
+
done
|
125
|
+
OURCYGPATTERN="(^($ROOTDIRS))"
|
126
|
+
# Add a user-defined pattern to the cygpath arguments
|
127
|
+
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
128
|
+
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
129
|
+
fi
|
130
|
+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
131
|
+
i=0
|
132
|
+
for arg in "$@" ; do
|
133
|
+
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
134
|
+
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
135
|
+
|
136
|
+
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
137
|
+
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
138
|
+
else
|
139
|
+
eval `echo args$i`="\"$arg\""
|
140
|
+
fi
|
141
|
+
i=$((i+1))
|
142
|
+
done
|
143
|
+
case $i in
|
144
|
+
(0) set -- ;;
|
145
|
+
(1) set -- "$args0" ;;
|
146
|
+
(2) set -- "$args0" "$args1" ;;
|
147
|
+
(3) set -- "$args0" "$args1" "$args2" ;;
|
148
|
+
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
149
|
+
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
150
|
+
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
151
|
+
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
152
|
+
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
153
|
+
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
154
|
+
esac
|
155
|
+
fi
|
156
|
+
|
157
|
+
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
158
|
+
function splitJvmOpts() {
|
159
|
+
JVM_OPTS=("$@")
|
160
|
+
}
|
161
|
+
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
162
|
+
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
163
|
+
|
164
|
+
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
|
data/gradlew.bat
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
@if "%DEBUG%" == "" @echo off
|
2
|
+
@rem ##########################################################################
|
3
|
+
@rem
|
4
|
+
@rem Gradle startup script for Windows
|
5
|
+
@rem
|
6
|
+
@rem ##########################################################################
|
7
|
+
|
8
|
+
@rem Set local scope for the variables with windows NT shell
|
9
|
+
if "%OS%"=="Windows_NT" setlocal
|
10
|
+
|
11
|
+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
12
|
+
set DEFAULT_JVM_OPTS=
|
13
|
+
|
14
|
+
set DIRNAME=%~dp0
|
15
|
+
if "%DIRNAME%" == "" set DIRNAME=.
|
16
|
+
set APP_BASE_NAME=%~n0
|
17
|
+
set APP_HOME=%DIRNAME%
|
18
|
+
|
19
|
+
@rem Find java.exe
|
20
|
+
if defined JAVA_HOME goto findJavaFromJavaHome
|
21
|
+
|
22
|
+
set JAVA_EXE=java.exe
|
23
|
+
%JAVA_EXE% -version >NUL 2>&1
|
24
|
+
if "%ERRORLEVEL%" == "0" goto init
|
25
|
+
|
26
|
+
echo.
|
27
|
+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
28
|
+
echo.
|
29
|
+
echo Please set the JAVA_HOME variable in your environment to match the
|
30
|
+
echo location of your Java installation.
|
31
|
+
|
32
|
+
goto fail
|
33
|
+
|
34
|
+
:findJavaFromJavaHome
|
35
|
+
set JAVA_HOME=%JAVA_HOME:"=%
|
36
|
+
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
37
|
+
|
38
|
+
if exist "%JAVA_EXE%" goto init
|
39
|
+
|
40
|
+
echo.
|
41
|
+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
42
|
+
echo.
|
43
|
+
echo Please set the JAVA_HOME variable in your environment to match the
|
44
|
+
echo location of your Java installation.
|
45
|
+
|
46
|
+
goto fail
|
47
|
+
|
48
|
+
:init
|
49
|
+
@rem Get command-line arguments, handling Windowz variants
|
50
|
+
|
51
|
+
if not "%OS%" == "Windows_NT" goto win9xME_args
|
52
|
+
if "%@eval[2+2]" == "4" goto 4NT_args
|
53
|
+
|
54
|
+
:win9xME_args
|
55
|
+
@rem Slurp the command line arguments.
|
56
|
+
set CMD_LINE_ARGS=
|
57
|
+
set _SKIP=2
|
58
|
+
|
59
|
+
:win9xME_args_slurp
|
60
|
+
if "x%~1" == "x" goto execute
|
61
|
+
|
62
|
+
set CMD_LINE_ARGS=%*
|
63
|
+
goto execute
|
64
|
+
|
65
|
+
:4NT_args
|
66
|
+
@rem Get arguments from the 4NT Shell from JP Software
|
67
|
+
set CMD_LINE_ARGS=%$
|
68
|
+
|
69
|
+
:execute
|
70
|
+
@rem Setup the command line
|
71
|
+
|
72
|
+
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
73
|
+
|
74
|
+
@rem Execute Gradle
|
75
|
+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
76
|
+
|
77
|
+
:end
|
78
|
+
@rem End local scope for the variables with windows NT shell
|
79
|
+
if "%ERRORLEVEL%"=="0" goto mainEnd
|
80
|
+
|
81
|
+
:fail
|
82
|
+
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
83
|
+
rem the _cmd.exe /c_ return code!
|
84
|
+
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
85
|
+
exit /b 1
|
86
|
+
|
87
|
+
:mainEnd
|
88
|
+
if "%OS%"=="Windows_NT" endlocal
|
89
|
+
|
90
|
+
:omega
|
@@ -0,0 +1,100 @@
|
|
1
|
+
package org.embulk.filter.json_key;
|
2
|
+
|
3
|
+
import com.google.common.base.Throwables;
|
4
|
+
import org.embulk.spi.Column;
|
5
|
+
import org.embulk.spi.ColumnVisitor;
|
6
|
+
import org.embulk.spi.Exec;
|
7
|
+
import org.embulk.spi.PageBuilder;
|
8
|
+
import org.embulk.spi.PageReader;
|
9
|
+
import org.slf4j.Logger;
|
10
|
+
|
11
|
+
import java.io.IOException;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Created by takahiro.nakayama on 10/28/15.
|
15
|
+
*/
|
16
|
+
class ColumnVisitorImpl
|
17
|
+
implements ColumnVisitor
|
18
|
+
{
|
19
|
+
private final Logger logger = Exec.getLogger(ColumnVisitorImpl.class);
|
20
|
+
private final PageReader pageReader;
|
21
|
+
private final PageBuilder pageBuilder;
|
22
|
+
private final String filterColumnName;
|
23
|
+
private final JsonKeyFilter filter;
|
24
|
+
|
25
|
+
public ColumnVisitorImpl(PageReader pageReader, PageBuilder pageBuilder, JsonKeyFilter filter, String filterColumnName)
|
26
|
+
{
|
27
|
+
this.pageReader = pageReader;
|
28
|
+
this.pageBuilder = pageBuilder;
|
29
|
+
this.filterColumnName = filterColumnName;
|
30
|
+
this.filter = filter;
|
31
|
+
}
|
32
|
+
|
33
|
+
@Override
|
34
|
+
public void booleanColumn(Column outputColumn) {
|
35
|
+
if (pageReader.isNull(outputColumn)) {
|
36
|
+
pageBuilder.setNull(outputColumn);
|
37
|
+
}
|
38
|
+
else {
|
39
|
+
pageBuilder.setBoolean(outputColumn, pageReader.getBoolean(outputColumn));
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
@Override
|
44
|
+
public void longColumn(Column outputColumn) {
|
45
|
+
if (pageReader.isNull(outputColumn)) {
|
46
|
+
pageBuilder.setNull(outputColumn);
|
47
|
+
}
|
48
|
+
else {
|
49
|
+
pageBuilder.setLong(outputColumn, pageReader.getLong(outputColumn));
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
@Override
|
54
|
+
public void doubleColumn(Column outputColumn) {
|
55
|
+
if (pageReader.isNull(outputColumn)) {
|
56
|
+
pageBuilder.setNull(outputColumn);
|
57
|
+
}
|
58
|
+
else {
|
59
|
+
pageBuilder.setDouble(outputColumn, pageReader.getDouble(outputColumn));
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
@Override
|
64
|
+
public void stringColumn(Column outputColumn) {
|
65
|
+
if (outputColumn.getName().contentEquals(filterColumnName)) {
|
66
|
+
String output = null;
|
67
|
+
try {
|
68
|
+
output = filter.doFilter(pageReader.getString(outputColumn));
|
69
|
+
}
|
70
|
+
catch (IOException e) {
|
71
|
+
Throwables.propagate(e);
|
72
|
+
}
|
73
|
+
|
74
|
+
if (output == null) {
|
75
|
+
pageBuilder.setNull(outputColumn);
|
76
|
+
}
|
77
|
+
else {
|
78
|
+
pageBuilder.setString(outputColumn, output);
|
79
|
+
}
|
80
|
+
}
|
81
|
+
else {
|
82
|
+
if (pageReader.isNull(outputColumn)) {
|
83
|
+
pageBuilder.setNull(outputColumn);
|
84
|
+
}
|
85
|
+
else {
|
86
|
+
pageBuilder.setString(outputColumn, pageReader.getString(outputColumn));
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
@Override
|
92
|
+
public void timestampColumn(Column outputColumn) {
|
93
|
+
if (pageReader.isNull(outputColumn)) {
|
94
|
+
pageBuilder.setNull(outputColumn);
|
95
|
+
}
|
96
|
+
else {
|
97
|
+
pageBuilder.setTimestamp(outputColumn, pageReader.getTimestamp(outputColumn));
|
98
|
+
}
|
99
|
+
}
|
100
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
package org.embulk.filter.json_key;
|
2
|
+
|
3
|
+
import org.embulk.spi.Exec;
|
4
|
+
import org.embulk.spi.Page;
|
5
|
+
import org.embulk.spi.PageBuilder;
|
6
|
+
import org.embulk.spi.PageOutput;
|
7
|
+
import org.embulk.spi.PageReader;
|
8
|
+
import org.embulk.spi.Schema;
|
9
|
+
import org.slf4j.Logger;
|
10
|
+
|
11
|
+
import static org.embulk.filter.json_key.JsonKeyFilterPlugin.*;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Created by takahiro.nakayama on 10/28/15.
|
15
|
+
*/
|
16
|
+
class FilteredPageOutput
|
17
|
+
implements PageOutput
|
18
|
+
{
|
19
|
+
private final Logger logger = Exec.getLogger(FilteredPageOutput.class);
|
20
|
+
private final PageReader pageReader;
|
21
|
+
private final PageBuilder pageBuilder;
|
22
|
+
private final PageOutput pageOutput;
|
23
|
+
private final Schema outputSchema;
|
24
|
+
private final ColumnVisitorImpl columnVisitor;
|
25
|
+
|
26
|
+
public FilteredPageOutput(PluginTask task, Schema inputSchema, Schema outputSchema, PageOutput pageOutput)
|
27
|
+
{
|
28
|
+
this.pageReader = new PageReader(inputSchema);
|
29
|
+
this.pageBuilder = new PageBuilder(Exec.getBufferAllocator(), outputSchema, pageOutput);
|
30
|
+
this.pageOutput = pageOutput;
|
31
|
+
this.outputSchema = outputSchema;
|
32
|
+
this.columnVisitor = new ColumnVisitorImpl(pageReader, pageBuilder, new JsonKeyFilter(task), task.getColumnName());
|
33
|
+
}
|
34
|
+
|
35
|
+
@Override
|
36
|
+
public void add(Page page)
|
37
|
+
{
|
38
|
+
pageReader.setPage(page);
|
39
|
+
|
40
|
+
while (pageReader.nextRecord()) {
|
41
|
+
outputSchema.visitColumns(columnVisitor);
|
42
|
+
pageBuilder.addRecord();
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
@Override
|
47
|
+
public void finish()
|
48
|
+
{
|
49
|
+
pageBuilder.finish();
|
50
|
+
pageOutput.finish();
|
51
|
+
}
|
52
|
+
|
53
|
+
@Override
|
54
|
+
public void close()
|
55
|
+
{
|
56
|
+
pageReader.close();
|
57
|
+
pageBuilder.close();
|
58
|
+
pageOutput.close();
|
59
|
+
}
|
60
|
+
}
|
@@ -0,0 +1,242 @@
|
|
1
|
+
package org.embulk.filter.json_key;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.databind.JsonNode;
|
4
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
+
import com.fasterxml.jackson.databind.node.ArrayNode;
|
6
|
+
import com.fasterxml.jackson.databind.node.ObjectNode;
|
7
|
+
import com.google.common.base.Splitter;
|
8
|
+
import com.google.common.collect.ImmutableList;
|
9
|
+
import com.google.common.collect.Lists;
|
10
|
+
import org.apache.commons.lang3.math.NumberUtils;
|
11
|
+
import org.embulk.filter.json_key.JsonKeyFilterPlugin.PluginTask;
|
12
|
+
import org.embulk.spi.Exec;
|
13
|
+
import org.slf4j.Logger;
|
14
|
+
|
15
|
+
import java.io.IOException;
|
16
|
+
import java.util.List;
|
17
|
+
|
18
|
+
import static org.embulk.filter.json_key.JsonKeyFilterPlugin.KeyConfig;
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Created by takahiro.nakayama on 10/28/15.
|
22
|
+
*/
|
23
|
+
class JsonKeyFilter
|
24
|
+
{
|
25
|
+
private final Logger logger = Exec.getLogger(JsonKeyFilter.class);
|
26
|
+
private final ObjectMapper mapper = new ObjectMapper();
|
27
|
+
|
28
|
+
private final AddKeyFilter addKeyFilter;
|
29
|
+
private final DropKeyFilter dropKeyFilter;
|
30
|
+
|
31
|
+
JsonKeyFilter(PluginTask task)
|
32
|
+
{
|
33
|
+
this.addKeyFilter = new AddKeyFilter(task.getAddKeyConfigs(), task.getNestedKeyDelimiter());
|
34
|
+
this.dropKeyFilter = new DropKeyFilter(task.getDropKeyConfigs(), task.getNestedKeyDelimiter());
|
35
|
+
}
|
36
|
+
|
37
|
+
public String doFilter(String json)
|
38
|
+
throws IOException
|
39
|
+
{
|
40
|
+
JsonNode rootNode = mapper.readTree(json);
|
41
|
+
JsonNode filteredNode = addKeyFilter.doFilter(rootNode);
|
42
|
+
filteredNode = dropKeyFilter.doFilter(filteredNode);
|
43
|
+
|
44
|
+
return mapper.writeValueAsString(filteredNode);
|
45
|
+
}
|
46
|
+
|
47
|
+
private abstract class AbstractKeyFilter
|
48
|
+
{
|
49
|
+
private final Logger logger = Exec.getLogger(AbstractKeyFilter.class);
|
50
|
+
private final List<ImmutableList<String>> keys;
|
51
|
+
private final List<JsonNode> values;
|
52
|
+
|
53
|
+
private List<ImmutableList<String>> newKeys(List<KeyConfig> keyConfigs, String delimiter)
|
54
|
+
{
|
55
|
+
ImmutableList.Builder<ImmutableList<String>> builder = ImmutableList.builder();
|
56
|
+
for (KeyConfig keyConfig : keyConfigs) {
|
57
|
+
builder.add(ImmutableList.copyOf(Lists.newArrayList(Splitter.on(delimiter).split(keyConfig.getKey()))));
|
58
|
+
}
|
59
|
+
return builder.build();
|
60
|
+
}
|
61
|
+
|
62
|
+
private List<JsonNode> newValues(List<KeyConfig> keyConfigs)
|
63
|
+
{
|
64
|
+
ImmutableList.Builder<JsonNode> builder = ImmutableList.builder();
|
65
|
+
for (KeyConfig keyConfig : keyConfigs) {
|
66
|
+
JsonNode value = mapper.getNodeFactory().nullNode();
|
67
|
+
if (keyConfig.getValue().isPresent()) {
|
68
|
+
value = mapper.getNodeFactory().pojoNode(keyConfig.getValue().get());
|
69
|
+
}
|
70
|
+
builder.add(value);
|
71
|
+
}
|
72
|
+
return builder.build();
|
73
|
+
}
|
74
|
+
|
75
|
+
AbstractKeyFilter(List<KeyConfig> keyConfigs, String nestedKeyDelimiter)
|
76
|
+
{
|
77
|
+
this.keys = newKeys(keyConfigs, nestedKeyDelimiter);
|
78
|
+
this.values = newValues(keyConfigs);
|
79
|
+
}
|
80
|
+
|
81
|
+
public abstract JsonNode doFilter(JsonNode rootNode);
|
82
|
+
}
|
83
|
+
|
84
|
+
private class AddKeyFilter
|
85
|
+
extends AbstractKeyFilter
|
86
|
+
{
|
87
|
+
AddKeyFilter(List<KeyConfig> keyConfigs, String nestedKeyDelimiter)
|
88
|
+
{
|
89
|
+
super(keyConfigs, nestedKeyDelimiter);
|
90
|
+
}
|
91
|
+
|
92
|
+
@Override
|
93
|
+
public JsonNode doFilter(JsonNode rootNode)
|
94
|
+
{
|
95
|
+
JsonNode filteredJsonNode = rootNode;
|
96
|
+
for (int i = 0; i < super.keys.size(); i++) {
|
97
|
+
filteredJsonNode = addKey(rootNode, super.keys.get(i), super.values.get(i));
|
98
|
+
}
|
99
|
+
return filteredJsonNode;
|
100
|
+
}
|
101
|
+
|
102
|
+
// TODO: addKey is overwriting existing values.
|
103
|
+
private JsonNode addKey(JsonNode node, List<String> nestedKey, JsonNode value)
|
104
|
+
{
|
105
|
+
if (node.isObject()) {
|
106
|
+
return addKeyToObject(node, nestedKey, value);
|
107
|
+
}
|
108
|
+
else if (node.isArray()) {
|
109
|
+
return addKeyToArray(node, nestedKey, value);
|
110
|
+
}
|
111
|
+
// TODO: if NullNode, need to create nodes?
|
112
|
+
// else if (node.isNull()) {
|
113
|
+
// return addKeyToNull(node, nestedKey, value);
|
114
|
+
// }
|
115
|
+
else {
|
116
|
+
return node;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
private JsonNode addKeyToObject(JsonNode node, List<String> nestedKey, JsonNode value)
|
121
|
+
{
|
122
|
+
ObjectNode object = (ObjectNode) node;
|
123
|
+
if (nestedKey.isEmpty()) {
|
124
|
+
return object;
|
125
|
+
}
|
126
|
+
else if (nestedKey.size() == 1) {
|
127
|
+
object.set(nestedKey.get(0), value);
|
128
|
+
return object;
|
129
|
+
}
|
130
|
+
else {
|
131
|
+
String parentKey = nestedKey.get(0);
|
132
|
+
List<String> newNestedKey = nestedKey.subList(1, nestedKey.size());
|
133
|
+
JsonNode newNode = addKey(object.get(parentKey), newNestedKey, value);
|
134
|
+
object.set(parentKey, newNode);
|
135
|
+
return object;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
// TODO: addKeyToArray cannot add not existing index.
|
140
|
+
private JsonNode addKeyToArray(JsonNode node, List<String> nestedKey, JsonNode value)
|
141
|
+
{
|
142
|
+
ArrayNode object = (ArrayNode) node;
|
143
|
+
if (nestedKey.isEmpty()) {
|
144
|
+
return object;
|
145
|
+
}
|
146
|
+
else if (nestedKey.size() == 1) {
|
147
|
+
String key = nestedKey.get(0);
|
148
|
+
if (NumberUtils.isNumber(key)) {
|
149
|
+
object.set(Integer.parseInt(key), value);
|
150
|
+
}
|
151
|
+
return object;
|
152
|
+
}
|
153
|
+
else {
|
154
|
+
String parentIdx = nestedKey.get(0);
|
155
|
+
List<String> newNestedKey = nestedKey.subList(1, nestedKey.size());
|
156
|
+
if (NumberUtils.isNumber(parentIdx)) {
|
157
|
+
int idx = Integer.parseInt(parentIdx);
|
158
|
+
JsonNode newNode = addKey(object.get(idx), newNestedKey, value);
|
159
|
+
object.set(idx, newNode);
|
160
|
+
}
|
161
|
+
return object;
|
162
|
+
}
|
163
|
+
}
|
164
|
+
}
|
165
|
+
|
166
|
+
private class DropKeyFilter
|
167
|
+
extends AbstractKeyFilter
|
168
|
+
{
|
169
|
+
DropKeyFilter(List<KeyConfig> keyConfigs, String nestedKeyDelimiter)
|
170
|
+
{
|
171
|
+
super(keyConfigs, nestedKeyDelimiter);
|
172
|
+
}
|
173
|
+
|
174
|
+
@Override
|
175
|
+
public JsonNode doFilter(JsonNode rootNode)
|
176
|
+
{
|
177
|
+
JsonNode filteredJsonNode = rootNode;
|
178
|
+
for (List<String> nestedKey : super.keys) {
|
179
|
+
filteredJsonNode = dropKey(rootNode, nestedKey);
|
180
|
+
}
|
181
|
+
return filteredJsonNode;
|
182
|
+
}
|
183
|
+
|
184
|
+
// if NullNode has come before end of a nested key, return NullNode not EmptyObjectNode.
|
185
|
+
private JsonNode dropKey(JsonNode node, List<String> nestedKey)
|
186
|
+
{
|
187
|
+
if (node.isObject()) {
|
188
|
+
return dropKeyFromObject(node, nestedKey);
|
189
|
+
}
|
190
|
+
else if (node.isArray()) {
|
191
|
+
return dropKeyFromArray(node, nestedKey);
|
192
|
+
}
|
193
|
+
else {
|
194
|
+
return node;
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
private JsonNode dropKeyFromObject(JsonNode node, List<String> nestedKey)
|
199
|
+
{
|
200
|
+
ObjectNode object = (ObjectNode) node;
|
201
|
+
if (nestedKey.isEmpty()) {
|
202
|
+
return object;
|
203
|
+
}
|
204
|
+
else if (nestedKey.size() == 1) {
|
205
|
+
object.remove(nestedKey.get(0));
|
206
|
+
return object;
|
207
|
+
}
|
208
|
+
else {
|
209
|
+
String parentKey = nestedKey.get(0);
|
210
|
+
List<String> newNestedKey = nestedKey.subList(1, nestedKey.size());
|
211
|
+
JsonNode newNode = dropKey(object.get(parentKey), newNestedKey);
|
212
|
+
object.set(parentKey, newNode);
|
213
|
+
return object;
|
214
|
+
}
|
215
|
+
}
|
216
|
+
|
217
|
+
private JsonNode dropKeyFromArray(JsonNode node, List<String> nestedKey)
|
218
|
+
{
|
219
|
+
ArrayNode object = (ArrayNode) node;
|
220
|
+
if (nestedKey.isEmpty()) {
|
221
|
+
return object;
|
222
|
+
}
|
223
|
+
else if (nestedKey.size() == 1) {
|
224
|
+
String key = nestedKey.get(0);
|
225
|
+
if (NumberUtils.isNumber(key)) {
|
226
|
+
object.remove(Integer.parseInt(key));
|
227
|
+
}
|
228
|
+
return object;
|
229
|
+
}
|
230
|
+
else {
|
231
|
+
String parentIdx = nestedKey.get(0);
|
232
|
+
List<String> newNestedKey = nestedKey.subList(1, nestedKey.size());
|
233
|
+
if (NumberUtils.isNumber(parentIdx)) {
|
234
|
+
int idx = Integer.parseInt(parentIdx);
|
235
|
+
JsonNode newNode = dropKey(object.get(idx), newNestedKey);
|
236
|
+
object.set(idx, newNode);
|
237
|
+
}
|
238
|
+
return object;
|
239
|
+
}
|
240
|
+
}
|
241
|
+
}
|
242
|
+
}
|
@@ -0,0 +1,69 @@
|
|
1
|
+
package org.embulk.filter.json_key;
|
2
|
+
|
3
|
+
import com.google.common.base.Optional;
|
4
|
+
import org.embulk.config.Config;
|
5
|
+
import org.embulk.config.ConfigDefault;
|
6
|
+
import org.embulk.config.ConfigSource;
|
7
|
+
import org.embulk.config.Task;
|
8
|
+
import org.embulk.config.TaskSource;
|
9
|
+
import org.embulk.spi.Exec;
|
10
|
+
import org.embulk.spi.FilterPlugin;
|
11
|
+
import org.embulk.spi.PageOutput;
|
12
|
+
import org.embulk.spi.Schema;
|
13
|
+
import org.slf4j.Logger;
|
14
|
+
|
15
|
+
import java.util.List;
|
16
|
+
|
17
|
+
public class JsonKeyFilterPlugin
|
18
|
+
implements FilterPlugin
|
19
|
+
{
|
20
|
+
public interface KeyConfig
|
21
|
+
extends Task
|
22
|
+
{
|
23
|
+
@Config("key")
|
24
|
+
public String getKey();
|
25
|
+
|
26
|
+
@Config("value")
|
27
|
+
@ConfigDefault("null")
|
28
|
+
public Optional<Object> getValue();
|
29
|
+
}
|
30
|
+
|
31
|
+
public interface PluginTask
|
32
|
+
extends Task
|
33
|
+
{
|
34
|
+
@Config("column")
|
35
|
+
public String getColumnName();
|
36
|
+
|
37
|
+
@Config("nested_key_delimiter")
|
38
|
+
@ConfigDefault("\".\"")
|
39
|
+
public String getNestedKeyDelimiter();
|
40
|
+
|
41
|
+
@Config("add_keys")
|
42
|
+
@ConfigDefault("[]")
|
43
|
+
public List<KeyConfig> getAddKeyConfigs();
|
44
|
+
|
45
|
+
@Config("drop_keys")
|
46
|
+
@ConfigDefault("[]")
|
47
|
+
public List<KeyConfig> getDropKeyConfigs();
|
48
|
+
}
|
49
|
+
|
50
|
+
private final Logger logger = Exec.getLogger(JsonKeyFilterPlugin.class);
|
51
|
+
|
52
|
+
@Override
|
53
|
+
public void transaction(ConfigSource config, Schema inputSchema, FilterPlugin.Control control)
|
54
|
+
{
|
55
|
+
PluginTask task = config.loadConfig(PluginTask.class);
|
56
|
+
|
57
|
+
Schema outputSchema = inputSchema;
|
58
|
+
|
59
|
+
control.run(task.dump(), outputSchema);
|
60
|
+
}
|
61
|
+
|
62
|
+
@Override
|
63
|
+
public PageOutput open(TaskSource taskSource, Schema inputSchema, Schema outputSchema, PageOutput output)
|
64
|
+
{
|
65
|
+
PluginTask task = taskSource.loadTask(PluginTask.class);
|
66
|
+
|
67
|
+
return new FilteredPageOutput(task, inputSchema, outputSchema, output);
|
68
|
+
}
|
69
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
package org.embulk.filter.json_key;
|
2
|
+
|
3
|
+
import junit.framework.TestCase;
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Created by takahiro.nakayama on 10/29/15.
|
7
|
+
*/
|
8
|
+
public class TestAddKeyFilter
|
9
|
+
extends TestCase
|
10
|
+
{
|
11
|
+
|
12
|
+
public void setUp()
|
13
|
+
throws Exception
|
14
|
+
{
|
15
|
+
super.setUp();
|
16
|
+
|
17
|
+
}
|
18
|
+
|
19
|
+
public void tearDown()
|
20
|
+
throws Exception
|
21
|
+
{
|
22
|
+
|
23
|
+
}
|
24
|
+
|
25
|
+
public void testDoFilter()
|
26
|
+
throws Exception
|
27
|
+
{
|
28
|
+
|
29
|
+
}
|
30
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
package org.embulk.filter.json_key;
|
2
|
+
|
3
|
+
import junit.framework.TestCase;
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Created by takahiro.nakayama on 10/29/15.
|
7
|
+
*/
|
8
|
+
public class TestDropKeyFilter
|
9
|
+
extends TestCase
|
10
|
+
{
|
11
|
+
|
12
|
+
public void setUp()
|
13
|
+
throws Exception
|
14
|
+
{
|
15
|
+
super.setUp();
|
16
|
+
|
17
|
+
}
|
18
|
+
|
19
|
+
public void tearDown()
|
20
|
+
throws Exception
|
21
|
+
{
|
22
|
+
|
23
|
+
}
|
24
|
+
|
25
|
+
public void testDoFilter()
|
26
|
+
throws Exception
|
27
|
+
{
|
28
|
+
|
29
|
+
}
|
30
|
+
}
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: embulk-filter-json_key
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Civitaspo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
requirement: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '1.0'
|
25
|
+
prerelease: false
|
26
|
+
type: :development
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '10.0'
|
39
|
+
prerelease: false
|
40
|
+
type: :development
|
41
|
+
description: Json Key
|
42
|
+
email:
|
43
|
+
- civitaspo@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.md
|
51
|
+
- build.gradle
|
52
|
+
- example/add_keys.yml
|
53
|
+
- example/data.tsv
|
54
|
+
- example/drop_keys.yml
|
55
|
+
- gradle/wrapper/gradle-wrapper.jar
|
56
|
+
- gradle/wrapper/gradle-wrapper.properties
|
57
|
+
- gradlew
|
58
|
+
- gradlew.bat
|
59
|
+
- lib/embulk/filter/json_key.rb
|
60
|
+
- src/main/java/org/embulk/filter/json_key/ColumnVisitorImpl.java
|
61
|
+
- src/main/java/org/embulk/filter/json_key/FilteredPageOutput.java
|
62
|
+
- src/main/java/org/embulk/filter/json_key/JsonKeyFilter.java
|
63
|
+
- src/main/java/org/embulk/filter/json_key/JsonKeyFilterPlugin.java
|
64
|
+
- src/test/java/org/embulk/filter/json_key/TestAddKeyFilter.java
|
65
|
+
- src/test/java/org/embulk/filter/json_key/TestDropKeyFilter.java
|
66
|
+
- src/test/java/org/embulk/filter/json_key/TestJsonKeyFilterPlugin.java
|
67
|
+
- classpath/embulk-filter-json_key-0.0.1.jar
|
68
|
+
homepage: https://github.com/civitaspo/embulk-filter-json_key
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.1.9
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Json Key filter plugin for Embulk
|
92
|
+
test_files: []
|