如何在scala中转换Option[Future[T]]为Future[Option[T]]?
我想用它:
val customerAddresses = for {
a <- addressDAO.insert(ca.address) // Future[Address]
ia <- ca.invoiceAddress.map(addressDAO.insert) // Option[Future[Address]]
} yield (a, ia) // Invalid value have to be two futures
Run Code Online (Sandbox Code Playgroud)
这里签名插入方法
def insert(address: Address): Future[Address]
Run Code Online (Sandbox Code Playgroud)
ca 是CustomerData
case class CustomerData(address: Address, invoiceAddress: Option[Address])
Run Code Online (Sandbox Code Playgroud) 我有一个执行大型连接的Spark应用程序
val joined = uniqueDates.join(df, $"start_date" <= $"date" && $"date" <= $"end_date")
Run Code Online (Sandbox Code Playgroud)
然后将生成的DataFrame汇总到一个可能有13k行的一个.在连接过程中,作业失败,并显示以下错误消息:
Caused by: org.apache.spark.SparkException: Job aborted due to stage failure: Total size of serialized results of 78021 tasks is bigger than spark.driver.maxResultSize (2.0 GB)
Run Code Online (Sandbox Code Playgroud)
这是在没有设置之前发生的spark.driver.maxResultSize,所以我设置了spark.driver.maxResultSize=2G.然后,我对连接条件稍作修改,错误重新出现.
编辑:在调整集群,我也加倍数据帧呈现在分区数.coalesce(256)的.coalesce(512),所以我不能确定这是因为,不是.
我的问题是,既然我没有向司机收集任何东西,为什么要spark.driver.maxResultSize在这里重要?驱动程序的内存是否用于我不知道的连接中的某些内容?
为什么变量可以调用(初始化自身)一个调用相同变量的方法(似乎是一个递归)?我希望看到无休止的递归,但它编译没有错误.任何解释?
class Forward {
static int test(){
return i;
}
static int i=test();
public static void main(String[] args) {
System.out.println(test()); //sout= 0
System.out.println(i); //sout =0
}
}
Run Code Online (Sandbox Code Playgroud)
另一个例子.为什么在引用时引用Backwards.j工作j会产生错误("非法转发引用"):
class Backwards{
//static int i=j; //illegal forward reference;
static int i=Backwards.j; //reference through class works
static int j=i;
public static void main(String[] args) {
System.out.println(i);
System.out.println(j);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的代码,
case class Person(name: String, age:Int)
import scala.reflect.runtime.universe._
val t1 = typeOf[Person]
val t2 = t1.dealias
println(t1 == t2)
Run Code Online (Sandbox Code Playgroud)
它输出为true,所以我想问一下Type.dealias的用途是什么?我什么时候应该使用它?一些代码示例会有所帮助
我之所以这样问,是因为当我阅读spark代码时ScalaReflection,它几乎总是dealias在使用类型之前使用
我foreach(_ => ())在使用Monix流的代码中发现了这一点,但我不明白它的含义。有人可以解释一下吗?
monix_concurrent_subject.foreach(_ => ())
def vertify(a:Int):Int= {
val result = 3
if(a>4) {
val result = a
}
return result
}
Run Code Online (Sandbox Code Playgroud)
我定义了上面的函数,然后使用它:
scala> vertify(5)
res4: Int = 3
Run Code Online (Sandbox Code Playgroud)
为什么结果3而不是5?
scala ×5
apache-spark ×1
future ×1
if-statement ×1
java ×1
memory ×1
recursion ×1
reference ×1
return ×1