hive 如何在pyspark中读取配置单元表沿着元数据?

nue99wik  于 2023-10-18  发布在  Hive
关注(0)|答案(1)|浏览(178)

我无法使用pyspark读取配置单元表及其元数据沿着
我认为我准确地创建了Hive表
设置:

  1. from pyspark.sql import SparkSession
  2. from pyspark.sql import functions as F
  3. spark = SparkSession.builder.enableHiveSupport().getOrCreate()
  4. data1 = [(1,2,3),(3,4,5),(5,6,7)]
  5. df1=spark.createDataFrame(data1,schema = 'a int,b int,c int')
  6. parquet_path = './bucket_test_parquet1'

现在,用DESCRIBE检查表

  1. df1.write.bucketBy(5,"a").format("parquet").saveAsTable('df',path=parquet_path,mode='overwrite')
  2. spark.sql("DESCRIBE EXTENDED df").show(100)
  3. output:
  4. +--------------------+--------------------+-------+
  5. | col_name| data_type|comment|
  6. +--------------------+--------------------+-------+
  7. | a| int| null|
  8. | b| int| null|
  9. | c| int| null|
  10. | | | |
  11. |# Detailed Table ...| | |
  12. | Database| default| |
  13. | Table| df| |
  14. | Owner| nitin| |
  15. | Created Time|Tue Feb 01 09:05:...| |
  16. | Last Access| UNKNOWN| |
  17. | Created By| Spark 3.2.0| |
  18. | Type| EXTERNAL| |
  19. | Provider| parquet| |
  20. | Num Buckets| 5| |
  21. | Bucket Columns| [`a`]| |
  22. | Sort Columns| []| |
  23. | Location|file:/home/nitin/...| |
  24. | Serde Library|org.apache.hadoop...| |
  25. | InputFormat|org.apache.hadoop...| |
  26. | OutputFormat|org.apache.hadoop...| |
  27. +--------------------+--------------------+-------+
  1. read_parquet1 = spark.read.format("parquet").load(parquet_path,header=True)
  2. read_parquet1.createOrReplaceTempView("rp1")
  3. read_parquet1 = spark.table("rp1")
  4. spark.sql("DESCRIBE EXTENDED rp1").show(100)
  5. output:
  6. |col_name|data_type|comment|
  7. +--------+---------+-------+
  8. | a| int| null|
  9. | b| int| null|
  10. | c| int| null|
  11. +--------+---------+-------+

正如您所看到的,当我从磁盘中阅读表时,元数据没有被读取。你能帮我读表,以便我有元数据沿着?

relj7zay

relj7zay1#

如果你想要一个数据路径的表模式,你也可以这样做:
read_parquet1 = spark.read.format(“parquet”).load(parquet_path,header=True)read_parquet1.PrintSchema()--这将给予您想要的结果。
代码的问题在于,当您注意到的第一个代码时,您已经将数据写入了一个位置并要求提供其模式,而在第二种情况下,您正在从一个位置阅读,创建一个临时表并要求临时表提供其定义。理想情况下,您应该像我的代码中那样询问数据路径的Schema。

相关问题