embulk-filter-reverse_geocoding 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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/build.gradle +110 -0
- data/config/checkstyle/checkstyle.xml +128 -0
- data/config/checkstyle/default.xml +108 -0
- data/example/config.yml +30 -0
- data/example/test.csv +9 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +6 -0
- data/gradlew +160 -0
- data/gradlew.bat +90 -0
- data/lib/embulk/filter/reverse_geocoding.rb +3 -0
- data/src/main/java/org/embulk/filter/reverse_geocoding/GeoCodeMap.java +80 -0
- data/src/main/java/org/embulk/filter/reverse_geocoding/ReverseGeoCodingFilterPlugin.java +266 -0
- data/src/main/resources/jpcities-master/geohash4.csv +628 -0
- data/src/main/resources/jpcities-master/geohash5.csv +20010 -0
- data/src/test/java/org/embulk/filter/reverse_geocoding/TestReverseGeocodingFilterPlugin.java +191 -0
- data/src/test/resources/jpcities-master/geohash4.csv +628 -0
- data/src/test/resources/jpcities-master/geohash5.csv +20010 -0
- metadata +96 -0
data/example/config.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
in:
|
2
|
+
type: file
|
3
|
+
path_prefix: /Users/devlop/github/embulk-filter-reverse_geocoding/example/test.csv
|
4
|
+
parser:
|
5
|
+
charset: UTF-8
|
6
|
+
newline: CRLF
|
7
|
+
type: csv
|
8
|
+
delimiter: ','
|
9
|
+
quote: '"'
|
10
|
+
escape: '"'
|
11
|
+
trim_if_not_quoted: false
|
12
|
+
skip_header_lines: 1
|
13
|
+
columns:
|
14
|
+
- {name: status, type: string}
|
15
|
+
- {name: address, type: string}
|
16
|
+
- {name: lat, type: double}
|
17
|
+
- {name: lon, type: double}
|
18
|
+
filters:
|
19
|
+
- type: column
|
20
|
+
drop_columns:
|
21
|
+
- {name: status}
|
22
|
+
- type: reverse_geocoding
|
23
|
+
target_lon: lon
|
24
|
+
target_lat: lat
|
25
|
+
level: 5
|
26
|
+
output_columns:
|
27
|
+
- {name: pref, type: string, out: pref}
|
28
|
+
- {name: city, type: string, out: city}
|
29
|
+
- {name: hash, type: string, out: hash}
|
30
|
+
out: {type: stdout}
|
data/example/test.csv
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
status,address,lat,lon
|
2
|
+
OK,東京都,1,1
|
3
|
+
OK,東京駅,35.681298,139.766247
|
4
|
+
OK,金閣寺,35.03937,135.729243
|
5
|
+
OK,首里城,26.217014,127.719521
|
6
|
+
OK,札幌市時計台,43.062562,141.35365
|
7
|
+
OK,東京都千代田区1−1−1,35.6721277,139.75891209999997
|
8
|
+
OK,東京都港区六本木3-2-1 六本木グランドタワー,35.6641222,139.729426
|
9
|
+
OK,ニューヨーク,40.712784,-74.005941
|
Binary file
|
data/gradlew
ADDED
@@ -0,0 +1,160 @@
|
|
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
|
+
# Attempt to set APP_HOME
|
46
|
+
# Resolve links: $0 may be a link
|
47
|
+
PRG="$0"
|
48
|
+
# Need this for relative symlinks.
|
49
|
+
while [ -h "$PRG" ] ; do
|
50
|
+
ls=`ls -ld "$PRG"`
|
51
|
+
link=`expr "$ls" : '.*-> \(.*\)$'`
|
52
|
+
if expr "$link" : '/.*' > /dev/null; then
|
53
|
+
PRG="$link"
|
54
|
+
else
|
55
|
+
PRG=`dirname "$PRG"`"/$link"
|
56
|
+
fi
|
57
|
+
done
|
58
|
+
SAVED="`pwd`"
|
59
|
+
cd "`dirname \"$PRG\"`/" >/dev/null
|
60
|
+
APP_HOME="`pwd -P`"
|
61
|
+
cd "$SAVED" >/dev/null
|
62
|
+
|
63
|
+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
64
|
+
|
65
|
+
# Determine the Java command to use to start the JVM.
|
66
|
+
if [ -n "$JAVA_HOME" ] ; then
|
67
|
+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
68
|
+
# IBM's JDK on AIX uses strange locations for the executables
|
69
|
+
JAVACMD="$JAVA_HOME/jre/sh/java"
|
70
|
+
else
|
71
|
+
JAVACMD="$JAVA_HOME/bin/java"
|
72
|
+
fi
|
73
|
+
if [ ! -x "$JAVACMD" ] ; then
|
74
|
+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
75
|
+
|
76
|
+
Please set the JAVA_HOME variable in your environment to match the
|
77
|
+
location of your Java installation."
|
78
|
+
fi
|
79
|
+
else
|
80
|
+
JAVACMD="java"
|
81
|
+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
82
|
+
|
83
|
+
Please set the JAVA_HOME variable in your environment to match the
|
84
|
+
location of your Java installation."
|
85
|
+
fi
|
86
|
+
|
87
|
+
# Increase the maximum file descriptors if we can.
|
88
|
+
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
|
89
|
+
MAX_FD_LIMIT=`ulimit -H -n`
|
90
|
+
if [ $? -eq 0 ] ; then
|
91
|
+
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
92
|
+
MAX_FD="$MAX_FD_LIMIT"
|
93
|
+
fi
|
94
|
+
ulimit -n $MAX_FD
|
95
|
+
if [ $? -ne 0 ] ; then
|
96
|
+
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
97
|
+
fi
|
98
|
+
else
|
99
|
+
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
100
|
+
fi
|
101
|
+
fi
|
102
|
+
|
103
|
+
# For Darwin, add options to specify how the application appears in the dock
|
104
|
+
if $darwin; then
|
105
|
+
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
106
|
+
fi
|
107
|
+
|
108
|
+
# For Cygwin, switch paths to Windows format before running java
|
109
|
+
if $cygwin ; then
|
110
|
+
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
111
|
+
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
112
|
+
JAVACMD=`cygpath --unix "$JAVACMD"`
|
113
|
+
|
114
|
+
# We build the pattern for arguments to be converted via cygpath
|
115
|
+
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
116
|
+
SEP=""
|
117
|
+
for dir in $ROOTDIRSRAW ; do
|
118
|
+
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
119
|
+
SEP="|"
|
120
|
+
done
|
121
|
+
OURCYGPATTERN="(^($ROOTDIRS))"
|
122
|
+
# Add a user-defined pattern to the cygpath arguments
|
123
|
+
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
124
|
+
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
125
|
+
fi
|
126
|
+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
127
|
+
i=0
|
128
|
+
for arg in "$@" ; do
|
129
|
+
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
130
|
+
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
131
|
+
|
132
|
+
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
133
|
+
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
134
|
+
else
|
135
|
+
eval `echo args$i`="\"$arg\""
|
136
|
+
fi
|
137
|
+
i=$((i+1))
|
138
|
+
done
|
139
|
+
case $i in
|
140
|
+
(0) set -- ;;
|
141
|
+
(1) set -- "$args0" ;;
|
142
|
+
(2) set -- "$args0" "$args1" ;;
|
143
|
+
(3) set -- "$args0" "$args1" "$args2" ;;
|
144
|
+
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
145
|
+
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
146
|
+
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
147
|
+
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
148
|
+
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
149
|
+
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
150
|
+
esac
|
151
|
+
fi
|
152
|
+
|
153
|
+
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
154
|
+
function splitJvmOpts() {
|
155
|
+
JVM_OPTS=("$@")
|
156
|
+
}
|
157
|
+
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
158
|
+
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
159
|
+
|
160
|
+
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,80 @@
|
|
1
|
+
package org.embulk.filter.reverse_geocoding;
|
2
|
+
|
3
|
+
import ch.hsr.geohash.GeoHash;
|
4
|
+
import com.google.common.base.Strings;
|
5
|
+
import org.h2.tools.Csv;
|
6
|
+
|
7
|
+
import java.net.URL;
|
8
|
+
import java.sql.ResultSet;
|
9
|
+
import java.sql.ResultSetMetaData;
|
10
|
+
import java.sql.SQLException;
|
11
|
+
import java.util.HashMap;
|
12
|
+
|
13
|
+
public class GeoCodeMap
|
14
|
+
{
|
15
|
+
|
16
|
+
static final String LEVEL5_CSV_PATH = "jpcities-master/geohash5.csv";
|
17
|
+
static final String LEVEL4_CSV_PATH = "jpcities-master/geohash4.csv";
|
18
|
+
static HashMap<String, String[]> GEOHASH_MAP;
|
19
|
+
// GeoHash, 緯度, 経度, 都道府県, 支庁, 群もしくは市, 市町村, 行政区域コード
|
20
|
+
// xpsj,43.154296875,140.80078125,北海道,後志支庁,余市郡,余市町,01408
|
21
|
+
static int IDX_HASH = 0;
|
22
|
+
static int IDX_LAT = 1;
|
23
|
+
static int IDX_LON = 2;
|
24
|
+
static int IDX_PREF = 3;
|
25
|
+
static int IDX_BRANCH = 4;
|
26
|
+
static int IDX_COUNTRY = 5;
|
27
|
+
static int IDX_CITY = 6;
|
28
|
+
static int IDX_CODE = 7;
|
29
|
+
|
30
|
+
public static String convert2Pref(double lat, double lon)
|
31
|
+
{
|
32
|
+
String hash = GeoHash.geoHashStringWithCharacterPrecision(lat, lon, 5);
|
33
|
+
String[] s = GEOHASH_MAP.get(hash);
|
34
|
+
//if null throw exception ? not japanese lat/lon exception
|
35
|
+
return s != null ? GEOHASH_MAP.get(hash)[IDX_PREF] : "";
|
36
|
+
}
|
37
|
+
|
38
|
+
public static String convert2City(double lat, double lon)
|
39
|
+
{
|
40
|
+
String hash = GeoHash.geoHashStringWithCharacterPrecision(lat, lon, 5);
|
41
|
+
//if null throw exception ? not japanese lat/lon exception
|
42
|
+
String[] s = GEOHASH_MAP.get(hash);
|
43
|
+
if (s == null) {
|
44
|
+
return "";
|
45
|
+
}
|
46
|
+
|
47
|
+
StringBuilder sb = new StringBuilder();
|
48
|
+
|
49
|
+
for (int i = IDX_PREF; i < IDX_CITY; i++) {
|
50
|
+
sb.append(s[i]);
|
51
|
+
}
|
52
|
+
|
53
|
+
return sb.toString();
|
54
|
+
}
|
55
|
+
|
56
|
+
public static String convert2GeoHash(double lat, double lon)
|
57
|
+
{
|
58
|
+
return GeoHash.geoHashStringWithCharacterPrecision(lat, lon, 5);
|
59
|
+
}
|
60
|
+
|
61
|
+
static {
|
62
|
+
GEOHASH_MAP = new HashMap<>();
|
63
|
+
URL url = GeoCodeMap.class.getClassLoader().getResource(LEVEL5_CSV_PATH);
|
64
|
+
try {
|
65
|
+
ResultSet rs = new Csv().read(url.toString(), null, null);
|
66
|
+
ResultSetMetaData meta = rs.getMetaData();
|
67
|
+
while (rs.next()) {
|
68
|
+
String[] tmp = new String[meta.getColumnCount()];
|
69
|
+
for (int i = 0; i < meta.getColumnCount(); i++) {
|
70
|
+
tmp[i] = Strings.nullToEmpty(rs.getString(i + 1));
|
71
|
+
}
|
72
|
+
GEOHASH_MAP.put(rs.getString(1), tmp);
|
73
|
+
}
|
74
|
+
rs.close();
|
75
|
+
}
|
76
|
+
catch (SQLException e) {
|
77
|
+
throw new RuntimeException("geohash4.csv is not found!");
|
78
|
+
}
|
79
|
+
}
|
80
|
+
}
|
@@ -0,0 +1,266 @@
|
|
1
|
+
package org.embulk.filter.reverse_geocoding;
|
2
|
+
|
3
|
+
import com.google.common.base.Optional;
|
4
|
+
import com.google.common.collect.ImmutableList;
|
5
|
+
import org.embulk.config.Config;
|
6
|
+
import org.embulk.config.ConfigDefault;
|
7
|
+
import org.embulk.config.ConfigSource;
|
8
|
+
import org.embulk.config.Task;
|
9
|
+
import org.embulk.config.TaskSource;
|
10
|
+
import org.embulk.spi.Column;
|
11
|
+
import org.embulk.spi.ColumnVisitor;
|
12
|
+
import org.embulk.spi.Exec;
|
13
|
+
import org.embulk.spi.FilterPlugin;
|
14
|
+
import org.embulk.spi.Page;
|
15
|
+
import org.embulk.spi.PageBuilder;
|
16
|
+
import org.embulk.spi.PageOutput;
|
17
|
+
import org.embulk.spi.PageReader;
|
18
|
+
import org.embulk.spi.Schema;
|
19
|
+
import org.embulk.spi.type.Type;
|
20
|
+
import org.embulk.spi.type.Types;
|
21
|
+
|
22
|
+
import java.util.HashMap;
|
23
|
+
import java.util.List;
|
24
|
+
import java.util.Map;
|
25
|
+
|
26
|
+
import static org.embulk.filter.reverse_geocoding.ReverseGeoCodingFilterPlugin.OutputColumnTask.TYPE_CITY;
|
27
|
+
import static org.embulk.filter.reverse_geocoding.ReverseGeoCodingFilterPlugin.OutputColumnTask.TYPE_HASH;
|
28
|
+
import static org.embulk.filter.reverse_geocoding.ReverseGeoCodingFilterPlugin.OutputColumnTask.TYPE_PREF;
|
29
|
+
|
30
|
+
public class ReverseGeoCodingFilterPlugin
|
31
|
+
implements FilterPlugin
|
32
|
+
{
|
33
|
+
static Schema buildOutputSchema(PluginTask task, Schema inputSchema)
|
34
|
+
{
|
35
|
+
List<OutputColumnTask> outputColumns = task.getOutputColumns();
|
36
|
+
ImmutableList.Builder<Column> builder = ImmutableList.builder();
|
37
|
+
builder.addAll(inputSchema.getColumns());
|
38
|
+
int i = inputSchema.getColumnCount();
|
39
|
+
|
40
|
+
for (OutputColumnTask outputColumn : outputColumns) {
|
41
|
+
Column column = new Column(i++, outputColumn.getName(), outputColumn.getType());
|
42
|
+
builder.add(column);
|
43
|
+
}
|
44
|
+
|
45
|
+
return new Schema(builder.build());
|
46
|
+
}
|
47
|
+
|
48
|
+
static Map<String, OutputColumnTask> getTaskColumnMap(List<OutputColumnTask> outputColumnTasks)
|
49
|
+
{
|
50
|
+
Map<String, OutputColumnTask> m = new HashMap<>();
|
51
|
+
for (OutputColumnTask columnTask : outputColumnTasks) {
|
52
|
+
m.put(columnTask.getName(), columnTask);
|
53
|
+
}
|
54
|
+
return m;
|
55
|
+
}
|
56
|
+
|
57
|
+
static Map<String, Column> getOutputSchemaMap(Schema outputSchema)
|
58
|
+
{
|
59
|
+
Map<String, Column> m = new HashMap<>();
|
60
|
+
for (Column column : outputSchema.getColumns()) {
|
61
|
+
m.put(column.getName(), column);
|
62
|
+
}
|
63
|
+
return m;
|
64
|
+
}
|
65
|
+
|
66
|
+
@Override
|
67
|
+
public void transaction(ConfigSource config, Schema inputSchema,
|
68
|
+
FilterPlugin.Control control)
|
69
|
+
{
|
70
|
+
PluginTask task = config.loadConfig(PluginTask.class);
|
71
|
+
Schema outputSchema = buildOutputSchema(task, inputSchema);
|
72
|
+
control.run(task.dump(), outputSchema);
|
73
|
+
}
|
74
|
+
|
75
|
+
@Override
|
76
|
+
public PageOutput open(TaskSource taskSource, final Schema inputSchema,
|
77
|
+
final Schema outputSchema, final PageOutput output)
|
78
|
+
{
|
79
|
+
|
80
|
+
final PluginTask task = taskSource.loadTask(PluginTask.class);
|
81
|
+
|
82
|
+
final Map<String, Column> outputSchemaMap = getOutputSchemaMap(outputSchema);
|
83
|
+
|
84
|
+
return new PageOutput()
|
85
|
+
{
|
86
|
+
private PageReader reader = new PageReader(inputSchema);
|
87
|
+
private PageBuilder builder = new PageBuilder(
|
88
|
+
Exec.getBufferAllocator(), outputSchema, output);
|
89
|
+
private ColumnVisitorImpl visitor = new ColumnVisitorImpl(builder);
|
90
|
+
|
91
|
+
@Override
|
92
|
+
public void add(Page page)
|
93
|
+
{
|
94
|
+
reader.setPage(page);
|
95
|
+
|
96
|
+
while (reader.nextRecord()) {
|
97
|
+
double lon = 0d;
|
98
|
+
double lat = 0d;
|
99
|
+
|
100
|
+
// get lon / lat
|
101
|
+
for (Column column : inputSchema.getColumns()) {
|
102
|
+
String colName = column.getName();
|
103
|
+
if (colName.equals(task.getTargetLon())) {
|
104
|
+
lon = reader.getDouble(column);
|
105
|
+
}
|
106
|
+
else if (colName.equals(task.getTargetLat())) {
|
107
|
+
lat = reader.getDouble(column);
|
108
|
+
}
|
109
|
+
column.visit(visitor);
|
110
|
+
}
|
111
|
+
|
112
|
+
for (OutputColumnTask outputColumnTask : task.getOutputColumns()) {
|
113
|
+
String name = outputColumnTask.getName();
|
114
|
+
String output = outputColumnTask.getOutput(); // pref/city
|
115
|
+
|
116
|
+
int idx = outputSchemaMap.get(name).getIndex();
|
117
|
+
Type type = outputColumnTask.getType();
|
118
|
+
|
119
|
+
if (Types.STRING.equals(type)) {
|
120
|
+
|
121
|
+
if (output.equals(TYPE_PREF)) {
|
122
|
+
builder.setString(idx, GeoCodeMap.convert2Pref(lat, lon));
|
123
|
+
}
|
124
|
+
else if (output.equals(TYPE_CITY)) {
|
125
|
+
builder.setString(idx, GeoCodeMap.convert2City(lat, lon));
|
126
|
+
}
|
127
|
+
else if (output.equals(TYPE_HASH)) {
|
128
|
+
builder.setString(idx, GeoCodeMap.convert2GeoHash(lat, lon));
|
129
|
+
}
|
130
|
+
}
|
131
|
+
else if (Types.DOUBLE.equals(type)) {
|
132
|
+
// builder.setDouble(idx, GeoCodeMap.convert2LatLon(hash));
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
//visitcolumns
|
137
|
+
builder.addRecord();
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
@Override
|
142
|
+
public void finish()
|
143
|
+
{
|
144
|
+
builder.finish();
|
145
|
+
}
|
146
|
+
|
147
|
+
@Override
|
148
|
+
public void close()
|
149
|
+
{
|
150
|
+
builder.close();
|
151
|
+
}
|
152
|
+
|
153
|
+
class ColumnVisitorImpl
|
154
|
+
implements ColumnVisitor
|
155
|
+
{
|
156
|
+
private final PageBuilder builder;
|
157
|
+
|
158
|
+
ColumnVisitorImpl(PageBuilder builder)
|
159
|
+
{
|
160
|
+
this.builder = builder;
|
161
|
+
}
|
162
|
+
|
163
|
+
@Override
|
164
|
+
public void booleanColumn(Column outputColumn)
|
165
|
+
{
|
166
|
+
if (reader.isNull(outputColumn)) {
|
167
|
+
builder.setNull(outputColumn);
|
168
|
+
}
|
169
|
+
else {
|
170
|
+
builder.setBoolean(outputColumn, reader.getBoolean(outputColumn));
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
@Override
|
175
|
+
public void longColumn(Column outputColumn)
|
176
|
+
{
|
177
|
+
if (reader.isNull(outputColumn)) {
|
178
|
+
builder.setNull(outputColumn);
|
179
|
+
}
|
180
|
+
else {
|
181
|
+
builder.setLong(outputColumn, reader.getLong(outputColumn));
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
@Override
|
186
|
+
public void doubleColumn(Column outputColumn)
|
187
|
+
{
|
188
|
+
if (reader.isNull(outputColumn)) {
|
189
|
+
builder.setNull(outputColumn);
|
190
|
+
}
|
191
|
+
else {
|
192
|
+
builder.setDouble(outputColumn, reader.getDouble(outputColumn));
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
@Override
|
197
|
+
public void stringColumn(Column outputColumn)
|
198
|
+
{
|
199
|
+
if (reader.isNull(outputColumn)) {
|
200
|
+
builder.setNull(outputColumn);
|
201
|
+
}
|
202
|
+
else {
|
203
|
+
builder.setString(outputColumn, reader.getString(outputColumn));
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
@Override
|
208
|
+
public void timestampColumn(Column outputColumn)
|
209
|
+
{
|
210
|
+
if (reader.isNull(outputColumn)) {
|
211
|
+
builder.setNull(outputColumn);
|
212
|
+
}
|
213
|
+
else {
|
214
|
+
builder.setTimestamp(outputColumn, reader.getTimestamp(outputColumn));
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
@Override
|
219
|
+
public void jsonColumn(Column outputColumn)
|
220
|
+
{
|
221
|
+
if (reader.isNull(outputColumn)) {
|
222
|
+
builder.setNull(outputColumn);
|
223
|
+
}
|
224
|
+
else {
|
225
|
+
builder.setJson(outputColumn, reader.getJson(outputColumn));
|
226
|
+
}
|
227
|
+
}
|
228
|
+
}
|
229
|
+
};
|
230
|
+
}
|
231
|
+
|
232
|
+
public interface PluginTask
|
233
|
+
extends Task
|
234
|
+
{
|
235
|
+
@Config("target_lon")
|
236
|
+
public String getTargetLon();
|
237
|
+
|
238
|
+
@Config("target_lat")
|
239
|
+
public String getTargetLat();
|
240
|
+
|
241
|
+
@Config("level")
|
242
|
+
@ConfigDefault("5")
|
243
|
+
public Optional<String> getLevel();
|
244
|
+
|
245
|
+
@Config("output_columns")
|
246
|
+
public List<OutputColumnTask> getOutputColumns();
|
247
|
+
}
|
248
|
+
|
249
|
+
public interface OutputColumnTask
|
250
|
+
extends Task
|
251
|
+
{
|
252
|
+
String TYPE_PREF = "pref";
|
253
|
+
String TYPE_CITY = "city";
|
254
|
+
String TYPE_HASH = "hash";
|
255
|
+
|
256
|
+
@Config("name")
|
257
|
+
String getName();
|
258
|
+
|
259
|
+
@Config("type")
|
260
|
+
Type getType();
|
261
|
+
|
262
|
+
@Config("out")
|
263
|
+
@ConfigDefault("pref")
|
264
|
+
String getOutput();
|
265
|
+
}
|
266
|
+
}
|