2. 统计每个月份的注册数量,把结果写入hdfs的/学号目录下。
时间: 2024-03-13 15:42:35 浏览: 15
下面是实现该功能的代码:
```python
from pyspark import SparkContext, SparkConf
# 创建SparkContext
conf = SparkConf().setAppName("BikeSharing").setMaster("local")
sc = SparkContext(conf=conf)
# 读取CSV文件,创建RDD
lines = sc.textFile("hdfs://localhost:9000/exam/学号/train.csv")
header = lines.first() # 获取头部信息
data = lines.filter(lambda line: line != header) # 过滤掉头部信息
# 提取月份和注册数量,将它们作为键值对
month_registered = data.map(lambda line: (line.split(",")[1][5:7], int(line.split(",")[9])))
# 计算每个月份的注册数量
month_registered = month_registered.reduceByKey(lambda x, y: x + y)
# 将结果写入HDFS的/学号目录下
month_registered.saveAsTextFile("hdfs://localhost:9000/学号/month_registered")
# 关闭SparkContext
sc.stop()
```
其中,`saveAsTextFile()`方法将RDD中的数据写入到指定的HDFS目录中。在本例中,我们将结果写入到了`/学号/month_registered`目录中。