当前位置: 动力学知识库 > 问答 > 编程问答 >

what is the output of a $ some string in scala

问题描述:

This question already has an answer here:

  • What does dollar sign do in scala

    1 answer

网友答案:

This is not a scala thing.

scala> val name = "something"
name: String = something

scala> println($"name")
<console>:12: error: value $ is not a member of StringContext
       println($"name")
               ^

Rather a spark thing, which seems to represent a column.

See the org.apache.spark.sql.SQLImplicits code over here,

  implicit class StringToColumn(val sc: StringContext) {
    def $(args: Any*): ColumnName = {
      new ColumnName(sc.s(args: _*))
    }
  }

You could simply do dataframe.select("columnname").show or dataframe.select(col("columnname")).show too, but dataframe.select($"columnname") to mutate the column value, like you are incrementing age in your example.

eg.

Given a dataframe,

 +----+-------+
 | age|   name|
 +----+-------+
 |null|Michael|
 |  30|   Andy|
 |  19| Justin|
 +----+-------+

scala> dataframe.select($"name".as('myname)).show()
+-------+
| myname|
+-------+
|Michael|
|   Andy|
| Justin|
+-------+

scala> dataframe.select("age"+1).show()
org.apache.spark.sql.AnalysisException: cannot resolve '`age1`' given input columns: [age, name];;
'Project ['age1]

Other example to use $ could be filter based on column values,

dataframe.filter($"age" > 28).show()

So, basically, you are making it a variable(of type Column) with $"" in Spark.

Scala has ${} while concatenating the variables, (aka String interpolation)

scala> val printMe = "prayagupd"
printMe: String = prayagupd

scala> println(s"value = $printMe")
value = prayagupd
分享给朋友:
您可能感兴趣的文章:
随机阅读: