【问题标题】:For/Loop/While in Databricks (Azure) SQLDatabricks (Azure) SQL 中的 For/Loop/While
【发布时间】:2021-12-24 08:58:19
【问题描述】:

有人知道我是否可以在 Databricks 中的 SQL 中进行迭代构造(如循环、while、for)吗?

【问题讨论】:

  • 写下你想实现什么
  • 我需要在表中插入多列(超过 200 列)。 then 之间的区别是子字符串函数中的一个参数:row_number。我从 1 开始,需要到 250 为止。

标签: sql azure loops while-loop databricks


【解决方案1】:

AFAIK,SQL 中没有这种直接的方法可以遍历 DataFrame。另一方面,pyspark 具有多个函数来执行数据循环。包括map()foreach()pandas

请看下面的例子:

# Using pandas
import pandas as pd
spark.conf.set("spark.sql.execution.arrow.enabled", "true")
pandasDF = df.toPandas()
for index, row in pandasDF.iterrows():
    print(row['firstname'], row['gender'])

如需了解更多,请访问https://sparkbyexamples.com/pyspark/pyspark-loop-iterate-through-rows-in-dataframe/#using-pandas-to-iterate

【讨论】:

  • 您好!我的整个代码都在 SQL 中,我对 pyspark 不是很熟悉。得考虑一下。不过谢谢!
猜你喜欢
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 2014-01-11
  • 2015-10-09
  • 2020-05-02
  • 1970-01-01
  • 2015-07-06
相关资源
最近更新 更多