如何在scala中编写pig自定义项

i2byvkas  于 2021-06-25  发布在  Pig
关注(0)|答案(2)|浏览(372)

我正在尝试用scala(使用eclipse)编写一个pig udf。我在java构建路径中添加了pig.jar作为一个库,它似乎解决了以下2个导入问题:
导入org.apache.pig.evalfunc
导入org.apache.pig.data.tuple
但是,我有两个错误无法解决:
org.apache.pig.evalfunc[t]没有构造函数
value get不是org.apache.pig.data.tuple的成员(尽管我确信tuple有get方法)
以下是完整代码:

package datesUDFs
import org.apache.pig.EvalFunc
import org.apache.pig.data.Tuple
class getYear extends EvalFunc {
  val extractDate = """^(\d\d\d\d)-\d\d-\d\d \d\d:\d\d:\d\d""".r
  def isDate(dtString: String): Boolean = extractDate.findFirstIn(dtString).nonEmpty

  override def exec(input: Tuple): Int = input.get(0) match {
    case dtString: String =>
      if (!isDate(dtString)) throw new IllegalArgumentException("Invalid date string!")
      else (for (extractDate(year) <- extractDate.findFirstIn(dtString)) yield year).head.toInt
    case _ => throw new IllegalArgumentException("Invalid function call!")
  }
}

有人能帮我解决这个问题吗?
提前谢谢!!!

vuv7lop3

vuv7lop31#

除了必须说明 EvalFunc 类型参数,你的代码编译得很好。

package datesUDFs
import org.apache.pig.EvalFunc
import org.apache.pig.data.Tuple
class getYear extends EvalFunc[Int] { // This is the only line I changed.
  val extractDate = """^(\d\d\d\d)-\d\d-\d\d \d\d:\d\d:\d\d""".r
  def isDate(dtString: String): Boolean = extractDate.findFirstIn(dtString).nonEmpty

  override def exec(input: Tuple): Int = input.get(0) match {
    case dtString: String =>
      if (!isDate(dtString)) throw new IllegalArgumentException("Invalid date string!")
      else (for (extractDate(year) <- extractDate.findFirstIn(dtString)) yield year).head.toInt
    case _ => throw new IllegalArgumentException("Invalid function call!")
  }
}

看看是否有用,有时scalaide会抱怨错误的事情。

dtcbnfnu

dtcbnfnu2#

解决了!我在java构建路径中添加了hadoop-common-2.2.0.jar和commons-logging-1.1.3.jar,问题得到了解决。

相关问题