Python 字典键值到 Pyspark 中的数据框 where 子句
Python dictionary key value into dataframe where clause in Pyspark
如何将 Python 字典键值传递到 Pyspark 中的数据框 where 子句 ...
Python字典如下...
column_dict= { 'email': 'customer_email_addr' ,
'addr_bill': 'crq_st_addr' ,
'addr_ship': 'ship_to_addr' ,
'zip_bill': 'crq_zip_cd' ,
'zip_ship': 'ship_to_zip' ,
'phone_bill': 'crq_cm_phone' ,
'phone_ship' : 'ship_to_phone'}
我有一个包含大约 30 亿条记录的 spark 数据框。数据框如下...
source_sql= ("select cust_id, customer_email_addr, crq_st_addr, ship_to_addr,
crq_zip_cd,ship_to_zip,crq_cm_phone,ship_to_phone from odl.cust_master where
trans_dt >= '{}' and trans_dt <= '{}' ").format('2017-11-01','2018-10-31')
cust_id_m = hiveCtx.sql(source_sql)
cust_id.cache()
我打算在上述字典键的循环中找出不同的有效客户的电子邮件、地址、邮编和 Phone 和 运行。为此,当我针对一个键值测试 spark shell 时,如下所示 ...
>>> cust_id_risk_m=cust_id_m.selectExpr("cust_id").where(
("cust_id_m.'{}'").format(column_dict['email']) != '' ).distinct()
我遇到错误...需要专家帮助解决这个问题。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/mapr/spark/spark-2.1.0/python/pyspark/sql/dataframe.py", line 1026, in filter
raise TypeError("condition should be string or Column")
TypeError: condition should be string or Column
你能试试在你的字典上使用 get 方法吗?
我已经使用以下数据框对此进行了测试:
df =spark.sql("select emp_id, emp_name, emp_city,emp_salary from udb.emp_table where emp_joining_date >= '{}' ".format(2018-12-05))
>>> df.show(truncate=False)
+------+----------------------+--------+----------+
|emp_id|emp_name |emp_city|emp_salary|
+------+----------------------+--------+----------+
|1 |VIKRANT SINGH RANA |NOIDA |10000 |
|3 |GOVIND NIMBHAL |DWARKA |92000 |
|2 |RAGHVENDRA KUMAR GUPTA|GURGAON |50000 |
+------+----------------------+--------+----------+
thedict={"CITY":"NOIDA"}
>>> newdf = df.selectExpr("emp_id").where("emp_city ='{}'".format(thedict.get('CITY'))).distinct()
>>> newdf.show();
+------+
|emp_id|
+------+
| 1|
+------+
或者您可以分享您的数据框示例数据?
如何将 Python 字典键值传递到 Pyspark 中的数据框 where 子句 ...
Python字典如下...
column_dict= { 'email': 'customer_email_addr' ,
'addr_bill': 'crq_st_addr' ,
'addr_ship': 'ship_to_addr' ,
'zip_bill': 'crq_zip_cd' ,
'zip_ship': 'ship_to_zip' ,
'phone_bill': 'crq_cm_phone' ,
'phone_ship' : 'ship_to_phone'}
我有一个包含大约 30 亿条记录的 spark 数据框。数据框如下...
source_sql= ("select cust_id, customer_email_addr, crq_st_addr, ship_to_addr,
crq_zip_cd,ship_to_zip,crq_cm_phone,ship_to_phone from odl.cust_master where
trans_dt >= '{}' and trans_dt <= '{}' ").format('2017-11-01','2018-10-31')
cust_id_m = hiveCtx.sql(source_sql)
cust_id.cache()
我打算在上述字典键的循环中找出不同的有效客户的电子邮件、地址、邮编和 Phone 和 运行。为此,当我针对一个键值测试 spark shell 时,如下所示 ...
>>> cust_id_risk_m=cust_id_m.selectExpr("cust_id").where(
("cust_id_m.'{}'").format(column_dict['email']) != '' ).distinct()
我遇到错误...需要专家帮助解决这个问题。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/mapr/spark/spark-2.1.0/python/pyspark/sql/dataframe.py", line 1026, in filter
raise TypeError("condition should be string or Column")
TypeError: condition should be string or Column
你能试试在你的字典上使用 get 方法吗? 我已经使用以下数据框对此进行了测试:
df =spark.sql("select emp_id, emp_name, emp_city,emp_salary from udb.emp_table where emp_joining_date >= '{}' ".format(2018-12-05))
>>> df.show(truncate=False)
+------+----------------------+--------+----------+
|emp_id|emp_name |emp_city|emp_salary|
+------+----------------------+--------+----------+
|1 |VIKRANT SINGH RANA |NOIDA |10000 |
|3 |GOVIND NIMBHAL |DWARKA |92000 |
|2 |RAGHVENDRA KUMAR GUPTA|GURGAON |50000 |
+------+----------------------+--------+----------+
thedict={"CITY":"NOIDA"}
>>> newdf = df.selectExpr("emp_id").where("emp_city ='{}'".format(thedict.get('CITY'))).distinct()
>>> newdf.show();
+------+
|emp_id|
+------+
| 1|
+------+
或者您可以分享您的数据框示例数据?