Spark JDBC读取API。动态确定日期类型列的分区数量

0 人关注

我正在尝试使用PySpark从RDS的MySQL实例中读取一个表。这是一个巨大的表,因此我想通过利用分区的概念来并行化读取操作。该表没有一个数字列来查找分区的数量。相反,它有一个时间戳列(即数据时间类型)。

我通过检索时间戳列的最小和最大值找到了下限和上限。然而,我不确定是否有一个标准的公式可以动态地找出分区的数量。以下是我目前正在做的事情(硬编码参数numPartititons的值)。

select_sql = "SELECT {} FROM {}".format(columns, table)
partition_info = {'partition_column': 'col1', 
                  'lower_bound': '<result of min(col1)>', 
                  'upper_bound': '<result of max(col1)>', 
                  'num_partitions': '10'}
read_df = spark.read.format("jdbc") \
        .option("driver", driver) \
        .option("url", url) \
        .option("dbtable", select_sql) \
        .option("user", user) \
        .option("password", password) \
        .option("useSSL", False) \
        .option("partitionColumn", partition_info['partition_column']) \
        .option("lowerBound", partition_info['lower_bound'])) \
        .option("upperBound", partition_info['upper_bound'])) \
        .option("numPartitions", partition_info['num_partitions']) \
        .load()