我想將一個用Scala編寫的例子(從Apache Spark項目)移植到Java中,並且遇到一些問題。如何使用Spark的.newAPIHadoopRDD()來自Java
從原來的Scala例子的代碼
val casRdd = sc.newAPIHadoopRDD(job.getConfiguration(),
classOf[CqlPagingInputFormat],
classOf[java.util.Map[String,ByteBuffer]],
classOf[java.util.Map[String,ByteBuffer]])
構建並運行得很好,但
JavaPairRDD rdd = jsc.newAPIHadoopRDD(job.getConfiguration(),
CqlPagingInputFormat.class,
java.util.Map<String, ByteBuffer>.class,
java.util.Map<String, ByteBuffer>.class);
在爪哇(Cannot select from parameterized type
)是不允許的。
更改
java.util.Map<String, ByteBuffer>.class
到
Class.forName("java.util.Map<String, ByteBuffer>")
產生一個新的錯誤:
Error:(42, 30) java: method newAPIHadoopRDD in class org.apache.spark.api.java.JavaSparkContext cannot be applied to given types;
required: org.apache.hadoop.conf.Configuration,java.lang.Class<F>,java.lang.Class<K>,java.lang.Class<V>
found: org.apache.hadoop.conf.Configuration,java.lang.Class<org.apache.cassandra.hadoop.cql3.CqlPagingInputFormat>,java.lang.Class<capture#1 of ?>,java.lang.Class<capture#2 of ?>
reason: inferred type does not conform to declared bound(s)
inferred: org.apache.cassandra.hadoop.cql3.CqlPagingInputFormat
bound(s): org.apache.hadoop.mapreduce.InputFormat<capture#1 of ?,capture#2 of ?>
它改變成簡單java.util.Map.class
產生類似的錯誤:
Error:(44, 30) java: method newAPIHadoopRDD in class org.apache.spark.api.java.JavaSparkContext cannot be applied to given types;
required: org.apache.hadoop.conf.Configuration,java.lang.Class<F>,java.lang.Class<K>,java.lang.Class<V>
found: org.apache.hadoop.conf.Configuration,java.lang.Class<org.apache.cassandra.hadoop.cql3.CqlPagingInputFormat>,java.lang.Class<java.util.Map>,java.lang.Class<java.util.Map>
reason: inferred type does not conform to declared bound(s)
inferred: org.apache.cassandra.hadoop.cql3.CqlPagingInputFormat
bound(s): org.apache.hadoop.mapreduce.InputFormat<java.util.Map,java.util.Map>
那麼正確的翻譯是什麼?值得注意的是,newAPIHadoopRDD()
函數是Scala和Java的不同實現。有關這些方法的文檔可以在以下位置找到:here,這裏是:http://spark.apache.org/docs/latest/api/java/org/apache/spark/api/java/JavaSparkContext.html#newAPIHadoopRDD(org.apache.hadoop.conf.Configuration,java.lang.Class,java.lang.Class,java.lang.Class)。
的CqlPagingInputFormat
聲明看起來是這樣的
public class CqlPagingInputFormat extends org.apache.cassandra.hadoop.AbstractColumnFamilyInputFormat<java.util.Map<java.lang.String,java.nio.ByteBuffer>,java.util.Map<java.lang.String,java.nio.ByteBuffer>> {
您是否嘗試過'java.util.Map.class'而不是'java.util.Map .class'? –
是的,我應該補充一點。我將在原始問題中發佈錯誤,謝謝。 – martingms