【发布时间】:2016-12-24 20:14:40
【问题描述】:
我正在尝试从 rdd 写入 elasticsearch(pyspark,python 3.5)。 我能够正确地编写 json 的主体,但 elasticsearch 不是采用我的 _id,而是创建它自己的。
我的代码:
class Article:
def __init__(self, title, text, text2):
self.id_ = title
self.text = text
self.text2 = text2
if __name__ == '__main__':
pt=_sc.parallelize([Article("rt", "ted", "ted2"),Article("rt2", "ted2", "ted22")])
save=pt.map(lambda item:
(item.id_,
{
'text' : item.text,
'text2' : item.text2
}
))
es_write_conf = {
"es.nodes": "localhost",
"es.port": "9200",
"es.resource": 'db/table1'
}
save.saveAsNewAPIHadoopFile(
path='-',
outputFormatClass="org.elasticsearch.hadoop.mr.EsOutputFormat",
keyClass="org.apache.hadoop.io.NullWritable",
valueClass="org.elasticsearch.hadoop.mr.LinkedMapWritable",
conf=es_write_conf)
程序跟踪: link to the image
【问题讨论】:
标签: python-3.x hadoop elasticsearch pyspark