本文整理汇总了Scala中akka.stream.IOResult类的典型用法代码示例。如果您正苦于以下问题:Scala IOResult类的具体用法?Scala IOResult怎么用?Scala IOResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IOResult类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MovieListPipeline
//设置package包名称以及导入依赖的类
package com.stacktrace.yo.scrapeline.imdb.pipelines
import java.nio.file.Paths
import akka.NotUsed
import akka.stream.scaladsl.{FileIO, Flow, Keep, Sink, Source}
import akka.stream.{ActorMaterializer, IOResult}
import akka.util.ByteString
import com.stacktrace.yo.scrapeline.core.ScrapeClient.jsoup
import com.stacktrace.yo.scrapeline.core._
import com.stacktrace.yo.scrapeline.imdb.Domain.MovieNameAndDetailUrl
import net.ruippeixotog.scalascraper.dsl.DSL._
import net.ruippeixotog.scalascraper.model.Document
import net.ruippeixotog.scalascraper.scraper.ContentExtractors.elementList
import scala.concurrent.Future
class MovieListPipeline(implicit val m: ActorMaterializer) {
def getPipelineSource: Source[jsoup.DocumentType, NotUsed] = Source.single(ScrapeClient.scrape("http://www.the-numbers.com/movie/budgets/all"))
def getParseFlow: Flow[Document, MovieNameAndDetailUrl, NotUsed] = {
Flow[Document]
.mapConcat(doc => {
val table = doc >> elementList("table tr")
val movieLinkTuples = table.flatMap(tr => {
val name = tr >> elementList("tr b a")
name.map(
link => {
MovieNameAndDetailUrl(link.text, "http://www.the-numbers.com/" + link.attr("href"))
}
)
})
movieLinkTuples
})
}
def getPipeOut: Sink[MovieNameAndDetailUrl, Future[IOResult]] = Flow[MovieNameAndDetailUrl]
.map(s => ByteString(s.name + "\n"))
.toMat(FileIO.toPath(Paths.get("movie.txt")))(Keep.right)
def buildAndRun: Future[IOResult] = {
getPipelineSource
.via(getParseFlow)
.runWith(getPipeOut)
}
}
示例2: toPath
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.ftp.scaladsl
import akka.NotUsed
import akka.stream.IOResult
import akka.stream.alpakka.ftp.impl.{FtpLike, FtpSourceFactory, FtpSourceParams, FtpsSourceParams, SftpSourceParams}
import akka.stream.alpakka.ftp.{FtpFile, RemoteFileSettings}
import akka.stream.scaladsl.{Sink, Source}
import akka.util.ByteString
import net.schmizz.sshj.SSHClient
import org.apache.commons.net.ftp.FTPClient
import scala.concurrent.Future
sealed trait FtpApi[FtpClient] { _: FtpSourceFactory[FtpClient] =>
def toPath(
path: String,
connectionSettings: S,
append: Boolean = false
): Sink[ByteString, Future[IOResult]] =
Sink.fromGraph(createIOSink(path, connectionSettings, append))
protected[this] implicit def ftpLike: FtpLike[FtpClient, S]
}
object Ftp extends FtpApi[FTPClient] with FtpSourceParams
object Ftps extends FtpApi[FTPClient] with FtpsSourceParams
object Sftp extends FtpApi[SSHClient] with SftpSourceParams
示例3: listFiles
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.ftp
import akka.NotUsed
import akka.stream.alpakka.ftp.FtpCredentials.AnonFtpCredentials
import akka.stream.alpakka.ftp.scaladsl.Sftp
import akka.stream.IOResult
import akka.stream.scaladsl.{Sink, Source}
import akka.util.ByteString
import scala.concurrent.Future
import java.net.InetAddress
trait BaseSftpSpec extends SftpSupportImpl with BaseSpec {
//#create-settings
val settings = SftpSettings(
InetAddress.getByName("localhost"),
getPort,
AnonFtpCredentials,
strictHostKeyChecking = false,
knownHosts = None,
sftpIdentity = None
)
//#create-settings
protected def listFiles(basePath: String): Source[FtpFile, NotUsed] =
Sftp.ls(basePath, settings)
protected def retrieveFromPath(path: String): Source[ByteString, Future[IOResult]] =
Sftp.fromPath(path, settings)
protected def storeToPath(path: String, append: Boolean): Sink[ByteString, Future[IOResult]] =
Sftp.toPath(path, settings, append)
}
示例4: listFiles
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.ftp
import akka.NotUsed
import akka.stream.alpakka.ftp.FtpCredentials.AnonFtpCredentials
import akka.stream.alpakka.ftp.scaladsl.Ftps
import akka.stream.IOResult
import akka.stream.scaladsl.{Sink, Source}
import akka.util.ByteString
import scala.concurrent.Future
import java.net.InetAddress
trait BaseFtpsSpec extends FtpsSupportImpl with BaseSpec {
//#create-settings
val settings = FtpsSettings(
InetAddress.getByName("localhost"),
getPort,
AnonFtpCredentials,
binary = true,
passiveMode = true
)
//#create-settings
protected def listFiles(basePath: String): Source[FtpFile, NotUsed] =
Ftps.ls(basePath, settings)
protected def retrieveFromPath(path: String): Source[ByteString, Future[IOResult]] =
Ftps.fromPath(path, settings)
protected def storeToPath(path: String, append: Boolean): Sink[ByteString, Future[IOResult]] =
Ftps.toPath(path, settings, append)
}
示例5: MovieNameToIMDBPipeline
//设置package包名称以及导入依赖的类
package com.stacktrace.yo.scrapeline.imdb.pipelines
import java.net.URLEncoder
import java.nio.file.Paths
import akka.stream.scaladsl.{FileIO, Flow, Framing, Source}
import akka.stream.{ActorMaterializer, IOResult}
import akka.util.ByteString
import akka.{Done, NotUsed}
import com.stacktrace.yo.scrapeline.imdb.Domain.MovieNameAndDetailUrl
import scala.concurrent.{ExecutionContext, Future}
class MovieNameToIMDBPipeline(implicit val m: ActorMaterializer, implicit val ec: ExecutionContext) {
private def getPipelineSource: Source[String, Future[IOResult]] = {
FileIO.fromPath(Paths.get("movie.txt"))
.via(Framing.delimiter(ByteString("\n"), 256)
.map(_.utf8String)
)
}
private def getDetailUrlFlow: Flow[String, MovieNameAndDetailUrl, NotUsed] = {
Flow[String]
.mapAsyncUnordered(100)(mapPipeToImdbSearch)
}
private def mapPipeToImdbSearch(in: String): Future[MovieNameAndDetailUrl] = Future {
val encodedString: String = URLEncoder.encode(in, "UTF-8")
MovieNameAndDetailUrl(in, "http://www.imdb.com/find?ref_=nv_sr_fn&q=" + encodedString + "&s=tt")
}
def getOutput: Future[Done] = {
getPipelineSource
.via(getDetailUrlFlow)
.via(new IMDBSearchSubPipe().getSubFlow)
.runForeach(x => println(x.name + ":" + x.url))
}
}
示例6: BlueprintExample
//设置package包名称以及导入依赖的类
package code
import java.nio.file.FileSystems
import akka.NotUsed
import akka.stream.IOResult
import akka.stream.scaladsl.{FileIO, Flow, Framing, Keep, RunnableGraph}
import akka.util.ByteString
import scala.concurrent.Future
object BlueprintExample extends AkkaStreamsApp {
val file = this.getClass.getClassLoader.getResource("current_inventory.csv")
val inPath = FileSystems.getDefault.getPath(file.getPath)
val outPath = FileSystems.getDefault.getPath("no_inventory.csv")
val fileSource = FileIO.fromPath(inPath)
val fileSink = FileIO.toPath(outPath)
val csvHandler: Flow[String, List[String], NotUsed] =
Flow[String]
.drop(1)
.map(_.split(",").toList)
.log("csvHandler")
val lowInventoryFlow: RunnableGraph[Future[IOResult]] =
fileSource
.via(Framing.delimiter(ByteString("\n"), Integer.MAX_VALUE))
.map(_.utf8String)
.log("Before CSV Handler")
.via(csvHandler)
.filter(list => list(2).toInt == 0)
.log("After filter")
.map { list =>
ByteString(list.mkString(",")) ++ ByteString("\n")
}.toMat(fileSink)(Keep.right)
override def akkaStreamsExample: Future[_] =
lowInventoryFlow.run()
runExample
}
示例7: healthCheck
//设置package包名称以及导入依赖的类
package io.pixelart.ambry.client.infrastructure.service
import java.nio.file.{ Files, Path }
import akka.http.scaladsl.model.ContentType
import akka.stream.IOResult
import akka.stream.scaladsl.{ Source, FileIO }
import com.typesafe.scalalogging.StrictLogging
import io.pixelart.ambry.client.application.{ ActorImplicits, AbstractAmbryClientService }
import io.pixelart.ambry.client.domain.model.httpModel._
import io.pixelart.ambry.client.infrastructure.adapter.{ AmbryClient }
import scala.concurrent.Future
protected[client] trait AmbryService extends AbstractAmbryClientService with StrictLogging with ActorImplicits {
this: AmbryClient =>
private[client] val ambryUri: AmbryUri
override def healthCheck: Future[AmbryHealthStatusResponse] =
healthCheckRequest
override def getFileProperty(ambryId: AmbryId): Future[AmbryBlobInfoResponse] =
getBlobInfoRequest(ambryId)
//todo: override def getFileUserMetadata(ambryId: AmbryId): AmbryUMResponse = ???
override def getFile(ambryId: AmbryId): Future[AmbryGetBlobResponse] =
getBlobRequest(ambryId)
override def getFile(ambryId: AmbryId, localPath: Path): Future[IOResult] =
getBlobRequest(ambryId)
.flatMap { result =>
result
.blob
.runWith(FileIO.toPath(localPath))
}
//todo: ttl to DateTime
override def postFileFromPath(
localPath: Path,
serviceId: AmbryServiceId,
contentType: ContentType,
ttl: Long = -1,
prvt: Boolean = false,
ownerId: AmbryOwnerId
): Future[AmbryBlobUploadResponse] = {
val fileSource = FileIO.fromPath(localPath)
val size = Files.size(localPath)
uploadBlobRequest(UploadBlobRequestData(fileSource, size, serviceId, contentType, ttl, prvt, ownerId))
}
override def postFile(uploadBlobRequestData: UploadBlobRequestData): Future[AmbryBlobUploadResponse] =
uploadBlobRequest(uploadBlobRequestData)
override def deleteFile(ambryId: AmbryId): Future[Boolean] =
deleteBlobRequest(ambryId)
}
示例8: WriteHello
//设置package包名称以及导入依赖的类
package com.example
import java.io.File
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.{IOResult, ActorMaterializer}
import akka.stream.scaladsl.{FileIO, Source}
import akka.util.ByteString
import scala.concurrent.Future
object WriteHello {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("QuickStart")
implicit val materializer = ActorMaterializer()
val source: Source[Int, NotUsed] = Source(1 to 10000)
val factorials = source.scan(BigInt(1))((acc, next) => acc * next)
val result: Future[IOResult] =
factorials
.map(num => ByteString(s"$num\n"))
.runWith(FileIO.toFile(new File("target/factorials.txt")))
println("Hello, world!")
Thread.sleep(1000)
system.shutdown()
system.awaitTermination() }
}
示例9: ReusableWriterHello
//设置package包名称以及导入依赖的类
package com.example
import java.io.File
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.scaladsl._
import akka.stream.{ActorMaterializer, IOResult}
import akka.util.ByteString
import scala.concurrent.Future
object ReusableWriterHello {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("QuickStart")
implicit val materializer = ActorMaterializer()
val source: Source[Int, NotUsed] = Source(1 to 10000)
val factorials = source.scan(BigInt(1))((acc, next) => acc * next)
val result: Future[IOResult] =
factorials
.map(_.toString)
.runWith(lineSink("target/factorials2.txt"))
println("Hello, world!")
system.shutdown()
}
def lineSink(filename: String): Sink[String, Future[IOResult]] =
Flow[String]
.map(s => ByteString(s + "\n"))
.toMat(FileIO.toFile(new File(filename)))(Keep.right)
}