pyspark读写elasticsearch依赖elasticsearch-hadoop包,需要首先在这里下载,版本号可以通过自行修改url解决。

"""
write data to elastic search
https://starsift.com/2018/01/18/integrating-pyspark-and-elasticsearch/
"""
from __future__ import print_function
import os
import json from pyspark import SparkContext
from pyspark import SparkConf os.environ["PYSPARK_PYTHON"] = "/usr/bin/python3"
os.environ["PYSPARK_DRIVER_PYTHON"] = "/usr/bin/python3"
# pay attention here, jars could be added at here
os.environ['PYSPARK_SUBMIT_ARGS'] = \
'--jars /home/buxizhizhoum/2-Learning/pyspark_tutorial/jars/elasticsearch-hadoop-6.4.2/dist/elasticsearch-spark-20_2.11-6.4.2.jar ' \
'--packages org.apache.spark:spark-streaming-kafka-0-8_2.11:2.3.1 ' \
'pyspark-shell' conf = SparkConf().setAppName("write_es").setMaster("local[2]")
sc = SparkContext(conf=conf) # config refer: https://www.elastic.co/guide/en/elasticsearch/hadoop/current/configuration.html
es_write_conf = {
# specify the node that we are sending data to (this should be the master)
"es.nodes": 'localhost',
# specify the port in case it is not the default port
"es.port": '',
# specify a resource in the form 'index/doc-type'
"es.resource": 'testindex/testdoc',
# is the input JSON?
"es.input.json": "yes",
# is there a field in the mapping that should be used to specify the ES document ID
"es.mapping.id": "doc_id"
} if __name__ == "__main__":
data = [
{'': '', 'doc_id': 1},
{'': '', 'doc_id': 2},
{'': '', 'doc_id': 3},
{'': '', 'doc_id': 4},
{'': '', 'doc_id': 5},
{'': '', 'doc_id': 6},
{'': '', 'doc_id': 7},
{'': '', 'doc_id': 8},
{'': '', 'doc_id': 9},
{'': '', 'doc_id': 10}
]
rdd = sc.parallelize(data)
rdd = rdd.map(lambda x: (x["doc_id"], json.dumps(x)))
rdd.saveAsNewAPIHadoopFile(
path='-',
outputFormatClass="org.elasticsearch.hadoop.mr.EsOutputFormat",
keyClass="org.apache.hadoop.io.NullWritable",
valueClass="org.elasticsearch.hadoop.mr.LinkedMapWritable",
# critically, we must specify our `es_write_conf`
conf=es_write_conf)

更多代码见:https://github.com/buxizhizhoum/pyspark_tutorial/tree/master/spark_elasticsearch

refer: https://starsift.com/2018/01/18/integrating-pyspark-and-elasticsearch/

spark 集成elasticsearch的更多相关文章

  1. Spark:利用Eclipse构建Spark集成开发环境

    前一篇文章“Apache Spark学习:将Spark部署到Hadoop 2.2.0上”介绍了如何使用Maven编译生成可直接运行在Hadoop 2.2.0上的Spark jar包,而本文则在此基础上 ...

  2. 使用spark访问elasticsearch的数据

    使用spark访问elasticsearch的数据,前提是spark能访问hive,hive能访问es http://blog.csdn.net/ggz631047367/article/detail ...

  3. spark集成hive遭遇mysql check失败的问题

    问题: spark集成hive,启动spark-shell或者spark-sql的时候,报错: INFO MetaStoreDirectSql: MySQL check failed, assumin ...

  4. springboot集成elasticsearch

    在基础阶段学习ES一般是首先是 安装ES后借助 Kibana 来进行CURD 了解ES的使用: 在进阶阶段可以需要学习ES的底层原理,如何通过Version来实现乐观锁保证ES不出问题等核心原理: 第 ...

  5. Spark 整合ElasticSearch

    Spark 整合ElasticSearch 因为做资料搜索用到了ElasticSearch,最近又了解一下 Spark ML,先来演示一个Spark 读取/写入 ElasticSearch 简单示例. ...

  6. 使用Logstash同步数据至Elasticsearch,Spring Boot中集成Elasticsearch实现搜索

    安装logstash.同步数据至ElasticSearch 为什么使用logstash来同步,CSDN上有一篇文章简要的分析了以下几种同步工具的优缺点:https://blog.csdn.net/la ...

  7. Spark搭档Elasticsearch

    Spark与elasticsearch结合使用是一种常用的场景,小编在这里整理了一些Spark与ES结合使用的方法.一. write data to elasticsearch利用elasticsea ...

  8. SpringBoot 集成 Elasticsearch

    前面在 ubuntu 完成安装 elasticsearch,现在我们SpringBoot将集成elasticsearch. 1.创建SpringBoot项目 我们这边直接引入NoSql中Spring ...

  9. 数据湖应用解析:Spark on Elasticsearch一致性问题

    摘要:脏数据对数据计算的正确性带来了很严重的影响.因此,我们需要探索一种方法,能够实现Spark写入Elasticsearch数据的可靠性与正确性. 概述 Spark与Elasticsearch(es ...

随机推荐

  1. [转][Java]尝试解决Java多行字符串的编辑问题

    转自:https://blog.csdn.net/jiuwuerliu/article/details/51207045 参考了:https://www.v2ex.com/amp/t/445522 除 ...

  2. 刚刚研究了下ipv6,尝试配置内网VPS的IPv6地址

    刚刚研究了下ipv6,尝试配置内网VPS的IPv6地址是3台设备,分别是客户机Windows系统.核心交换机.PPPoE拨号的路由器 第一步:在PPPoE拨号的路由器上面查看ppp0拨号的地址 ifc ...

  3. Oracle 官方文档地址

    官方文档地址: https://docs.oracle.com/cd/E11882_01/index.htm

  4. golang 常量的用法

    1.Golang常量的用法 //常量的用法 var num int num =9 //1.常量声明的时候必须赋值 const tax int = 0 //2.常量值是无法修改的 //tax = 10 ...

  5. Hibernate SQL

    SQL查询: 5.2之后的版本: NativeQuery<Order> sqlQuery = session.createNativeQuery("select * from t ...

  6. 在django中,执行原始sql语句

    extra()方法 结果集修改器,一种提供额外查询参数的机制 使用extra: 1:Book.objects.filter(publisher__name='广东人员出版社').extra(where ...

  7. CF1017G The Tree

    /* 这是什么神仙题目QAQ 首先考虑在序列上的问题 先不考虑修改成白色, 一个白点能r被染成黑色 意味着能够找到一个l使得在l-r中的操作1次数大于等于 r - l + 1 我们把初始值覆盖成-1就 ...

  8. python学习过程中的踩坑记录<若干,随时更新>

    问题1:python中print的连串输出与java不一样? 输入print(code +"+++"); --在代码中写入,界面未报错,但是告诉你不行 会报错,如图: 解决办法: ...

  9. Spring和SpringBoot比较,解惑区别

    1.概述: 对于Spring和SpringBoot到底有什么区别,我听到了很多答案,刚开始迈入学习SpringBoot的我当时也是一头雾水,随着经验的积累.我慢慢理解了这两个框架到底有什么区别,我相信 ...

  10. Call requires permission which may be rejected by user: code should explicitly check to see if permi

    Call requires permission which may be rejected by user: code should explicitly check to see if permi ...