添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
有腹肌的充值卡  ·  pycharm console 清屏 ...·  2 月前    · 
爽快的大熊猫  ·  【Python】PyCharm ...·  1 周前    · 
八块腹肌的春卷  ·  Mysql error 1452 - ...·  1 年前    · 
腼腆的饼干  ·  JPA中save和saveAndFlush的 ...·  1 年前    · 
神勇威武的红酒  ·  使用 Power BI ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Writing in a table while including the schema is failing on PySpark with Python 3.

Here are the steps that are working.

a = sc.textFile("ad_actions.csv")
b = a.map(lambda x:  x.split('||')).toDF()
b.write.saveAsTable('AD_ACTIONS', mode='append')

But if I try to add the schema, it is failing:

a = sc.textFile("ad_actions.csv")
b = a.map(lambda x:  x.split('||')).toDF(schema=sqlContext.table("AD_ACTIONS").schema)
b.write.saveAsTable('AD_ACTIONS', mode='append')
  

AttributeError: 'str' object has no attribute 'toordinal'

Does any of you know how I can fix this? Do you need to see anything?

Means that you want to cast your string to ordinal. This a function for date.

So, the problem is that you are not able to convert the data to the schema that you want.

What I suggest you, take only the name of the columns. Like this:

a = sc.textFile("ad_actions.csv")
b = a.map(lambda x:  x.split('||')).toDF(sqlContext.table("AD_ACTIONS").schema.names)
b.write.saveAsTable('AD_ACTIONS', mode='append')

This will work fine, due to the schema convertion will be handle by your Metadata Store.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.