2.17
今天学习了如何将Spark与Elasticsearch集成,用于处理和分析Elasticsearch中的数据。
代码示例:
python
复制
from pyspark.sql import SparkSession
# 创建SparkSession
spark = SparkSession.builder \
.appName("ElasticsearchIntegration") \
.config("spark.es.nodes", "localhost") \
.config("spark.es.port", "9200") \
.getOrCreate()
# 从Elasticsearch读取数据
df = spark.read.format("org.elasticsearch.spark.sql") \
.option("es.resource", "my_index/my_type") \
.load()
# 显示数据
df.show()
# 将数据写入Elasticsearch
df.write.format("org.elasticsearch.spark.sql") \
.option("es.resource", "my_index_output/my_type") \
.save()
spark.stop()