如何使用PySpark将一组数据帧记录发送到API

如何使用PySpark将一组数据帧记录发送到API,pyspark,batch-processing,Pyspark,Batch Processing,如何向API批量发送数据帧元组 headers = { 'Content-Type': 'application/json', 'Accept': '*/*' } data = {"some_key": "some_value", "another_key": "another_value" } r = requests.post('https://api.somewhere/batch', params={}, headers=headers, json=data) 如果J

如何向API批量发送数据帧元组

headers = {
    'Content-Type': 'application/json',
    'Accept': '*/*'
}

data = {"some_key": "some_value", "another_key": "another_value" }
r = requests.post('https://api.somewhere/batch', params={}, headers=headers, json=data)

如果JSON负载来自PySpark中的数据帧,我如何利用Spark批处理当前的单线程方法?

您可以将数据帧转换为JSON:

def batch_json(行):
#您想对每一行/分区处理的任何内容
r=请求。post('https://api.somewhere/batch“,params={},headers=headers,json=row)
打印(r.状态\ U代码)
df.toJSON().foreach(批处理json)
#或
#“batch_json”不能按原样使用,您必须根据需要进行更改
df.toJSON().foreachPartition(批处理json)
快速测试代码:

def batch(row):
    print(row)

df.toJSON().foreach(batch)
可能重复的