本文整理汇总了Scala中com.google.protobuf.ByteString类的典型用法代码示例。如果您正苦于以下问题:Scala ByteString类的具体用法?Scala ByteString怎么用?Scala ByteString使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ByteString类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: serialize
//设置package包名称以及导入依赖的类
package akka.remote
import akka.remote.WireFormats._
import com.google.protobuf.ByteString
import akka.actor.ExtendedActorSystem
import akka.serialization.SerializationExtension
def serialize(system: ExtendedActorSystem, message: AnyRef): SerializedMessage = {
val s = SerializationExtension(system)
val serializer = s.findSerializerFor(message)
val builder = SerializedMessage.newBuilder
builder.setMessage(ByteString.copyFrom(serializer.toBinary(message)))
builder.setSerializerId(serializer.identifier)
if (serializer.includeManifest)
builder.setMessageManifest(ByteString.copyFromUtf8(message.getClass.getName))
builder.build
}
}
示例2: from
//设置package包名称以及导入依赖的类
package shapeless.datatype.tensorflow
import com.google.protobuf.ByteString
import org.tensorflow.example._
import shapeless.datatype.mappable.{BaseMappableType, MappableType}
trait BaseTensorFlowMappableType[V] extends MappableType[Features.Builder, V] {
def from(value: Feature): V = fromSeq(value).head
def to(value: V): Feature = toSeq(Seq(value))
def fromSeq(value: Feature): Seq[V]
def toSeq(value: Seq[V]): Feature
override def get(m: Features.Builder, key: String): Option[V] =
Option(m.getFeatureMap.get(key)).map(from)
override def getAll(m: Features.Builder, key: String): Seq[V] =
Option(m.getFeatureMap.get(key)).toSeq.flatMap(fromSeq)
override def put(key: String, value: V, tail: Features.Builder): Features.Builder =
tail.putFeature(key, to(value))
override def put(key: String, value: Option[V], tail: Features.Builder): Features.Builder =
value.foldLeft(tail)((b, v) => b.putFeature(key, to(v)))
override def put(key: String, values: Seq[V], tail: Features.Builder): Features.Builder =
tail.putFeature(key, toSeq(values))
}
trait TensorFlowMappableType {
implicit val tensorFlowBaseMappableType = new BaseMappableType[Features.Builder] {
override def base: Features.Builder = Features.newBuilder()
override def get(m: Features.Builder, key: String): Option[Features.Builder] = ???
override def getAll(m: Features.Builder, key: String): Seq[Features.Builder] = ???
override def put(key: String, value: Features.Builder, tail: Features.Builder): Features.Builder = ???
override def put(key: String, value: Option[Features.Builder], tail: Features.Builder): Features.Builder = ???
override def put(key: String, values: Seq[Features.Builder], tail: Features.Builder): Features.Builder = ???
}
import TensorFlowType._
implicit val booleanTensorFlowMappableType = at[Boolean](toBooleans, fromBooleans)
implicit val intTensorFlowMappableType = at[Int](toInts, fromInts)
implicit val longTensorFlowMappableType = at[Long](toLongs, fromLongs)
implicit val floatTensorFlowMappableType = at[Float](toFloats, fromFloats)
implicit val doubleTensorFlowMappableType = at[Double](toDoubles, fromDoubles)
implicit val byteStringTensorFlowMappableType = at[ByteString](toByteStrings, fromByteStrings)
implicit val byteArrayTensorFlowMappableType = at[Array[Byte]](toByteArrays, fromByteArrays)
implicit val stringTensorFlowMappableType = at[String](toStrings, fromStrings)
}
示例3: Records
//设置package包名称以及导入依赖的类
package shapeless.datatype.test
import java.net.URI
import com.google.protobuf.ByteString
import org.joda.time.Instant
import org.scalacheck._
object Records {
case class Required(booleanField: Boolean,
intField: Int, longField: Long, floatField: Float, doubleField: Double,
stringField: String,
byteStringField: ByteString, byteArrayField: Array[Byte],
timestampField: Instant)
case class Optional(booleanField: Option[Boolean],
intField: Option[Int], longField: Option[Long],
floatField: Option[Float], doubleField: Option[Double],
stringField: Option[String],
byteStringField: Option[ByteString], byteArrayField: Option[Array[Byte]],
timestampField: Option[Instant])
case class Repeated(booleanField: List[Boolean],
intField: List[Int], longField: List[Long],
floatField: List[Float], doubleField: List[Double],
stringField: List[String],
byteStringField: List[ByteString], byteArrayField: List[Array[Byte]],
timestampField: List[Instant])
case class Mixed(longField: Long, doubleField: Double, stringField: String,
longFieldO: Option[Long], doubleFieldO: Option[Double], stringFieldO: Option[String],
longFieldR: List[Long], doubleFieldR: List[Double], stringFieldR: List[String])
case class Nested(longField: Long, longFieldO: Option[Long], longFieldR: List[Long],
mixedField: Mixed, mixedFieldO: Option[Mixed], mixedFieldR: List[Mixed])
case class Seqs(array: Array[Int], list: List[Int], vector: Vector[Int])
case class Custom(uriField: URI, uriFieldO: Option[URI], uriFieldR: List[URI])
implicit val arbByteString = Arbitrary(Gen.alphaStr.map(ByteString.copyFromUtf8))
implicit val arbInstant = Arbitrary(Gen.chooseNum(0, Int.MaxValue).map(new Instant(_)))
implicit val arbUri = Arbitrary(Gen.alphaStr.map(URI.create))
}
示例4: FixUserSequenceMigrator
//设置package包名称以及导入依赖的类
package im.actor.server.migrations
import akka.actor.ActorSystem
import com.google.protobuf.ByteString
import im.actor.api.rpc.sequence.UpdateRawUpdate
import im.actor.server.db.ActorPostgresDriver.api._
import im.actor.server.db.DbExtension
import im.actor.server.model.{ SerializedUpdate, UpdateMapping }
import slick.jdbc.{ PositionedParameters, SetParameter }
import scala.concurrent.duration._
import scala.concurrent.Future
object FixUserSequenceMigrator extends Migration {
implicit object SetByteArray extends SetParameter[Array[Byte]] {
def apply(v: Array[Byte], pp: PositionedParameters) = {
pp.setBytes(v)
}
}
override protected def migrationName: String = "2015-11-12-FixUserSequence"
override protected def migrationTimeout: Duration = 15.minutes
override protected def startMigration()(implicit system: ActorSystem): Future[Unit] = {
import system.dispatcher
val timestamp = System.currentTimeMillis()
val upd = UpdateRawUpdate(None, Array.empty)
val mapping = UpdateMapping().withDefault(SerializedUpdate(upd.header, ByteString.copyFrom(upd.toByteArray)))
val mappingBytes = mapping.toByteArray
def fix(userId: Int): DBIO[Unit] = {
for {
seq ? sql"SELECT seq FROM user_sequence WHERE user_id = $userId ORDER BY seq DESC LIMIT 1".as[Int].headOption map (_ getOrElse 0)
newSeq = seq + 3000000
_ ? sqlu"INSERT INTO user_sequence (user_id, seq, timestamp, mapping) VALUES ($userId, $newSeq, $timestamp, $mappingBytes)"
} yield ()
}
DbExtension(system).db.run(for {
userIds ? sql"SELECT id FROM users".as[Int]
_ ? DBIO.sequence(userIds map fix)
} yield ())
}
}
示例5: MasterKeyTable
//设置package包名称以及导入依赖的类
package im.actor.server.persist
import java.security.MessageDigest
import com.google.common.primitives.Longs
import com.google.protobuf.ByteString
import im.actor.server.db.ActorPostgresDriver.api._
import im.actor.server.model.MasterKey
import scala.concurrent.ExecutionContext
final class MasterKeyTable(tag: Tag) extends Table[MasterKey](tag, "master_keys") {
def authId = column[Long]("auth_id", O.PrimaryKey)
def body = column[ByteString]("body")
def * = (authId, body) <> ((MasterKey.apply _).tupled, MasterKey.unapply)
}
object MasterKeyRepo {
val masterKeys = TableQuery[MasterKeyTable]
val byIdC = Compiled { id: Rep[Long] ?
masterKeys filter (_.authId === id)
}
def create(body: Array[Byte])(implicit ec: ExecutionContext) = {
val md = MessageDigest.getInstance("SHA-256")
val authId = Longs.fromByteArray(md.digest(body).take(java.lang.Long.BYTES))
val masterKey = MasterKey(authId, ByteString.copyFrom(body))
(masterKeys += masterKey) map (_ ? masterKey)
}
def find(authId: Long) = byIdC(authId).result.headOption
}
示例6: ApplePushCredentialsTable
//设置package包名称以及导入依赖的类
package im.actor.server.persist.push
import com.google.protobuf.ByteString
import com.google.protobuf.wrappers.{ Int32Value, StringValue }
import im.actor.server.model.push.ApplePushCredentials
import im.actor.server.db.ActorPostgresDriver.api._
import im.actor.server.persist.AuthIdRepo
import scala.concurrent.ExecutionContext
import scala.language.postfixOps
final class ApplePushCredentialsTable(tag: Tag) extends Table[ApplePushCredentials](tag, "apple_push_credentials") {
def authId = column[Long]("auth_id", O.PrimaryKey)
def apnsKey = column[Option[Int32Value]]("apns_key")
def token = column[ByteString]("token")
def isVoip = column[Boolean]("is_voip", O.PrimaryKey)
def bundleId = column[Option[StringValue]]("bundle_id")
def * = (authId, apnsKey, token, isVoip, bundleId) <> ((ApplePushCredentials.apply _).tupled, ApplePushCredentials.unapply)
}
object ApplePushCredentialsRepo {
val creds = TableQuery[ApplePushCredentialsTable]
def byToken(token: ByteString): Query[ApplePushCredentialsTable, ApplePushCredentials, Seq] =
creds.filter(_.token === token)
def byToken(token: Array[Byte]): Query[ApplePushCredentialsTable, ApplePushCredentials, Seq] =
byToken(ByteString.copyFrom(token))
def createOrUpdate(c: ApplePushCredentials) =
creds.insertOrUpdate(c)
private def byAuthId(authId: Rep[Long]) = creds.filter(_.authId === authId)
private val byAuthIdC = Compiled(byAuthId _)
def find(authId: Long) =
byAuthIdC(authId).result.headOption
def find(authIds: Set[Long]) =
creds filter (_.authId inSet authIds) result
def findByUser(userId: Int)(implicit ec: ExecutionContext) =
for {
authIds ? AuthIdRepo.activeByUserIdCompiled(userId).result
creds ? find(authIds map (_.id) toSet)
} yield creds
def delete(authId: Long) =
creds.filter(_.authId === authId).delete
def findByToken(token: Array[Byte]) =
byToken(token).result
def deleteByToken(token: Array[Byte]) =
byToken(token).delete
}
示例7: ActorPostgresDriver
//设置package包名称以及导入依赖的类
package im.actor.server.db
import com.github.tminglei.slickpg._
import com.google.protobuf.ByteString
import com.google.protobuf.wrappers.{ Int32Value, StringValue }
trait ByteStringImplicits {
import slick.driver.PostgresDriver.api._
implicit val byteStringColumnType = MappedColumnType.base[ByteString, Array[Byte]](
{ bs ? bs.toByteArray },
{ ba ? ByteString.copyFrom(ba) }
)
}
trait ProtoWrappersImplicits {
import slick.driver.PostgresDriver.api._
//TODO: check if it used anywhere
implicit val stringValueColumnType = MappedColumnType.base[StringValue, String](
{ sv ? sv.value },
{ s ? StringValue(s) }
)
implicit val int32ValueColumnType = MappedColumnType.base[Int32Value, Int](
{ iv ? iv.value },
{ i ? Int32Value(i) }
)
}
trait ActorPostgresDriver extends ExPostgresDriver
with PgDateSupport
with PgDate2Support
with PgArraySupport
with PgLTreeSupport {
override val api =
new API with ArrayImplicits with LTreeImplicits with DateTimeImplicits with ByteStringImplicits with ProtoWrappersImplicits
}
object ActorPostgresDriver extends ActorPostgresDriver
示例8: FeaturesServiceImpl
//设置package包名称以及导入依赖的类
package im.actor.server.api.rpc.service.features
import akka.actor._
import com.google.protobuf.ByteString
import im.actor.api.rpc._
import im.actor.api.rpc.features.FeaturesService
import im.actor.api.rpc.misc.{ ResponseBool, ResponseVoid }
import im.actor.api.rpc.peers.ApiUserOutPeer
import im.actor.server.db.DbExtension
import im.actor.server.model.DeviceFeature
import im.actor.server.persist.DeviceFeatureRepo
import scala.concurrent.{ ExecutionContext, Future }
final class FeaturesServiceImpl(implicit system: ActorSystem) extends FeaturesService {
override implicit protected val ec: ExecutionContext = system.dispatcher
private val db = DbExtension(system).db
override def doHandleEnableFeature(
featureName: String,
args: Option[Array[Byte]],
clientData: ClientData
): Future[HandlerResult[ResponseVoid]] =
authorized(clientData) { client ?
for {
_ ? db.run(
DeviceFeatureRepo.enable(
DeviceFeature(
client.authId,
featureName,
args.map(ByteString.copyFrom).getOrElse(ByteString.EMPTY)
)
)
)
} yield Ok(ResponseVoid)
}
override def doHandleCheckFeatureEnabled(
userOutPeer: ApiUserOutPeer,
featureName: String,
clientData: ClientData
): Future[HandlerResult[ResponseBool]] =
authorized(clientData) { client ?
for {
exists ? db.run(DeviceFeatureRepo.exists(client.authId, featureName))
} yield Ok(ResponseBool(exists))
}
override def doHandleDisableFeature(
featureName: String,
clientData: ClientData
): Future[HandlerResult[ResponseVoid]] =
authorized(clientData) { client ?
for {
_ ? db.run(DeviceFeatureRepo.disable(client.authId, featureName))
} yield Ok(ResponseVoid)
}
}
示例9: ProtoOpsMacros
//设置package包名称以及导入依赖的类
package deaktator.pops.msgs
import java.io.InputStream
import com.google.protobuf.Descriptors.Descriptor
import com.google.protobuf.{ByteString, CodedInputStream, ExtensionRegistryLite, GeneratedMessage}
import scala.reflect.macros.blackbox
@macrocompat.bundle
class ProtoOpsMacros(val c: blackbox.Context) {
def materialize[A <: GeneratedMessage: c.WeakTypeTag]: c.Expr[ProtoOps[A]] = {
import c.universe._
val a = weakTypeOf[A]
val protoOps = weakTypeOf[ProtoOps[A]]
val serializable = weakTypeOf[Serializable]
val proto = a.companion
val descriptor = weakTypeOf[Descriptor]
val byteString = weakTypeOf[ByteString]
val extensionRegistryLite = weakTypeOf[ExtensionRegistryLite]
val codedInputStream = weakTypeOf[CodedInputStream]
val inputStream = weakTypeOf[InputStream]
c.Expr[ProtoOps[A]] {
q"""new $protoOps with $serializable {
def getDefaultInstance(): $a = $proto.getDefaultInstance()
def getDescriptor(): $descriptor = $proto.getDescriptor()
def parseFrom(data: $byteString): $a = $proto.parseFrom(data)
def parseFrom(data: $byteString, extensionRegistry: $extensionRegistryLite): $a =
$proto.parseFrom(data, extensionRegistry)
def parseFrom(data: Array[Byte]): $a = $proto.parseFrom(data)
def parseFrom(data: Array[Byte], extensionRegistry: $extensionRegistryLite): $a =
$proto.parseFrom(data, extensionRegistry)
def parseFrom(input: $inputStream): $a = $proto.parseFrom(input)
def parseFrom(input: $inputStream, extensionRegistry: $extensionRegistryLite): $a =
$proto.parseFrom(input, extensionRegistry)
def parseFrom(input: $codedInputStream): $a = $proto.parseFrom(input)
def parseFrom(input: $codedInputStream, extensionRegistry: $extensionRegistryLite): $a =
$proto.parseFrom(input, extensionRegistry)
def parseDelimitedFrom(input: $inputStream): $a = $proto.parseDelimitedFrom(input)
def parseDelimitedFrom(input: $inputStream, extensionRegistry: $extensionRegistryLite):
$a = $proto.parseDelimitedFrom(input, extensionRegistry)
}
"""
}
}
}
示例10: RuntimeProtoOps
//设置package包名称以及导入依赖的类
package deaktator.pops.msgs
import java.io.{IOException, InputStream}
import com.google.protobuf.Descriptors.Descriptor
import com.google.protobuf.{ByteString, CodedInputStream, ExtensionRegistryLite, GeneratedMessage, InvalidProtocolBufferException}
private[pops] final case class RuntimeProtoOps[A <: GeneratedMessage](messageClass: Class[A]) extends ProtoOps[A] {
def getDefaultInstance(): A =
messageClass.getMethod("getDefaultInstance").invoke(null).asInstanceOf[A]
def getDescriptor(): Descriptor =
messageClass.getMethod("getDescriptor").invoke(null).asInstanceOf[Descriptor]
@throws(classOf[InvalidProtocolBufferException])
def parseFrom(data: ByteString): A =
messageClass.getMethod("parseFrom", classOf[ByteString]).invoke(null, data).asInstanceOf[A]
@throws(classOf[InvalidProtocolBufferException])
def parseFrom(data: ByteString, extensionRegistry: ExtensionRegistryLite): A =
messageClass.getMethod("parseFrom", classOf[ByteString], classOf[ExtensionRegistryLite]).
invoke(null, data, extensionRegistry).asInstanceOf[A]
@throws(classOf[InvalidProtocolBufferException])
def parseFrom(data: Array[Byte]): A =
messageClass.getMethod("parseFrom", classOf[Array[Byte]]).invoke(null, data).asInstanceOf[A]
@throws(classOf[InvalidProtocolBufferException])
def parseFrom(data: Array[Byte], extensionRegistry: ExtensionRegistryLite): A =
messageClass.getMethod("parseFrom", classOf[Array[Byte]], classOf[ExtensionRegistryLite]).
invoke(null, data, extensionRegistry).asInstanceOf[A]
@throws(classOf[IOException])
def parseFrom(input: InputStream): A =
messageClass.getMethod("parseFrom", classOf[InputStream]).invoke(null, input).asInstanceOf[A]
@throws(classOf[IOException])
def parseFrom(input: InputStream, extensionRegistry: ExtensionRegistryLite): A =
messageClass.getMethod("parseFrom", classOf[InputStream], classOf[ExtensionRegistryLite]).
invoke(null, input, extensionRegistry).asInstanceOf[A]
@throws(classOf[IOException])
def parseDelimitedFrom(input: InputStream): A =
messageClass.getMethod("parseDelimitedFrom", classOf[InputStream]).invoke(null, input).asInstanceOf[A]
@throws(classOf[IOException])
def parseDelimitedFrom(input: InputStream, extensionRegistry: ExtensionRegistryLite): A =
messageClass.getMethod("parseDelimitedFrom", classOf[InputStream], classOf[ExtensionRegistryLite]).
invoke(null, input, extensionRegistry).asInstanceOf[A]
@throws(classOf[IOException])
def parseFrom(input: CodedInputStream): A =
messageClass.getMethod("parseFrom", classOf[CodedInputStream]).invoke(null, input).asInstanceOf[A]
@throws(classOf[IOException])
def parseFrom(input: CodedInputStream, extensionRegistry: ExtensionRegistryLite): A =
messageClass.getMethod("parseFrom", classOf[CodedInputStream], classOf[ExtensionRegistryLite]).
invoke(null, input, extensionRegistry).asInstanceOf[A]
}
示例11: PaymentProtocolSpec
//设置package包名称以及导入依赖的类
package fr.acinq.syscoin
import java.io._
import java.security._
import java.security.cert.{CertificateFactory, X509Certificate}
import java.security.spec.PKCS8EncodedKeySpec
import com.google.protobuf.ByteString
import org.syscoin.protocols.payments.Protos.{Output, PaymentDetails, PaymentRequest}
import org.bouncycastle.util.io.pem.PemReader
import org.junit.runner.RunWith
import org.scalatest.FlatSpec
import org.scalatest.junit.JUnitRunner
import scala.compat.Platform
@RunWith(classOf[JUnitRunner])
class PaymentProtocolSpec extends FlatSpec {
val keystore = KeyStore.getInstance("JKS")
keystore.load(classOf[PaymentProtocolSpec].getResourceAsStream("/cacerts"), null)
val aliases = keystore.aliases()
"Payment protocol" should "verify payment requests" in {
val stream = classOf[PaymentProtocolSpec].getResourceAsStream("/r1411736682.syscoinpaymentrequest")
val request = PaymentRequest.parseFrom(stream)
val (name, publicKey, trustAnchor) = PaymentProtocol.verifySignature(request, keystore)
assert(name === "www.syscoincore.org")
// check that we get an exception if we attempt to modify payment details
val details = PaymentDetails.parseFrom(request.getSerializedPaymentDetails)
val request1 = request.toBuilder.setSerializedPaymentDetails(details.toBuilder.setPaymentUrl("foo").build().toByteString).build()
intercept[RuntimeException] {
PaymentProtocol.verifySignature(request1, keystore)
}
}
it should "sign payment requests" in {
val factory = CertificateFactory.getInstance("X.509")
val cacert = factory.generateCertificate(classOf[PaymentProtocolSpec].getResourceAsStream("/cacert.pem")).asInstanceOf[X509Certificate]
val servercert = factory.generateCertificate(classOf[PaymentProtocolSpec].getResourceAsStream("/servercert.pem")).asInstanceOf[X509Certificate]
//val cert3 = factory.generateCertificate(classOf[PaymentProtocolSpec].getResourceAsStream("/ca-int2.crt")).asInstanceOf[X509Certificate]
val keyPair = new PemReader(new InputStreamReader(classOf[PaymentProtocolSpec].getResourceAsStream("/serverkey.pem"))).readPemObject()
val keyFactory = KeyFactory.getInstance("RSA")
val key = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyPair.getContent))
keystore.setCertificateEntry("foo", cacert)
val details = PaymentDetails.newBuilder()
.addOutputs(Output.newBuilder().setAmount(100).setScript(ByteString.EMPTY))
.setMemo("foo")
.setPaymentUrl("")
.setTime(Platform.currentTime)
val request = PaymentRequest.newBuilder()
.setPaymentDetailsVersion(1)
.setSerializedPaymentDetails(details.build().toByteString)
.build
val request1 = PaymentProtocol.sign(request, Seq(servercert), key)
val (name, publicKey, trustAnchor) = PaymentProtocol.verifySignature(request1, keystore)
assert(name === "Foobar")
}
}
示例12: AnyValue
//设置package包名称以及导入依赖的类
package com.avast.cactus.v3
import com.avast.cactus.GpbName
import com.google.protobuf.{ByteString, Message}
import scala.language.experimental.macros
case class AnyValue(typeUrl: String, @GpbName("value") bytes: ByteString)
object AnyValue {
private val DefaultTypePrefix: String = "type.googleapis.com"
def of[Gpb <: Message](gpb: Gpb): AnyValue = {
AnyValue(
typeUrl = s"$DefaultTypePrefix/${gpb.getDescriptorForType.getFullName}",
bytes = gpb.toByteString
)
}
}
示例13: SimplifiedTweetProcessorActor
//设置package包名称以及导入依赖的类
package org.eigengo.rsa.ingest.v100
import java.util.UUID
import akka.actor.{Actor, OneForOneStrategy, Props, SupervisorStrategy}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, Uri}
import akka.stream.ActorMaterializer
import cakesolutions.kafka.{KafkaProducer, KafkaProducerRecord, KafkaSerializer}
import com.google.protobuf.ByteString
import com.typesafe.config.Config
import org.apache.kafka.common.serialization.StringSerializer
import org.eigengo.rsa.Envelope
object SimplifiedTweetProcessorActor {
def props(config: Config): Props = {
val producerConf = KafkaProducer.Conf(
config.getConfig("tweet-image-producer"),
new StringSerializer,
KafkaSerializer[Envelope](_.toByteArray)
)
Props(classOf[SimplifiedTweetProcessorActor], producerConf)
}
}
class SimplifiedTweetProcessorActor(producerConf: KafkaProducer.Conf[String, Envelope]) extends Actor {
private[this] val producer = KafkaProducer(conf = producerConf)
implicit val _ = ActorMaterializer()
import scala.concurrent.duration._
override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 10.seconds) {
case _ ? SupervisorStrategy.Restart
}
override def receive: Receive = {
case TweetImage(handle, content) ?
producer.send(KafkaProducerRecord("tweet-image", handle,
Envelope(version = 100,
handle = handle,
ingestionTimestamp = System.nanoTime(),
processingTimestamp = System.nanoTime(),
messageId = UUID.randomUUID().toString,
correlationId = UUID.randomUUID().toString,
payload = content)))
case SimplifiedTweet(handle, mediaUrls) ?
mediaUrls.foreach { mediaUrl ?
import context.dispatcher
val request = HttpRequest(method = HttpMethods.GET, uri = Uri(mediaUrl))
val timeout = 1000.millis
Http(context.system).singleRequest(request).flatMap(_.entity.toStrict(timeout)).foreach { entity ?
self ! TweetImage(handle, ByteString.copyFrom(entity.data.toArray))
}
}
}
}
示例14: MessageWriter
//设置package包名称以及导入依赖的类
package com.trueaccord.scalapb.parquet
import com.google.protobuf.ByteString
import com.google.protobuf.Descriptors.FieldDescriptor.JavaType
import com.google.protobuf.Descriptors.{EnumValueDescriptor, FieldDescriptor}
import com.trueaccord.scalapb.{GeneratedMessage, Message}
import org.apache.parquet.io.api.Binary
import org.apache.parquet.Log
import org.apache.parquet.io.api.RecordConsumer
object MessageWriter {
val log = Log.getLog(this.getClass)
def writeTopLevelMessage[T <: GeneratedMessage with Message[T]](consumer: RecordConsumer, m: T) = {
consumer.startMessage()
writeAllFields(consumer, m)
consumer.endMessage()
}
private def writeAllFields[T <: GeneratedMessage](consumer: RecordConsumer, m: T): Unit = {
m.getAllFields.foreach {
case (fd, value) =>
consumer.startField(fd.getName, fd.getIndex)
if (fd.isRepeated) {
value.asInstanceOf[Seq[Any]].foreach {
v =>
writeSingleField(consumer, fd, v)
}
} else {
writeSingleField(consumer, fd, value)
}
consumer.endField(fd.getName, fd.getIndex)
}
}
private def writeSingleField(consumer: RecordConsumer, fd: FieldDescriptor, v: Any) = fd.getJavaType match {
case JavaType.BOOLEAN => consumer.addBoolean(v.asInstanceOf[Boolean])
case JavaType.INT => consumer.addInteger(v.asInstanceOf[Int])
case JavaType.LONG => consumer.addLong(v.asInstanceOf[Long])
case JavaType.FLOAT => consumer.addFloat(v.asInstanceOf[Float])
case JavaType.DOUBLE => consumer.addDouble(v.asInstanceOf[Double])
case JavaType.BYTE_STRING => consumer.addBinary(Binary.fromByteArray(v.asInstanceOf[ByteString].toByteArray))
case JavaType.STRING => consumer.addBinary(Binary.fromString(v.asInstanceOf[String]))
case JavaType.MESSAGE =>
consumer.startGroup()
writeAllFields(consumer, v.asInstanceOf[GeneratedMessage])
consumer.endGroup()
case JavaType.ENUM => consumer.addBinary(Binary.fromString(v.asInstanceOf[EnumValueDescriptor].getName))
case javaType =>
throw new UnsupportedOperationException("Cannot convert Protocol Buffer: unknown type " + javaType)
}
}
示例15: PrimitiveWrappersSpec
//设置package包名称以及导入依赖的类
package com.trueaccord.scalapb.json
import com.google.protobuf.ByteString
import jsontest.test3._
import org.json4s.JsonAST.{JBool, JDouble, JString}
import org.json4s.{JInt, JValue}
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
import org.scalatest.{FlatSpec, MustMatchers}
class PrimitiveWrappersSpec extends FlatSpec with MustMatchers {
"Empty object" should "give empty json for Wrapper" in {
JsonFormat.toJson(Wrapper()) must be (render(Map.empty[String, JValue]))
}
"primitive values" should "serialize properly" in {
JsonFormat.toJson(Wrapper(wBool = Some(false))) must be (render(Map("wBool" -> JBool(false))))
JsonFormat.toJson(Wrapper(wBool = Some(true))) must be (render(Map("wBool" -> JBool(true))))
JsonFormat.toJson(Wrapper(wDouble = Some(3.1))) must be (render(Map("wDouble" -> JDouble(3.1))))
JsonFormat.toJson(Wrapper(wFloat = Some(3.0f))) must be (render(Map("wFloat" -> JDouble(3.0))))
JsonFormat.toJson(Wrapper(wInt32 = Some(35544))) must be (render(Map("wInt32" -> JInt(35544))))
JsonFormat.toJson(Wrapper(wInt32 = Some(0))) must be (render(Map("wInt32" -> JInt(0))))
JsonFormat.toJson(Wrapper(wInt64 = Some(125))) must be (render(Map("wInt64" -> JString("125"))))
JsonFormat.toJson(Wrapper(wUint32 = Some(125))) must be (render(Map("wUint32" -> JInt(125))))
JsonFormat.toJson(Wrapper(wUint64 = Some(125))) must be (render(Map("wUint64" -> JString("125"))))
JsonFormat.toJson(Wrapper(wString = Some("bar"))) must be (render(Map("wString" -> JString("bar"))))
JsonFormat.toJson(Wrapper(wString = Some(""))) must be (render(Map("wString" -> JString(""))))
JsonFormat.toJson(Wrapper(wBytes = Some(ByteString.copyFrom(Array[Byte](3,5,4))))) must be (
render(Map("wBytes" -> JString("AwUE"))))
JsonFormat.toJson(Wrapper(wBytes = Some(ByteString.EMPTY))) must be (
render(Map("wBytes" -> JString(""))))
}
"primitive values" should "parse properly" in {
JsonFormat.fromJson[Wrapper](render(Map("wBool" -> JBool(false)))) must be (Wrapper(wBool = Some(false)))
JsonFormat.fromJson[Wrapper](render(Map("wBool" -> JBool(true)))) must be (Wrapper(wBool = Some(true)))
JsonFormat.fromJson[Wrapper](render(Map("wDouble" -> JDouble(3.1)))) must be (Wrapper(wDouble = Some(3.1)))
JsonFormat.fromJson[Wrapper](render(Map("wFloat" -> JDouble(3.0)))) must be (Wrapper(wFloat = Some(3.0f)))
JsonFormat.fromJson[Wrapper](render(Map("wInt32" -> JInt(35544)))) must be (Wrapper(wInt32 = Some(35544)))
JsonFormat.fromJson[Wrapper](render(Map("wInt32" -> JInt(0)))) must be (Wrapper(wInt32 = Some(0)))
JsonFormat.fromJson[Wrapper](render(Map("wInt64" -> JString("125")))) must be (Wrapper(wInt64 = Some(125)))
JsonFormat.fromJson[Wrapper](render(Map("wUint32" -> JInt(125)))) must be (Wrapper(wUint32 = Some(125)))
JsonFormat.fromJson[Wrapper](render(Map("wUint64" -> JString("125"))))must be (Wrapper(wUint64 = Some(125)))
JsonFormat.fromJson[Wrapper](render(Map("wString" -> JString("bar"))))must be (Wrapper(wString = Some("bar")))
JsonFormat.fromJson[Wrapper](render(Map("wString" -> JString("")))) must be (Wrapper(wString = Some("")))
JsonFormat.fromJson[Wrapper](render(Map("wBytes" -> JString("AwUE")))) must be (Wrapper(wBytes = Some(ByteString.copyFrom(Array[Byte](3,5,4)))))
JsonFormat.fromJson[Wrapper](render(Map("wBytes" -> JString("")))) must be (Wrapper(wBytes = Some(ByteString.EMPTY)))
}
}