2017-08-09 9 views
0

をI次のPython/Pysparkコードを持っている:Pythonのデータフレームに文字列のリストを変換する - pysparkパイソンsparksql

sql_command = ''' query '''' 
df = spark.sql(sql_command) 
ls_colnames = df.schema.names 
ls_colnames 
    ['id', 'level1', 'level2', 'level3', 'specify_facts'] 

cSchema = StructType([ 
    StructField("colname", StringType(), False) 
    ]) 
df_colnames = spark.createDataFrame(dataset_array,schema=cSchema) 

File "/opt/mapr/spark/spark-2.1.0/python/pyspark/sql/types.py", line 1366, in _verify_type raise TypeError("StructType can not accept object %r in type %s" % (obj, type(obj))) TypeError: StructType can not accept object 'id' in type class 'str'

私はCOLNAMESのスパークオブジェクトを取得するために何ができますか?私はあなたの質問を正しく理解している場合はわからない `

答えて

0

。しかし、指定されたリストに基づいてデータフレームを作成しようとしている場合は、同じコードに対して以下のコードを使用することができます。

from pyspark.sql import Row 
l = ['id', 'level1', 'level2', 'level3', 'specify_facts'] 
rdd1 = sc.parallelize(l) 
row_rdd = rdd1.map(lambda x: Row(x)) 
sqlContext.createDataFrame(row_rdd,['col_name']).show() 

よろしく、

Neeraj

関連する問題