我有一个看起来像这样的烟火数据框:
sdf1 = sc.parallelize([["toto", "tata", ["table", "column"], "SELECT {1} FROM {0}"], "titi", "tutu", ["table", "column"], "SELECT {1} FROM {0}"]]).toDF(["table", "column", "parameters", "statement"])
+-----+------+---------------+-------------------+
|table|column| parameters| statement|
+-----+------+---------------+-------------------+
| toto| tata|[table, column]|SELECT {1} FROM {0}|
| titi| tutu|[table, column]|SELECT {1} FROM {0}|
+-----+------+---------------+-------------------+
我尝试将数组“参数”元素Map到列,最后用列中的值来格式化“语句”。
这就是我在处理转型后所期待的:
sdf2 = sc.parallelize([["toto", "tata", ["table", "column"], "SELECT {1} FROM {0}", "SELECT tata FROM toto"],["titi", "tutu", ["table", "column"], "SELECT {1} FROM {0}", "SELECT tutu FROM titi"]]).toDF(["table", "column", "parameters", "statement", "result"])
+-----+------+---------------+-------------------+---------------------+
|table|column| parameters| statement| result|
+-----+------+---------------+-------------------+---------------------+
| toto| tata|[table, column]|SELECT {1} FROM {0}|SELECT tata FROM toto|
| titi| tutu|[table, column]|SELECT {1} FROM {0}|SELECT tutu FROM titi|
+-----+------+---------------+-------------------+---------------------+
1条答案
按热度按时间3pmvbmvn1#
一种使用RDD的方法。
函数
addParamsToQuery
使用列值创建参数值列表,并使用.format()
插入到语句中。