以下のようにsparkを使用してテーブルを読み取った後に取得したデータフレームがあります。
val orgDF = spark.read.format("jdbc").options("url", url).options("dbtable","select id, org_name, delete_state, soft_del, 0 as soft_del from schema.table as orgTable").options("user",username)options("password",pwd).load()
以下のようにデータフレームからの出力データを見ることができます:
-----------------------------------------
id | org_name | delete_state | soft_del
-----------------------------------------
1 | Net | delete | 0
2 | Vert | delete | 0
3 | Bio | insert | 0
4 | Card | delete | 0
7 | stock | update | 0
-----------------------------------------
中列の谷があれば「1」としてsoft_del:HDFS上のデータフレームを保存する前に、私はCOLの値を設定しようとしていますdelete_state
でdelete
以下のように、最終的なデータフレームをして行います。
-----------------------------------------
id | org_name | delete_state | soft_del
-----------------------------------------
1 | Net | delete | 1
2 | Vert | delete | 1
3 | Bio | insert | 0
4 | Card | delete | 1
7 | Stock | update | 0
-----------------------------------------
私はそれを以下のようにする方法があることを知っています:
orgDF.createOrReplaceTempView("orgData")
spark.sql("update orgData set soft_del = 1 where delete_state = 'delete'")
また、データフレーム関数の使用方法を理解しようとしていますが、正しい資料を見つけることができません。データフレーム関数を使用してそれを行う方法を誰かに教えてもらえますか?
あなたはこのようなことを試すことができます
orgDF.withColumn("soft_del", when($"delete_state" === "delete", 1).otherwise(0))
when
必要に応じて、次のように複数のをチェーンすることもできます。
orgDF.withColumn("soft_del",
when($"delete_state" === "delete", 1)
.when($"delete_state" === "update", 2)
.otherwise(0)
)
参照
when
機能。この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加