• 背景:

接到任务,需要在一个一天数据量在460亿条记录的hive表中,筛选出某些host为特定的值时才解析该条记录的http_content中的经纬度:

解析规则譬如:

需要解析host: api.map.baidu.com
需要解析的规则:"result":{"location":{"lng":120.25088311933617,"lat":30.310684375444877},
"confidence":25
需要解析http_conent:renderReverse&&renderReverse({"status":0,"result":{"location":{"lng":120.25088311933617,"lat":30.310684375444877},"formatted_address":"???????????????????????????????????????","business":"","addressComponent":{"country":"??????","country_code":0,"province":"?????????","city":"?????????","district":"?????????","adcode":"330104","street":"????????????","street_number":"","direction":"","distance":""},"pois":[{"addr":"????????????5277???","cp":" ","direction":"???","distance":"68","name":"????????????????????????????????????","poiType":"????????????","point":{"x":120.25084961536486,"y":30.3112150
  • Scala代码实现“访问hive,并保存结果到hive表”的spark任务:

开发工具为IDEA16,开发语言为scala,开发包有了spark对应集群版本下的很多个jar包,和对应集群版本下的很多个jar包,引入jar包:

scala代码:

import java.sql.{Connection, DriverManager, PreparedStatement, Timestamp}

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.sql.hive.HiveContext
import java.util
import java.util.{UUID, Calendar, Properties}
import org.apache.spark.rdd.JdbcRDD
import org.apache.spark.sql.{Row, SaveMode, SQLContext}
import org.apache.spark.storage.StorageLevel
import org.apache.spark.{sql, SparkContext, SparkConf}
import org.apache.spark.sql.DataFrameHolder /**
* temp http_content
**/
case class Temp_Http_Content_ParserResult(success: String, lnglatType: String, longitude: String, Latitude: String, radius: String) /**
* Created by Administrator on 2016/11/15.
*/
object ParserMain {
def main(args: Array[String]): Unit = {
val conf = new SparkConf()
//.setAppName("XXX_ParserHttp").setMaster("local[1]").setMaster("spark://172.21.7.10:7077").setJars(List("xxx.jar"))
//.set("spark.executor.memory", "10g")
val sc = new SparkContext(conf)
val hiveContext = new HiveContext(sc) // use abc_hive_db;
hiveContext.sql("use abc_hive_db")
// error date format:2016-11-15,date format must be 20161115
val rdd = hiveContext.sql("select host,http_content from default.http where hour>='20161115' and hour<'20161116'") // toDF() method need this line...
import hiveContext.implicits._ // (success, lnglatType, longitude, latitude, radius)
val rdd2 = rdd.map(s => parse_http_context(s.getAs[String]("host"), s.getAs[String]("http_content"))).filter(s => s._1).map(s => Temp_Http_Content_ParserResult(s._1.toString(), s._2, s._3, s._4, s._5)).toDF()
rdd2.registerTempTable("Temp_Http_Content_ParserResult_20161115")
hiveContext.sql("create table Temp_Http_Content_ParserResult20161115 as select * from Temp_Http_Content_ParserResult_20161115") sc.stop()
} /**
* @ summary: 解析http_context字段信息
* @ param http_context 参数信息
* @ result 1:是否匹配成功;
* @ result 2:匹配出的是什么经纬度的格式:
* @ result 3:经度;
* @ result 4:纬度,
* @ result 5:radius
**/
def parse_http_context(host: String, http_context: String): (Boolean, String, String, String, String) = {
if (host == null || http_context == null) {
return (false, "", "", "", "")
} // val result2 = parse_http_context(“api.map.baidu.com”,"renderReverse&&renderReverse({\"status\":0,\"result\":{\"location\":{\"lng\":120.25088311933617,\"lat\":30.310684375444877},\"formatted_address\":\"???????????????????????????????????????\",\"business\":\"\",\"addressComponent\":{\"country\":\"??????\",\"country_code\":0,\"province\":\"?????????\",\"city\":\"?????????\",\"district\":\"?????????\",\"adcode\":\"330104\",\"street\":\"????????????\",\"street_number\":\"\",\"direction\":\"\",\"distance\":\"\"},\"pois\":[{\"addr\":\"????????????5277???\",\"cp\":\" \",\"direction\":\"???\",\"distance\":\"68\",\"name\":\"????????????????????????????????????\",\"poiType\":\"????????????\",\"point\":{\"x\":120.25084961536486,\"y\":30.3112150")
// println(result2._1 + ":" + result2._2 + ":" + result2._3 + ":" + result2._4 + ":" + result2._5) var success = false
var lnglatType = ""
var longitude = ""
var latitude = ""
var radius = ""
var lowerCaseHost = host.toLowerCase().trim();
val lowerCaseHttp_Content = http_context.toLowerCase()
// api.map.baidu.com
// "result":{"location":{"lng":120.25088311933617,"lat":30.310684375444877},
// "confidence":25
// --renderReverse&&renderReverse({"status":0,"result":{"location":{"lng":120.25088311933617,"lat":30.310684375444877},"formatted_address":"???????????????????????????????????????","business":"","addressComponent":{"country":"??????","country_code":0,"province":"?????????","city":"?????????","district":"?????????","adcode":"330104","street":"????????????","street_number":"","direction":"","distance":""},"pois":[{"addr":"????????????5277???","cp":" ","direction":"???","distance":"68","name":"????????????????????????????????????","poiType":"????????????","point":{"x":120.25084961536486,"y":30.3112150
if (lowerCaseHost.equals("api.map.baidu.com")) {
val indexLng = lowerCaseHttp_Content.indexOf("\"lng\"")
val indexLat = lowerCaseHttp_Content.indexOf("\"lat\"")
if (lowerCaseHttp_Content.indexOf("\"location\"") != -1 && indexLng != -1 && indexLat != -1) {
var splitstr: String = "\\,|\\{|\\}"
var uriItems: Array[String] = lowerCaseHttp_Content.split(splitstr)
var tempItem: String = ""
lnglatType = "BD"
success = true
for (uriItem <- uriItems) {
tempItem = uriItem.trim()
if (tempItem.startsWith("\"lng\":")) {
longitude = tempItem.replace("\"lng\":", "").trim()
} else if (tempItem.startsWith("\"lat\":")) {
latitude = tempItem.replace("\"lat\":", "").trim()
} else if (tempItem.startsWith("\"confidence\":")) {
radius = tempItem.replace("\"confidence\":", "").trim()
}
}
}
}
else if (lowerCaseHost.equals("loc.map.baidu.com")) {
。。。
} longitude = longitude.replace("\"", "")
latitude = latitude.replace("\"", "")
radius = radius.replace("\"", "") (success, lnglatType, longitude, latitude, radius)
}
}

打包,注意应为我们使用的hadoop&hive&spark on yarn的集群,我们这里并不需要想spark&hadoop一样还需要在执行spark-submit时将spark-hadoop-xx.jar打包进来,也不需要在submit-spark脚本.sh中制定jars参数,yarn会自动诊断我们需要哪些集群系统包;但是,如果你应用的是第三方的包,比如ab.jar,那打包时可以打包进来,也可以在spark-submit 参数jars后边指定特定的包。

  • 写spark-submit提交脚本.sh:

  • 当执行spark-submit脚本出现错误时,怎么应对呢?

注意,我们这里不是spark而是spark on yarn,当我们使用yarn-cluster方式提交时,界面是看不到任何日志新的。我们需要借助yarn管理系统来查看日志:

1、根据返回的任务id查看历史日志:
yarn logs -applicationId  application_1475071482566_3329402

2、yarn页面查看日志

https://xx.xx.xx.xx:xxxxx/Yarn/ResourceManager/xxxx/cluster
用户名/密码:user/password
 
 
3、yarn关闭application:
从yarn resourcemanger界面中,可以查看到具体的applicationId,使用命令来杀掉该任务:
更多命令可以参考:http://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-site/YarnCommands.html
yarn application -kill application_1475071482566_3807023

或者从界面进入spark作业进度管理界面,进行查看作业具体执行进度,也可以kill application

参考资料:
http://blog.csdn.net/sparkexpert/article/details/50964732

Spark On YARN内存分配:http://blog.javachen.com/2015/06/09/memory-in-spark-on-yarn.html?utm_source=tuicool

Spark&Hive:如何使用scala开发spark访问hive作业,如何使用yarn resourcemanager。的更多相关文章

  1. 使用scala开发spark入门总结

    使用scala开发spark入门总结 一.spark简单介绍 关于spark的介绍网上有很多,可以自行百度和google,这里只做简单介绍.推荐简单介绍连接:http://blog.jobbole.c ...

  2. Spark之路 --- Windows Scala 开发环境安装配置

    JDK安装 JDK安装包下载 到Oracle官网下载JDK. 传送门 下载之前要记得勾选上同意协议然后选择相应的版本(Windows/Linux, 32/64) JDK安装及验证 按提示完成安装,安装 ...

  3. 【Spark】使用java语言开发spark程序

    目录 步骤 一.创建maven工程,导入jar包 二.开发代码 步骤 一.创建maven工程,导入jar包 <properties> <scala.version>2.11.8 ...

  4. 大数据学习day18----第三阶段spark01--------0.前言(分布式运算框架的核心思想,MR与Spark的比较,spark可以怎么运行,spark提交到spark集群的方式)1. spark(standalone模式)的安装 2. Spark各个角色的功能 3.SparkShell的使用,spark编程入门(wordcount案例)

    0.前言 0.1  分布式运算框架的核心思想(此处以MR运行在yarn上为例)  提交job时,resourcemanager(图中写成了master)会根据数据的量以及工作的复杂度,解析工作量,从而 ...

  5. pyinstaller打包python源程序访问hive

    1.需求 使用hvie server一段时间后,业务部门需要自己不定时的查询业务数据,之前这一块都是他们提需求我们来做,后来发现这样重复一样的工作放在我们这边做是在没有效率,遂提出给他们工具或者web ...

  6. SQL Standard Based Hive Authorization(基于SQL标准的Hive授权)

    说明:该文档翻译/整理于Hive官方文档https://cwiki.apache.org/confluence/display/Hive/SQL+Standard+Based+Hive+Authori ...

  7. Spark集群 + Akka + Kafka + Scala 开发(2) : 开发一个Spark应用

    前言 在Spark集群 + Akka + Kafka + Scala 开发(1) : 配置开发环境,我们已经部署好了一个Spark的开发环境. 本文的目标是写一个Spark应用,并可以在集群中测试. ...

  8. 利用Scala语言开发Spark应用程序

    Spark内核是由Scala语言开发的,因此使用Scala语言开发Spark应用程序是自然而然的事情.如果你对Scala语言还不太熟悉,可 以阅读网络教程A Scala Tutorial for Ja ...

  9. 使用spark访问hive错误记录

    在spark集群中执行./spark-shell时报以下错误: 18/07/23 10:02:39 WARN DataNucleus.Connection: BoneCP specified but ...

随机推荐

  1. ASP.NET使用Memcached

    一.安装Memcached及Memcached配置和状态查询 要想使用Memcached做缓存首先需要安装Memcached服务,安装方法如下: memcached.exe下载 保存至相应路径 打开c ...

  2. php 冒泡排序 快速排序

    $a=array('3','8','1','4','11','7'); print_r($a); $len = count($a); //从小到大 for($i=1;$i<$len;$i++) ...

  3. IntelliJ IDEA 14 注册码

    IntelliJ IDEA 14 下载地址: IntelliJ IDEA 14 下载 分享几个license: (1) key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7 ...

  4. iOS @@创建NSURL的字面量

    @@ 是创建 NSURL 的字面量的绝佳方法(例如:@@"http://example.com")

  5. mac系统下mysql开机启动总是3307

    修改了mysql的my.cnf可还是不行,启动后就是3307,必须关掉再启动. 觉得可能是mac系统在哪里写死了开机启动项. http://queforum.com/mysql/1012987-mys ...

  6. Leetcode jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. expected an indented block

    expected an indented block 在初步使用Python的时候遇到了" expected an indented block"报错信息,查询相关的博客得知是因为 ...

  8. Openfire3.9.3源代码导入eclipse中开发配置指南

    这篇文章向大家介绍一下,如何把openfire3.9.3源码导入eclipse中,编译并启动的详细过程. 首先得感谢这篇文章的作者,http://www.micmiu.com/opensource/o ...

  9. python与shell的3种交互方式介绍

    [目录] 1.os.system(cmd) 2.os.popen(cmd) 3.利用subprocess模块 4.subprocessor模块进阶 [概述] 考虑这样一个问题,有hello.py脚本, ...

  10. Unity学习疑问记录之触屏

    当将Unity游戏运行到ios或android设备上时,桌面系统中的鼠标左键操作可以自动变为手机屏幕上的触屏操作,但鼠标操作无法实现一些特有的触屏操作,比如多点触屏. 触控对于Android移动设备来 ...