当前位置: 首页 > wzjs >正文

自己建网站开网店建设考试网站首页

自己建网站开网店,建设考试网站首页,如何申请免费域名做网站,移动应用开发网站使用Spark处理数据文件 检查数据 检查$DATA_EXERCISE/activations里的数据&#xff0c;每个XML文件包含了客户在指定月份活跃的设备数据。 拷贝数据到HDFS的/dw目录 样本数据示例&#xff1a; <activations><activation timestamp"1225499258" type&qu…

使用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 Shellimport scala.xml._// Given a string containing XML, parse the string, and 
// return an iterator of activation XML records (Nodes) contained in the stringdef 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

http://www.dtcms.com/wzjs/835351.html

相关文章:

  • 网站设计示例怎么建php网站
  • 网站搬迁苏州网页制作与设计
  • 网站管理规划方案沈阳头条新闻
  • 如何说服企业做网站网页设计毕业设计开题报告
  • 洛阳网站建站云南网站做的好的公司哪家好
  • 平台网站建设协议建立网站的基本流程有哪些步骤
  • 广告发布包括哪些关于seo关键词选择有哪些方法
  • 网站 内容 营销沧州网站推广优化
  • 有域名怎么做公司网站wordpress 4.1分页
  • 什么是自适应网站优化步骤
  • 网站腾讯备案吗国家信息公示系统官网
  • 旅游论坛网站建设网站建设的目的与意义是什么意思
  • 个人域名备案做企业网站最新国际军事新闻
  • 网站建设的客户在哪里定制系统软件开发
  • 个人网站建设方案模板建站工具哪个好用
  • 仿做网站要多少钱seo优化包括哪些内容
  • 成都flash互动网站开发免费链接转换短网址
  • 正规的企业网站建设公司深圳西乡建网站
  • 做销售的 都有什么网站一个人可以建设几个网站
  • 网站运营团队管理广州广告公司
  • 书写网站建设策划书wordpress 内页打不开
  • 南京地区网站开发房产网名字叫啥好听
  • 学校网站怎么做的好处wordpress有中文版没
  • 深圳建站网站网站公司天津网站开发价格
  • 有没有做家具特卖的网站网站网站建设网站
  • 佛山做外贸网站如何怎么开店
  • 上海创意型网站建设姑苏区住房和建设局网站
  • 网站浮动窗口怎么设置国际网站怎么做
  • 爱墙 网站怎么做网站用模板为什么不利于seo推广
  • 网站开发用什么软件用手机做网站的流程