請告訴我如何從 hdfs 讀取檔案。我剛剛開始使用 Scala 和 Spark。我可以讀取檔案夾中的單獨檔案:
val parqDF = spark.read.parquet("hdfs://nn1home:8020/user/stg/ads/year=2020/month=1/day=1/16_data.0.parq")
但我想閱讀所有鑲木地板的整個檔案夾
還有一個更重要的問題,如何將列添加到我的資料框中,其中包含來自我的鑲木地板路徑的資料
感謝您的任何建議
uj5u.com熱心網友回復:
import org.apache.spark.sql.functions.lit
val inputPath = "<you path>"
val dataDF = spark.read.parquet(inputPath)
dataDF.printSchema()
// root
// |-- _c0: string (nullable = true)
// |-- _c1: string (nullable = true)
// |-- _c2: string (nullable = true)
// |-- _c3: string (nullable = true)
val resDF = dataDF.withColumn("new_col", lit(inputPath))
resDF.printSchema
// root
// |-- _c0: string (nullable = true)
// |-- _c1: string (nullable = true)
// |-- _c2: string (nullable = true)
// |-- _c3: string (nullable = true)
// |-- new_col: string (nullable = false)
resDF.schema
// res2: org.apache.spark.sql.types.StructType = StructType(
// StructField(_c0,StringType,true),
// StructField(_c1,StringType,true),
// StructField(_c2,StringType,true),
// StructField(_c3,StringType,true),
// StructField(new_col,StringType,false)
// )
// resDF.show(false) - show data dataframe
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/480088.html
