我使用spark 1.6.1和Java代码。当我使用callUDF()时,它显示

The method callUDF(String, Column) is undefined for the type PhaseOne

并且callUdf()不起作用。我的代码如下:
sqlContext.udf().register("stringToLong", new UDF1<String, Long>() {

        @Override
        public Long call(String arg0) throws Exception {
            // TODO Auto-generated method stub
            IPTypeConvert itc = new IPTypeConvert();
            return itc.stringtoLong(arg0);
        }
    }, DataTypes.LongType);
    DataFrame interDF = initInterDF.withColumn("interIPInt", callUDF("stringToLong", initInterDF.col("interIP")));

最佳答案

您必须在开头添加:

import static org.apache.spark.sql.functions.callUDF;

然后使用它:
sqlContext.udf().register("stringToLong", new UDF1<String, Long>() {

        @Override
        public Long call(String arg0) throws Exception {
            // TODO Auto-generated method stub
            IPTypeConvert itc = new IPTypeConvert();
            return itc.stringtoLong(arg0);
        }
    }, DataTypes.LongType);
DataFrame interDF = initInterDF.withColumn("interIPInt", callUDF("stringToLong", initInterDF.col("interIP")));

09-25 21:57