Upacking a list to select multiple columns from a spark data frame

后端 未结 7 1158
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 14:03

I have a spark data frame df. Is there a way of sub selecting a few columns using a list of these columns?

scala> df.columns
res0: Array[Stri         


        
7条回答
  •  悲哀的现实
    2020-12-07 14:25

    you can do like this

    String[] originCols = ds.columns();
    ds.selectExpr(originCols)
    

    spark selectExp source code

         /**
       * Selects a set of SQL expressions. This is a variant of `select` that accepts
       * SQL expressions.
       *
       * {{{
       *   // The following are equivalent:
       *   ds.selectExpr("colA", "colB as newName", "abs(colC)")
       *   ds.select(expr("colA"), expr("colB as newName"), expr("abs(colC)"))
       * }}}
       *
       * @group untypedrel
       * @since 2.0.0
       */
      @scala.annotation.varargs
      def selectExpr(exprs: String*): DataFrame = {
        select(exprs.map { expr =>
          Column(sparkSession.sessionState.sqlParser.parseExpression(expr))
        }: _*)
      }
    

提交回复
热议问题