【问题标题】:Append a column to dataframe using withColumn and callUDF使用 withColumn 和 callUDF 将列附加到数据框
【发布时间】:2016-06-28 02:21:05
【问题描述】:

我正在尝试根据同一数据帧的其他列向给定数据帧添加一个新列。

map 是一个字符串布尔映射

下面是我的代码。

    hiveContext.udf().register("udfname", new UDF2<String, String, Boolean>() {
    @Override
    public Boolean call(String col1, String col2) throws Exception {
            return map.get(col1+col2);
        }
    }, DataTypes.BooleanType);

    DataFrame newDF = df.withColumn("newcol", calUDF("udfname" ,df.col("col1"),df.col("col2")));

我收到编译错误。

 The method calUDF(String, Column, Column) is undefined for the type myclass

【问题讨论】:

    标签: java apache-spark spark-dataframe udf


    【解决方案1】:

    我无法使用 callUDF,但我通过使用 udf 进行查询解决了它。

    从表中选择 col1,col2,udfname(col1,col2) 作为 newCol3。 以上将为我提供带有新列的新数据框,其值基于其他列填充。

    【讨论】:

      【解决方案2】:

      这真的很晚了,但问题很可能是由于您错过了通话中的第二个 l 造成的拼写错误。您还需要确保您已导入 callUDF,但如果 udf 工作,那么您可能做到了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-24
        • 1970-01-01
        • 2019-01-22
        • 1970-01-01
        • 2021-06-06
        • 2018-04-06
        • 2014-01-03
        • 1970-01-01
        相关资源
        最近更新 更多