วิธีเขียน Pig UDF ใน Scala

ฉันกำลังพยายามเขียน Pig UDF ใน Scala (โดยใช้ Eclipse) ฉันได้เพิ่ม pig.jar เป็นไลบรารีในเส้นทางการสร้าง java ซึ่งดูเหมือนว่าจะแก้ไขการนำเข้า 2 รายการด้านล่าง:

  • นำเข้า org.apache.pig.EvalFunc
  • นำเข้า org.apache.pig.data.Tuple

อย่างไรก็ตาม ฉันได้รับข้อผิดพลาด 2 ข้อซึ่งฉันไม่สามารถแก้ไขได้:

  1. org.apache.pig.EvalFunc[T] ไม่มีตัวสร้าง
  2. 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!")
  }
}

ใครสามารถช่วยฉันแก้ไขปัญหานี้ได้บ้าง

ขอบคุณล่วงหน้า!!!


person Roberto A.    schedule 04.11.2013    source แหล่งที่มา


คำตอบ (2)


นอกจากจะต้องระบุพารามิเตอร์ประเภท 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 ก็บ่นเกี่ยวกับสิ่งที่ผิด

person Jack Leow    schedule 04.11.2013
comment
ขอบคุณแจ็ค น่าเสียดายที่ฉันยังคงได้รับข้อผิดพลาดเดิมอยู่ คุณใช้ Eclipse หรือคุณกำลังรวบรวมจากเชลล์หรือไม่? ถ้าเป็นเช่นนั้นบรรทัดคำสั่งของคุณคืออะไร? ขอบคุณอีกครั้ง - person Roberto A.; 05.11.2013

แก้ไขมัน! ฉันเพิ่ม hadoop-common-2.2.0.jar และ commons-logging-1.1.3.jar ลงในเส้นทางการสร้าง java ของฉัน และปัญหาได้รับการแก้ไขแล้ว

person Roberto A.    schedule 05.11.2013