我尝试在Java中重写以下scala代码(取自https://github.com/DataSystemsLab/GeoSpark/blob/master/sql/src/main/scala/org/apache/spark/sql/geosparksql/expressions/Functions.scala),但不知道如何为此行执行此操作:
val geometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData]).
有人能帮帮我吗?包括我到目前为止所做的Java代码。
/**
* Return the convex hull of a Geometry.
*
* @param inputExpressions
*/
case class ST_ConvexHull(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
assert(inputExpressions.length == 1)
val geometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData])
new GenericArrayData(GeometrySerializer.serialize(geometry.convexHull()))
}
override def dataType: DataType = new GeometryUDT()
override def children: Seq[Expression] = inputExpressions
}
这是我到目前为止所做的Java代码:
import com.vividsolutions.jts.geom.Geometry;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.catalyst.expressions.Expression;
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext;
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback;
import org.apache.spark.sql.catalyst.expressions.codegen.ExprCode;
import org.datasyslab.geosparksql.utils.GeometrySerializer;
import org.apache.spark.sql.geosparksql.UDT.GeometryUDT;
import org.apache.spark.sql.catalyst.util.ArrayData;
import org.apache.spark.sql.catalyst.util.GenericArrayData;
import org.apache.spark.sql.types.DataType;
import scala.collection.Seq;
/**
*
* @author Paul Z. Wu Apr 16, 2018
*/
public class ST_ConcaveHull extends Expression implements CodegenFallback {
Seq<Expression> inputExpressions;
public ST_ConcaveHull(Seq<Expression> inputExpressions) {
this.inputExpressions = inputExpressions;
}
@Override
public boolean nullable() {
return false;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Object eval(InternalRow input) {
assert (inputExpressions.length() == 1);
//****** Have trouble here!!!!
ArrayData ad = inputExpressions.take(0).eval(input.);
Geometry g = GeometrySerializer.deserialize(ad);
return new GenericArrayData(GeometrySerializer.serialize(g.convexHull()));
}
@Override
public ExprCode doGenCode(CodegenContext cc, ExprCode ec) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public DataType dataType() {
return new GeometryUDT();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Seq<Expression> children() {
return this.inputExpressions;
}
@Override
public Object productElement(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int productArity() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean canEqual(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
答案 0 :(得分:1)
该行可以“翻译”为:
// val geometry = GeometrySerializer.deserialize(
// inputExpressions(0) // get value with index 0 in Seq
// .eval(input) // call "eval" method on that value
// .asInstanceOf[ArrayData] // cast to ArrayData
//)
ArrayData ad = (ArrayData) inputExpressions.apply(0).eval(input);
Geometry g = GeometrySerializer.deserialize(ad);
这里发生了什么:
inputExpressions
是scala.collection.Seq
。要从i
的索引xs: Seq
获取值,在Scala中我们只需编写xs(i)
,但这只是xs.apply(i)
的“语法糖”(即速记) - “apply”方法在Scala中是特殊的,因为方法名称可以简单地省略。因此,在迁移到Java时,我们会明确地调用apply
。 x.asInstanceOf[T]
相当于Java的投射 (T) x
,在本例中 - 为了分配eval
的结果Object
进入ArrayData
我们必须使用强制转换