FS2按顺序运行流

时间:2018-01-10 22:50:17

标签: scala scala-cats scalaz-stream http4s fs2

我有一个相当简单的用例。我有两个Web服务调用,一个获取产品,另一个获取关系。我想运行fetchProducts()首先从产品集中提取一个字段,然后将输出传递给fetchRelationships(ids:Seq [String]),这样我就可以在产品上设置关系了。这是代码:

def fetchProducts(): Stream[IO, Seq[Product]]= {
 //webservice call
}

def fetchRelationship(ids: Seq[Product]): Stream[IO, Seq[Relationship]] = {
 //webservice call
}

//Pseudocode. How can I do this with fs2 Streams?
def process = {
      val prods = fetchProducts() //run this first
      val prodIds = prods.flatMap(i => i.productId)
      val rels = fetchRelationships(prodIds) //run after all all products are fetched 
      prods.forEach(p => p.setRelation(rels.get(p.id))
    }
}

 case class Product(productId: Option[String],
                        name: Option[String],
                        description: Option[String],
                        brandName: Option[String])

我受到外部Api的限制,可以批量获得结果。因此,我不确定如何使用fs2表达这一点,或者我是否应该使用它。

1 个答案:

答案 0 :(得分:1)

不幸的是,问题中的代码与您的文字描述不符,并且错过了相当多的重要位(例如整个Relationship类)。还不清楚是什么

  

我受到外部Api的限制,可以批量获得结果

真的意味着。此外,还不清楚为什么Product中包含productId的所有字段都为Option

以下代码编译,可能或可能不是您需要的:

case class Product(productId: Option[String],
                   name: Option[String],
                   description: Option[String],
                   brandName: Option[String],
                   relationships: mutable.ListBuffer[Relationship]) {

}

case class Relationship(productId: String, someInfo: String)

def fetchProducts(): Stream[IO, Seq[Product]] = {
  //webservice call
  ???
}

//    def fetchRelationships(ids: Seq[Product]): Stream[IO, Seq[Relationship]] = {
def fetchRelationships(ids: Seq[String]): Stream[IO, Seq[Relationship]] = {
  //webservice call
  ???
}

def process():  = {
  val prods = fetchProducts() //run this first
  val prodsAndRels: Stream[IO, (Seq[Product], Seq[Relationship])] = prods.flatMap(ps => fetchRelationships(ps.map(p => p.productId.get)).map(rs => (ps, rs)))

  val prodsWithFilledRels: Stream[IO, immutable.Seq[Product]] = prodsAndRels.map({ case (ps, rs) => {
    val productsMap = ps.map(p => (p.productId.get, p)).toMap
    rs.foreach(rel => productsMap(rel.productId).relationships += rel)
    ps.toList
  }
  })
  prodsWithFilledRels
}