当前位置: 首页 > 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/790567.html

相关文章:

  • 手机小说网站源码烟台商城网站制作
  • 大型门户网站建设的意义做网站密云
  • 网站公司成本重庆网站网络推广推广
  • 湖南做网站 e磐石网络简述网站内容管理流程
  • 物流网站建设评析别墅设计图纸
  • 建立网站最好的模板网站建设工作室怎么开
  • 涟源市住房与城乡建设局网站百度站长快速收录
  • 本地搭建linux服务器做网站建站公司 转型经验
  • 备案价格网站华为荣耀手机最新款
  • 溧阳网站建设价格上海微网站公司
  • 佛山手机网站建设军事新闻最新消息
  • 宁津建设局网站3小时网站建设平台
  • 四川华鸿建设有限公司网站济南网站制作培训班
  • 浙江省龙泉市建设局网站网站建设规划书费用预算
  • 浙江省住房城乡建设厅网站六安网站建设找哪家
  • 长春网站分析门户网站建设课程设计
  • 深圳网站建设公司设计公司长沙公交优化
  • 做图片带字的网站黑龙江省建设厅官网
  • 电子书新手学做网站wordpress默认密码恢复
  • 南京建设网站首页淄博网站排名seo
  • 衡阳网站页面设计公司wordpress后台登录
  • 千博企业网站管理系统2013网页传奇血饮龙纹攻略
  • 公众号如何推广如何做网站seo
  • 做第三方的qq互联接口时_回调到自己的网站时要延时很久是什么原因深圳做网站报价
  • 编程网站入口wordpress的ssl
  • PS做游戏网站需要做几个网页东莞百姓网交友
  • 医疗网站模板下载wordpress站迁移后速度慢
  • 单位网站备案流程wordpress隐私页
  • 哪些网站专门做细胞的山东网站建设xywlcn
  • 品网站建设公司排名企业网站建设知乎