提供序列作为接受varargs的方法的参数

时间:2017-09-21 18:50:06

标签: scala apache-spark

Spark DataFrame中,我们有一个select方法,其第二个参数为varargs

  @scala.annotation.varargs
  def select(col: String, cols: String*): DataFrame = 
        select((col +: cols).map(Column(_)) : _*)

我想使用select

调用Sequence
val ProductCols = Seq("prdct_id", "prdct_tag")

首选项将按以下方式调用:

myDataFrame.select(ProductCols: _*)

但是,这并没有解决上面显示的方法,因此使用了以下内容:

myDataFrame.select(ProductCols.head, ProductCols.tail: _*)

有没有办法只发送ProductCols一次 - 而varargs会接受它?

1 个答案:

答案 0 :(得分:1)

您应该将String包装到列:

import MySQLdb
import base64
db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="test",         # your username
                     passwd="test",  # your password
                     db="test")
cur = db.cursor()


cur.execute("SELECT * FROM attachments")

#looping through row 4 where all blobs are located

for row in cur.fetchall():
    blob_read = row[4]

#(error happen here while decoding)

    decoding = blob_read.decode('base64')
    print '<img src="data:image/jpg;base64,"'+ decoding + '/>'

    filename = row[2]

#(extracted the files to test if images are corrupted; they are not)

       with open(filename, 'wb') as output_file:
        output_file.write(blob_read)

db.close()

也写 Df.select(cols.map(x => col(x)) :_*)