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. android手机截屏、录屏

    1. 手动截屏,通过其他第三方软件发送截图,或者从手机取出截图 2. 使用命令截图,将截图保存到手机,再拉取到电脑 #!/bin/sh #运行 sh screenshot name picName=$ ...

  2. mybatisz中一个可以替代between..and 的技巧

    用mybatis进行时间段筛选时,如果,查询本日,本月的信息量,我们可以使用like concat()函数来替换between..and <select id="queryMyStaf ...

  3. Postman模拟ajax请求 并模拟登陆

    2.设置header头 <span style="font-size:16px;">设置代理.cookie.X-Requested-With   注意 :X-Reque ...

  4. vue 数据绑定 绑定属性 循环渲染数据

    <template> <!-- vue的模板里面 所有的内容要被一个根节点包含起来 --> <div id="app"> <h2>{ ...

  5. Change default network name (ens33) to old “eth0” on Ubuntu 18.04 / Ubuntu 16.04

    Change default network name (ens33) to old “eth0” on Ubuntu 18.04 / Ubuntu 16.04 By Raj Last updated ...

  6. spark streaming的有状态例子

    import org.apache.spark._ import org.apache.spark.streaming._ /** * Created by code-pc on 16/3/14. * ...

  7. JDBC连接数据库,结合DbUtil数据库连接工具类的使用

    (以Mysql数据库为例) 第一步:在项目里配置数据库驱动 Build Path->configure  Build Path ->Add Exteral JARs   将JDBC驱动包导 ...

  8. virtual box + win7 + usb + share folder

    1.enable virtaulization on BIOS 2.new machine setup, memory, harddisk size 3. 4.install extension pa ...

  9. php引用变量

    引用变量:在php中引用意味着用不同的名字访问同一个变量内容 定义方式:& 总结:$b=&$a其中$b是取到了$a的地址,随着$a的地址变化,不会重新开辟空间可以根据他们的内存占用情况 ...

  10. Ubuntu下RabbitMQ安装

    由于RabbitMQ需要erlang语言的支持,在安装RabbitMQ之前需要安装erlang,执行命令: sudo apt-get install erlang-nox 安装RabbitMQ命令: ...