我试图从在 clojure 中运行的 Apache flink 获得一个小例子,但现在我被卡住了,因为 clojure 中的类型提示和 flink 中的一些奇怪的怪癖。
这是我的代码:
(ns pipeline.core
(:import
(org.apache.flink.api.java ExecutionEnvironment)
(org.apache.flink.api.common.functions FlatMapFunction)
(org.apache.flink.api.java.tuple Tuple2)
(org.apache.flink.util Collector)
(java.lang String)))
(def flink-env (ExecutionEnvironment/createLocalEnvironment))
(def dataset (.fromElements flink-env (to-array ["please test me"])))
(defn tokenizer [] (reify FlatMapFunction
( flatMap [this value collector]
(println value))))
(.flatMap dataset (tokenizer))
如果我不提供类型提示,我会从 flink api 收到错误:
Caused by: java.lang.IllegalArgumentException: The types of the interface org.apache.flink.api.common.functions.FlatMapFunction could not be inferred. Support for synthetic interfaces, lambdas, and generic types is limited at this point.
at org.apache.flink.api.java.typeutils.TypeExtractor.getParameterType(TypeExtractor.java:662)
如果我提供类型提示:
(defn tokenizer [] (reify FlatMapFunction
( ^void flatMap [this ^String value ^Collector collector]
(println value))))
我从 clojure 编译器得到一个错误:
Caused by: java.lang.IllegalArgumentException: Can't find matching method: flatMap, leave off hints for auto match.
at clojure.lang.Compiler$NewInstanceMethod.parse(Compiler.java:8065)
有没有办法在 clojure 中使用泛型类添加类型提示?它应该是这样的:
(defn tokenizer [] (reify FlatMapFunction
( ^void flatMap [this ^String value ^Collector<Tuple2<String, Integer>> collector]
(println value))))
但这不起作用。有任何想法吗?
lein 配置如下所示:
(defproject pipeline "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0"]
[org.apache.flink/flink-java "0.9.0"]
]
:aot :all)