scala 在 Spark 中将 BigInt 转换为 Int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/41736642/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Casting BigInt to Int in Spark
提问by jojo_Berlin
Hi I'm trying to cast a BigIntto an intin order to generate Ratingclasses. 
I only want to use instances that are small enough to fit into an in I use the following code:
嗨,我正在尝试将 a 转换BigInt为 anint以生成评级类。我只想使用小到足以放入 in 我使用以下代码的实例:
val tup=rs.select("kunden_nr","product_list") 
val rdd=tup.rdd.map(row=>(row.getAs[BigInt](0),row.getAs[Seq[Int]](1)))
val fs=rdd.filter(el=>el._1.isValidInt)
fs.count()
rdd.count()
The fs count delivers the following exception in Zepplin:
在 Zepplin 中 fs 计数会产生以下异常:
java.lang.ClassCastException: java.lang.Long cannot be cast to scala.math.BigInt
java.lang.ClassCastException: java.lang.Long cannot be cast to scala.math.BigInt
采纳答案by Pablo Francisco Pérez Hidalgo
Casting is like changing "the glasses" your code use to represent what is referenced by your value and not actually changing the referenced content nor changing the reference to point to a new BigIntinstance.
转换就像更改代码用来表示值引用的内容的“眼镜”,而不是实际更改引用的内容,也不更改引用以指向新BigInt实例。
That implies that you need to get your value with the type it really has and then build a BigIntinstance from it:
这意味着您需要使用它真正拥有的类型获取您的值,然后BigInt从中构建一个实例:
BigInt(row.getAs[Long](0))
Following the same reasoning, you can create an Intinstance from the Longas follows:
按照相同的推理,您可以Int从Long以下内容创建一个实例:
row.getAs[Long](0).toInt
But it might overflow the integer type representation range.
但它可能会溢出整数类型表示范围。
回答by user7371662
When I use below to cast , the value of number will be changed!
当我使用下面的强制转换时,数字的值会改变!
table.col("id").cast("long") //java
回答by user 923227
I noticed that the overflow is happening for me fairly often with .toIntso I tried Adding the remainder and the quotient.
我注意到溢出对我来说经常发生,.toInt所以我尝试添加余数和商。
((rec.getLong(0) % Int.MaxValue) + (rec.getLong(0) / Int.MaxValue)).toInt
This was better as I was getting 232 unique values in place of 234 but with toInt I was getting 6 unique values in place of 234.
这更好,因为我得到了 232 个唯一值而不是 234,但是使用 toInt 我得到了 6 个唯一值代替了 234。

