7.7日 实验03-Spark批处理开发(2)

使用Spark处理数据文件

检查数据

检查$DATA_EXERCISE/activations里的数据,每个XML文件包含了客户在指定月份活跃的设备数据。

拷贝数据到HDFS的/dw目录

样本数据示例:

复制代码
<activations>
  <activation timestamp="1225499258" type="phone">
    <account-number>316</account-number>
    <device-id>d61b6971-33e1-42f0-bb15-aa2ae3cd8680</device-id>
    <phone-number>5108307062</phone-number>
    <model>iFruit 1</model>
  </activation>
  ...
</activations>

处理文件

读取XML文件并抽取账户号和设备型号,把结果保存到/dw/account-models,格式为account_number:model

输出示例:

复制代码
1234:iFruit 1
987:Sorrento F00L
4566:iFruit 1
...

提供了解析XML的函数如下:

复制代码
// Stub code to copy into Spark Shell

import scala.xml._

// Given a string containing XML, parse the string, and 
// return an iterator of activation XML records (Nodes) contained in the string

def getActivations(xmlstring: String): Iterator[Node] = {
    val nodes = XML.loadString(xmlstring) \\ "activation"
    nodes.toIterator
}

// Given an activation record (XML Node), return the model name
def getModel(activation: Node): String = {
   (activation \ "model").text
}

// Given an activation record (XML Node), return the account number
def getAccount(activation: Node): String = {
   (activation \ "account-number").text
}

上传数据

复制代码
# 1. 检查并创建HDFS目录
hdfs dfs -mkdir -p /dw

# 2. 将本地数据上传到HDFS(替换$DATA_EXERCISE为实际路径)
hdfs dfs -put $DATA_EXERCISE/activations /dw/

# 3. 检查文件是否上传成功
hdfs dfs -ls /dw/activations
复制代码
定义题目提供的解析函数
复制代码
def getActivations(xmlstring: String): Iterator[Node] = {
    (XML.loadString(xmlstring) \\ "activation").toIterator
}

def getModel(activation: Node): String = (activation \ "model").text
def getAccount(activation: Node): String = (activation \ "account-number").text
复制代码
读取数据(像处理日志一样)
复制代码
val xmlRDD = sc.wholeTextFiles("/dw/activations/*.xml")
复制代码
测试解析(查看第一条记录)
复制代码
val firstRecord = getActivations(xmlRDD.first()._2).next()
println(s"测试解析结果: ${getAccount(firstRecord)}:${getModel(firstRecord)}")
复制代码
处理全部数据
复制代码
val resultRDD = xmlRDD.flatMap { case (_, xml) => 
  getActivations(xml).map(act => s"${getAccount(act)}:${getModel(act)}")
}
复制代码
查看结果样例(10条)
复制代码
resultRDD.take(10).foreach(println)
复制代码
保存结果(先清理旧数据)
复制代码
import org.apache.hadoop.fs._
val outputPath = "/dw/account-models"
val fs = FileSystem.get(sc.hadoopConfiguration)
if (fs.exists(new Path(outputPath))) fs.delete(new Path(outputPath), true)

resultRDD.saveAsTextFile(outputPath)
println(s"结果已保存到 hdfs://$outputPath")

验证结果(在Linux终端执行)

复制代码
# 查看输出结果
hdfs dfs -cat /dw/account-models/part-* | head -n 10

# 如果需要合并结果到单个文件
hdfs dfs -getmerge /dw/account-models ./account_models.txt
head account_models.txt
相关推荐
AI进化营-智能译站13 分钟前
ROS2 C++开发系列12-用多态与虚函数构建可扩展的ROS2机器人行为模块
开发语言·c++·ai·机器人
iCxhust16 分钟前
微机原理实践教程(C语言篇)---A002流水灯
c语言·开发语言·单片机·嵌入式硬件·51单片机·课程设计·微机原理
是上好佳佳佳呀32 分钟前
【前端(十一)】JavaScript 语法基础笔记(多语言对比)
前端·javascript·笔记
莎士比亚的文学花园38 分钟前
Linux驱动开发(3)——设备树
开发语言·javascript·ecmascript
图码1 小时前
如何用多种方法判断字符串是否为回文?
开发语言·数据结构·c++·算法·阿里云·线性回归·数字雕刻
U盘失踪了1 小时前
python curl转python脚本
开发语言·chrome·python
charlie1145141911 小时前
Linux 字符设备驱动:cdev、设备号与设备模型
linux·开发语言·驱动开发·c
handler011 小时前
Linux 内核剖析:进程优先级、上下文切换与 O(1) 调度算法
linux·运维·c语言·开发语言·c++·笔记·算法
FQNmxDG4S1 小时前
Java泛型编程:类型擦除与泛型方法的应用场景
java·开发语言·python
我星期八休息1 小时前
IT疑难杂症诊疗室:AI时代工程师Superpowers进化论
linux·开发语言·数据结构·人工智能·python·散列表