Utils.tryOrIOException无法正常工作

时间:2018-08-15 21:42:37

标签: scala apache-spark

我正在使用以下scala代码:

import java.io.{Externalizable, ObjectInput, ObjectOutput}

import com.google.api.services.pubsub.model.PubsubMessage
import org.apache.spark.util.Utils

/**
  * Input stream that subscribe messages from Google cloud Pub/Sub subscription.
  *
  */


class SparkPubsubMessage() extends Externalizable {

    var message = new PubsubMessage

    def getData(): Array[Byte] = message.decodeData()

    def getAttributes(): java.util.Map[String, String] = message.getAttributes

    def getMessageId(): String = message.getMessageId

    def getPublishTime(): String = message.getPublishTime

    override def writeExternal(out: ObjectOutput): Unit =  {
      message.decodeData() match {
        case null => out.writeInt(-1)
        case data =>
          out.writeInt(data.size)
          out.write(data)
      }

      message.getMessageId match {
        case null => out.writeInt(-1)
        case id =>
          val idBuff = Utils.serialize(id)
          out.writeInt(idBuff.length)
          out.write(idBuff)
      }

      message.getPublishTime match {
        case null => out.writeInt(-1)
        case time =>
          val publishTimeBuff = Utils.serialize(time)
          out.writeInt(publishTimeBuff.length)
          out.write(publishTimeBuff)
      }

      message.getAttributes match {
        case null => out.writeInt(-1)
        case attrs =>
          out.writeInt(attrs.size())
          for ((k, v) <- message.getAttributes.asScala) {
            val keyBuff = Utils.serialize(k)
            out.writeInt(keyBuff.length)
            out.write(keyBuff)
            val valBuff = Utils.serialize(v)
            out.writeInt(valBuff.length)
            out.write(valBuff)
          }
      }
    }

    override def readExternal(in: ObjectInput): Unit = Utils.tryOrIOException {
      in.readInt() match {
        case -1 => message.encodeData(null)
        case bodyLength =>
          val data = new Array[Byte](bodyLength)
          in.readFully(data)
          message.encodeData(data)
      }

      in.readInt() match {
        case -1 => message.setMessageId(null)
        case idLength =>
          val idBuff = new Array[Byte](idLength)
          in.readFully(idBuff)
          val id: String = Utils.deserialize(idBuff)
          message.setMessageId(id)
      }

      in.readInt() match {
        case -1 => message.setPublishTime(null)
        case publishTimeLength =>
          val publishTimeBuff = new Array[Byte](publishTimeLength)
          in.readFully(publishTimeBuff)
          val publishTime: String = Utils.deserialize(publishTimeBuff)
          message.setPublishTime(publishTime)
      }

      in.readInt() match {
        case -1 => message.setAttributes(null)
        case numAttributes =>
          val attributes = new java.util.HashMap[String, String]
          for (i <- 0 until numAttributes) {
            val keyLength = in.readInt()
            val keyBuff = new Array[Byte](keyLength)
            in.readFully(keyBuff)
            val key: String = Utils.deserialize(keyBuff)

            val valLength = in.readInt()
            val valBuff = new Array[Byte](valLength)
            in.readFully(valBuff)
            val value: String = Utils.deserialize(valBuff)

            attributes.put(key, value)
          }
          message.setAttributes(attributes)
      }
    }
}

我收到与Utils软件包有关的编译器错误: 找不到:值实用程序 如果出现此错误,我将无法理解来源,因为该模块的导入似乎已正确完成 您知道如何解决此问题或如何替换Utils.tryOrIOExceptionUtils.deserialize吗? 我的目的是编写自定义序列化格式

0 个答案:

没有答案