简体   繁体   English

FS2按顺序运行流

[英]FS2 Running streams in sequence

I have a fairly simple use case. 我有一个相当简单的用例。 I have two web service calls one fetches products and another fetches relationship. 我有两个Web服务调用,一个获取产品,另一个获取关系。 I want to run fetchProducts() first extract a field from the set of products and then pass the output to fetchRelationships(ids: Seq[String]) so that I can then set the relationship back on the product. 我想运行fetchProducts()首先从产品集中提取一个字段,然后将输出传递给fetchRelationships(ids:Seq [String]),以便可以在产品上重新设置关系。 Here's the code: 这是代码:

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])

I am constrained by the external Api to get the results in batches. 我受到外部Api的约束,无法批量获取结果。 So I'm not sure how to express this using fs2 or if I should be using it at all. 所以我不确定如何使用fs2来表达它,或者我是否应该使用它。

Unfortunately you code in the question doesn't match your text description and misses quite a few important bits (such as whole Relationship class). 不幸的是,您在问题中编写的代码与您的文本描述不匹配,并且错过了很多重要的内容(例如整个Relationship类)。 Also it is not clear what 也不清楚是什么

I am constrained by the external Api to get the results in batches 我受到外部Api的限制,无法批量获取结果

really means. 真正意思。 Also it is not clear why all fields in Product including productId are Option . 也不清楚为什么Product包括productId所有字段都是Option

The following code compiles and might or might not be what you need: 以下代码可以编译,可能不是您需要的代码:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM