spark to函数

本文深入探讨了Apache Spark中`to`函数的使用,详细解释了如何在Scala编程环境中利用该函数进行数据转换和操作。内容包括`to`函数的基本语法、参数说明、常见用例及其在大数据处理中的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

scala> "astron".to
to          toBuffer      toDouble       toInt        toList        toMap   toShort    toTraversable   
toArray     toByte        toFloat        toIterable   toLong        toSeq   toStream   toUpperCase     
toBoolean   toCharArray   toIndexedSeq   toIterator   toLowerCase   toSet   toString   toVector        

scala> "astron".to
res0: scala.collection.immutable.IndexedSeq[Char] = Vector(a, s, t, r, o, n)

scala> "astron".toBuffer
res1: scala.collection.mutable.Buffer[Char] = ArrayBuffer(a, s, t, r, o, n)

scala> "astron".toDouble
java.lang.NumberFormatException: For input string: "astron"
  at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
  at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
  at java.lang.Double.parseDouble(Double.java:538)
  at scala.collection.immutable.StringLike$class.toDouble(StringLike.scala:284)
  at scala.collection.immutable.StringOps.toDouble(StringOps.scala:29)
  ... 48 elided

scala> "astron".toInt
java.lang.NumberFormatException: For input string: "astron"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Integer.parseInt(Integer.java:580)
  at java.lang.Integer.parseInt(Integer.java:615)
  at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:272)
  at scala.collection.immutable.StringOps.toInt(StringOps.scala:29)
  ... 48 elided

scala> "astron".toList
res4: List[Char] = List(a, s, t, r, o, n)

scala> "astron".toMap
<console>:24: error: Cannot prove that Char <:< (T, U).
       "astron".toMap
                ^

scala> "astron".toShort
java.lang.NumberFormatException: For input string: "astron"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Integer.parseInt(Integer.java:580)
  at java.lang.Short.parseShort(Short.java:118)
  at java.lang.Short.parseShort(Short.java:144)
  at scala.collection.immutable.StringLike$class.toShort(StringLike.scala:268)
  at scala.collection.immutable.StringOps.toShort(StringOps.scala:29)
  ... 48 elided

scala> "astron".toTraversable
res7: Traversable[Char] = astron

scala> "astron".toArray
res8: Array[Char] = Array(a, s, t, r, o, n)

scala> "astron".toByte
java.lang.NumberFormatException: For input string: "astron"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Integer.parseInt(Integer.java:580)
  at java.lang.Byte.parseByte(Byte.java:149)
  at java.lang.Byte.parseByte(Byte.java:175)
  at scala.collection.immutable.StringLike$class.toByte(StringLike.scala:264)
  at scala.collection.immutable.StringOps.toByte(StringOps.scala:29)
  ... 48 elided

scala> "astron".toFloat
java.lang.NumberFormatException: For input string: "astron"
  at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
  at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
  at java.lang.Float.parseFloat(Float.java:451)
  at scala.collection.immutable.StringLike$class.toFloat(StringLike.scala:280)
  at scala.collection.immutable.StringOps.toFloat(StringOps.scala:29)
  ... 48 elided

scala> "astron".toIterable
res11: Iterable[Char] = astron

scala> "astron".toLong
java.lang.NumberFormatException: For input string: "astron"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Long.parseLong(Long.java:589)
  at java.lang.Long.parseLong(Long.java:631)
  at scala.collection.immutable.StringLike$class.toLong(StringLike.scala:276)
  at scala.collection.immutable.StringOps.toLong(StringOps.scala:29)
  ... 48 elided

scala> "astron".toSeq
res13: Seq[Char] = astron

scala> "astron".toStream
res14: scala.collection.immutable.Stream[Char] = Stream(a, ?)

scala> "astron".toUpperCase     
res15: String = ASTRON

scala> "astron".toBoolean
java.lang.IllegalArgumentException: For input string: "astron"
  at scala.collection.immutable.StringLike$class.parseBoolean(StringLike.scala:290)
  at scala.collection.immutable.StringLike$class.toBoolean(StringLike.scala:260)
  at scala.collection.immutable.StringOps.toBoolean(StringOps.scala:29)
  ... 48 elided

scala> "astron".toCharArray
res17: Array[Char] = Array(a, s, t, r, o, n)

scala> "astron".toIndexedSeq
res18: scala.collection.immutable.IndexedSeq[Char] = Vector(a, s, t, r, o, n)

scala> "astron".toIterator
res19: Iterator[Char] = non-empty iterator

scala> "astron".toLowerCase
res20: String = astron

scala> "astron".toSet
res21: scala.collection.immutable.Set[Char] = Set(s, n, t, a, r, o)

scala> "astron".toString
res22: String = astron

scala> "astron".toVector 
res23: Vector[Char] = Vector(a, s, t, r, o, n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值