本文整理汇总了Scala中com.esotericsoftware.kryo.Serializer类的典型用法代码示例。如果您正苦于以下问题:Scala Serializer类的具体用法?Scala Serializer怎么用?Scala Serializer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Serializer类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: WritableSerializer
//设置package包名称以及导入依赖的类
package org.hammerlab.hadoop.kryo
import java.io.{ DataInputStream, DataOutputStream }
import com.esotericsoftware.kryo.io.{ Input, Output }
import com.esotericsoftware.kryo.{ Kryo, Serializer }
import org.apache.hadoop.io.Writable
class WritableSerializer[T <: Writable](ctorArgs: Any*) extends Serializer[T] {
override def read(kryo: Kryo, input: Input, clz: Class[T]): T = {
val t = clz.newInstance()
t.readFields(new DataInputStream(input))
t
}
override def write(kryo: Kryo, output: Output, t: T): Unit = {
t.write(new DataOutputStream(output))
}
}
示例2: SerializableSerializer
//设置package包名称以及导入依赖的类
package org.hammerlab.hadoop.kryo
import java.io.{ ObjectInputStream, ObjectOutputStream }
import com.esotericsoftware.kryo.io.{ Input, Output }
import com.esotericsoftware.kryo.{ Kryo, Serializer }
case class SerializableSerializer[T <: Serializable]()
extends Serializer[T] {
override def read(kryo: Kryo, input: Input, `type`: Class[T]): T =
new ObjectInputStream(input)
.readObject()
.asInstanceOf[T]
override def write(kryo: Kryo, output: Output, t: T): Unit =
new ObjectOutputStream(output)
.writeObject(t)
}
示例3: serializeAs
//设置package包名称以及导入依赖的类
package org.hammerlab
import com.esotericsoftware.kryo.io.{ Input, Output }
import com.esotericsoftware.kryo.{ Kryo, Serializer }
package object kryo {
def serializeAs[T, U](implicit to: T ? U, from: U ? T): Serializer[T] =
new Serializer[T] {
override def read(kryo: Kryo, input: Input, `type`: Class[T]): T =
from(
kryo
.readClassAndObject(input)
.asInstanceOf[U]
)
override def write(kryo: Kryo, output: Output, t: T): Unit =
kryo.writeClassAndObject(output, to(t))
}
}
示例4: BasesSerializer
//设置package包名称以及导入依赖的类
package org.hammerlab.genomics.bases
import com.esotericsoftware.kryo.io.{ Input, Output }
import com.esotericsoftware.kryo.{ Kryo, Serializer }
class BasesSerializer
extends Serializer[Bases] {
override def write(kryo: Kryo, output: Output, bases: Bases): Unit = {
output.writeInt(bases.bytes.length, true)
bases.foreach(base ? output.writeByte(base.byte))
}
override def read(kryo: Kryo, input: Input, cls: Class[Bases]): Bases = {
val count: Int = input.readInt(true)
val bases = Bases.newBuilder
(0 until count).foreach(_ ? bases += input.readByte())
bases.result()
}
}
示例5: NCKryoRegistrator
//设置package包名称以及导入依赖的类
package se.kth.climate.fast.netcdf
import org.apache.spark.serializer.KryoRegistrator
import com.esotericsoftware.kryo.{ Kryo, KryoException, Serializer }
import com.esotericsoftware.kryo.io.Output
import com.esotericsoftware.kryo.io.Input
import ucar.nc2.NetcdfFile
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import ucar.nc2.iosp.netcdf3.N3outputStreamWriter;
import java.util.UUID;
class NCKryoRegistrator extends KryoRegistrator {
override def registerClasses(kryo: Kryo) {
println("Registering custom NetCDF serializers");
com.esotericsoftware.minlog.Log.TRACE();
kryo.register(classOf[NetcdfFile], new NetcdfFileSerializer());
kryo.register(classOf[Array[String]], new com.esotericsoftware.kryo.serializers.DefaultArraySerializers.StringArraySerializer())
kryo.register(classOf[Array[Int]], new com.esotericsoftware.kryo.serializers.DefaultArraySerializers.IntArraySerializer())
//kryo.setRegistrationRequired(true);
}
}
class NetcdfFileSerializer extends Serializer[NetcdfFile] {
override def write(kryo: Kryo, output: Output, ncfile: NetcdfFile) {
val baos = new ByteArrayOutputStream();
val out = new DataOutputStream(baos);
val w = new N3outputStreamWriter(ncfile);
val ulim = ncfile.getUnlimitedDimension();
val numrec = if (ulim == null) 0 else ncfile.getUnlimitedDimension().getLength();
w.writeHeader(out, numrec);
w.writeDataAll(out);
out.flush();
out.close();
val byteArray = baos.toByteArray();
// println("**********BYTES***********");
// println(byteArray.take(100000).mkString);
output.writeInt(byteArray.length);
output.write(byteArray);
println(s"******** Wrote ncfile (size=${byteArray.length}) **********");
}
override def read(kryo: Kryo, input: Input, `type`: Class[NetcdfFile]): NetcdfFile = {
val len = input.readInt();
println(s"******** Reading ncfile (size=${len}) **********");
val raw = new Array[Byte](len);
var readBytes = 0;
do {
val res = input.read(raw, readBytes, len - readBytes);
if (res > -1) {
readBytes += res;
} else {
throw new KryoException(s"Read only $readBytes bytes when $len bytes were expected!");
}
} while (readBytes < len);
println(s"******** Read ncfile (read=${readBytes}, size=${len}) **********");
NetcdfFile.openInMemory(UUID.randomUUID().toString(), raw);
}
}