apache-spark - Spark 数据框列命名约定/限制

标签 apache-spark hive pyspark naming-conventions amazon-athena

我的 (Py)Spark 列名称的默认命名(从收到的 .csv 文件导入)现在多次遇到问题。似乎与 Spark 混淆的事情是 MixedCase 和诸如 .或 - 在列名称中。所以我决定找出实际保存的列名,并发现以下内容:

This website似乎只建议使用小写名称:

Hive stores the table, field names in lowercase in Hive Metastore. Spark preserves the case of the field name in Dataframe, Parquet Files. When a table is created/accessed using Spark SQL, Case Sensitivity is preserved by Spark storing the details in Table Properties (in hive metastore). This results in a weird behavior when parquet records are accessed thru Spark SQL using Hive Metastore.



Amazon Athena似乎证实了这一点,并补充说“_”是唯一的保存特殊字符:

... but Spark requires lowercase table and column names.

Athena table, view, database, and column names cannot contain special characters, other than underscore (_).



我从中得到的是,如果可能的话,我应该尝试只使用小写的列名,并使用 _ 作为单词之间的分隔符,以确保与可能出现在我的 Spark 工作流程中的工具的最大交叉兼容性。 这样对吗?是否有理由更喜欢空格而不是下划线,还有什么需要考虑的吗?

我意识到在许多情况下,将所有列重命名为上述模式时我可能会过度使用它 - 但是,我宁愿避免在项目中间遇到与命名相关的麻烦,因为我发现这些错误有时难以调试。

最佳答案

将文件保存为 Parquet 格式时,不能使用空格和某些特定字符。我在从 CSV 读取和写入 Parquet 时遇到了类似的问题。以下代码为我解决了这个问题:

# Column headers: lower case + remove spaces and the following characters: ,;{}()=  
newColumns = []
problematic_chars = ',;{}()='
for column in df.columns:
    column = column.lower()
    column = column.replace(' ', '_')
    for c in problematic_chars:
        column = column.replace(c, '')
    newColumns.append(column)
df = df.toDF(*newColumns)

所以是的,如果你的目标是确保最大的交叉兼容性,你应该确保你的列名都是小写的,只有 _ 作为分隔符。

关于apache-spark - Spark 数据框列命名约定/限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53010507/

相关文章:

mysql - 如何在配置单元中将时间戳转换为 gmt 格式

apache-spark - 如何在 Yarn 上运行的 Spark 中动态增加事件任务

hadoop - Log4j 不写入 HDFS/Log4j.properties

python - 从元组列表创建 Spark rdd 并使用 groupByKey

Pyspark 数据帧计数花费的时间太长

apache-spark - 发送Spark流指标以打开tsdb

hadoop - Yarn 上的 Spark 作业 |性能调整和优化

hadoop - 如何(仅)按时间戳列的一部分对配置单元表进行分区?

linux - 找不到类 'org.apache.hadoop.hive.druid.DruidStorageHandler'

mysql - 使用 JDBC 连接器读取 Spark 中 MySQL 表的一部分