set.createOrReplaceTempView("input1");
String look = "select case when length(date)>0 then 'Y' else 'N' end as date from input1";
Dataset<Row> Dataset_op = spark.sql(look);
Dataset_op.show();
在上面的代碼中,資料框“set”有 10 列,我已經完成了其中一列的驗證(即)“日期”。它單獨回傳日期列。
我的問題是如何在單個資料框中回傳所有帶有驗證日期列的列?
有沒有辦法在不手動選擇 select 陳述句中的所有列的情況下獲取資料框中的所有列。請分享您的建議。TIA
uj5u.com熱心網友回復:
資料
df= spark.createDataFrame([
(1,'2022-03-01'),
(2,'2022-04-17'),
(3,None)
],('id','date'))
df.show()
--- ----------
| id| date|
--- ----------
| 1|2022-03-01|
| 2|2022-04-17|
| 3| null|
--- ----------
你有兩個選擇
選項 1 選擇不使用 N 和 Y 投影新列
df.createOrReplaceTempView("input1");
String_look = "select id, date from input1 where length(date)>0";
Dataset_op = spark.sql(String_look).show()
--- ----------
| id| date|
--- ----------
| 1|2022-03-01|
| 2|2022-04-17|
--- ----------
或者將 Y 和 N 投影到一個新列中。請記住 where 子句在列投影之前應用。所以你不能在 where 子句中使用新創建的列
String_look = "select id, date, case when length(date)>0 then 'Y' else 'N' end as status from input1 where length(date)>0";
--- ---------- ------
| id| date|status|
--- ---------- ------
| 1|2022-03-01| Y|
| 2|2022-04-17| Y|
--- ---------- ------
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/457310.html
標籤:熊猫 数据框 阿帕奇火花 pyspark apache-spark-sql
下一篇:基于欄位值的連接:SQL
