spark读取本地文件的方法有哪些

558
2024/3/26 14:13:58
栏目: 大数据
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Spark中,可以使用以下几种方式来读取本地文件:

  1. 使用sc.textFile()方法读取文本文件:
from pyspark import SparkContext

sc = SparkContext("local", "FileReader")

file_path = "file:///path/to/file.txt"
lines = sc.textFile(file_path)
  1. 使用spark.read.csv()方法读取CSV文件:
from pyspark.sql import SparkSession

spark = SparkSession.builder.master("local").appName("FileReader").getOrCreate()

file_path = "file:///path/to/file.csv"
df = spark.read.csv(file_path, header=True, inferSchema=True)
  1. 使用spark.read.json()方法读取JSON文件:
from pyspark.sql import SparkSession

spark = SparkSession.builder.master("local").appName("FileReader").getOrCreate()

file_path = "file:///path/to/file.json"
df = spark.read.json(file_path)

这些是一些常见的方法,可以根据实际需求选择合适的方法来读取本地文件。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: spark怎么处理大量日志文件