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. openstack添加热添加硬盘并识别

    假定在虚拟机当中添加了磁盘,但是虚拟机没有识别出来:如何识别出来 可以使用命令 echo '- - -' >/sys/class/scsi_host/host0/scan 使用后就可以识别出来了 ...

  2. ros6.44版本增加了测试出两台ROS之间的速度极限是多少

    原文: https://wiki.mikrotik.com/wiki/Manual:Tools/Speed_Test [admin@MikroTik]] > /tool speed-test a ...

  3. 经典算法冒泡排序java版

    写个冒泡排序吧 冒泡排序(Bubble Sort)是一种典型的交换排序算法,通过交换数据元素的位置进行排序. public class BubbleSort{ public int[] bubbleS ...

  4. 利用队列Queue实现一个多并发“线程池”效果的Socket程序

    本例通过利用类Queue建立了一个存放着Thread对象的“容器对象”,当Client端申请与Server端通信时,在Server端的“链接循环”中每次拿出一个Thread对象去创建“线程链接”,从而 ...

  5. python 基本数据类型常用方法总结

    [引言] python中基本数据类型的有很多常用方法,熟悉这些方法有助于不仅提升了编码效率,而且能写出高质量代码,本文做总结 int .bit_length:返回二进制长度 str 切片索引超出不会报 ...

  6. OpenCV-Python基本功能

    一.图像读取/保存 import cv2 img = cv2.imread("name.png") cv2.imwrite('save.jpg', img) #显示图像 cv2.i ...

  7. Devlopment Env Setup install ubuntu16.04

    http://blog.csdn.net/ljheee/article/details/52966048 1.add chinese language support settings -> i ...

  8. 电商系统架构总结2(Redis)

    二  Redis缓存 考虑到将来服务器的升级扩展,使用redis代替.net内置缓存是比较理想的选择.redis是非常成熟好用的缓存系统,安装配置非常简单,直接上官网下载安装包 安装启动就行了. 1 ...

  9. 03 Linux的目录结构与常见文件管理

    Linux目录结构 根文件系统rootfs用来实现整个文件的管理: 而Linux下的所有文件都需要通过根文件系统才能访问: FHS:Filesystem Hierarchy Standard文件层次化 ...

  10. 使用Nginx+Lua实现自定义WAF

    使用Nginx+Lua实现自定义WAF 版权声明:全部抄自赵班长的GitHub上waf项目 功能列表: 支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝. 支持URL白名单,将不需要过滤的URL ...