成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

pyspark訪問hive數(shù)據(jù)實(shí)戰(zhàn)

大數(shù)據(jù) Spark
直接進(jìn)行spark開發(fā)需要去學(xué)習(xí)scala,為了降低數(shù)據(jù)分析師的學(xué)習(xí)成本,決定前期先試用sparkSQL,能夠讓計(jì)算引擎無縫從MR切換到spark,現(xiàn)在主要使用pyspark訪問hive數(shù)據(jù)。

數(shù)據(jù)分析都是直接使用hive腳本進(jìn)行調(diào)用,隨著APP用戶行為和日志數(shù)據(jù)量的逐漸累積,跑每天的腳本運(yùn)行需要花的時(shí)間越來越長(zhǎng),雖然進(jìn)行了sql優(yōu)化,但是上spark已經(jīng)提上日程。

直接進(jìn)行spark開發(fā)需要去學(xué)習(xí)scala,為了降低數(shù)據(jù)分析師的學(xué)習(xí)成本,決定前期先試用sparkSQL,能夠讓計(jì)算引擎無縫從MR切換到spark,現(xiàn)在主要使用pyspark訪問hive數(shù)據(jù)。

以下是安裝配置過程中的詳細(xì)步驟:

1.安裝spark

需要先安裝JDK和scala,這不必多說,由于現(xiàn)有hadoop集群版本是采用的2.6.3,所以spark版本是下載的穩(wěn)定版本spark-1.4.0-bin-hadoop2.6.tgz

我是先在一臺(tái)機(jī)器上完成了Spark的部署,Master和Slave都在一臺(tái)機(jī)器上。注意要配置免秘鑰ssh登陸。

1.1 環(huán)境變量配置

  1. export JAVA_HOME=/usr/jdk1.8.0_73 
  2. export HADOOP_HOME=/usr/hadoop 
  3. export HADOOP_CONF_DIR=/usr/hadoop/etc/hadoop 
  4. export SCALA_HOME=/usr/local/scala-2.11.7 
  5. export SPARK_HOME=/home/hadoop/spark_folder/spark-1.4.0-bin-hadoop2.6 
  6. export SPARK_MASTER_IP=127.0.0.1 
  7. export SPARK_MASTER_PORT=7077 
  8. export SPARK_MASTER_WEBUI_PORT=8099 
  9.   
  10. export SPARK_WORKER_CORES=3     //每個(gè)Worker使用的CPU核數(shù) 
  11. export SPARK_WORKER_INSTANCES=1   //每個(gè)Slave中啟動(dòng)幾個(gè)Worker實(shí)例 
  12. export SPARK_WORKER_MEMORY=10G    //每個(gè)Worker使用多大的內(nèi)存 
  13. export SPARK_WORKER_WEBUI_PORT=8081 //Worker的WebUI端口號(hào) 
  14. export SPARK_EXECUTOR_CORES=1       //每個(gè)Executor使用使用的核數(shù) 
  15. export SPARK_EXECUTOR_MEMORY=1G     //每個(gè)Executor使用的內(nèi)存 
  16.  
  17. export HIVE_HOME=/home/hadoop/hive 
  18. export SPARK_CLASSPATH=$HIVE_HOME/lib/mysql-connector-java-5.1.31-bin.jar:$SPARK_CLASSPATH 
  19. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$HADOOP_HOME/lib/native 

1.2 配置slaves

  1. cp slaves.template slaves 
  2. vi slaves 添加以下內(nèi)容:localhost 

1.3 啟動(dòng)master和slave

  1. cd $SPARK_HOME/sbin/ 
  2. ./start-master.sh 
  3.  
  4. 啟動(dòng)日志位于 $SPARK_HOME/logs/目錄,訪問 http://localhost:8099,即可看到Spark的WebUI界面 
  5.  
  6. 執(zhí)行 ./bin/spark-shell,打開Scala到Spark的連接窗口    

2.SparkSQL與Hive的整合

  1. 拷貝$HIVE_HOME/conf/hive-site.xml和hive-log4j.properties到 $SPARK_HOME/conf/  
  2. 在$SPARK_HOME/conf/目錄中,修改spark-env.sh,添加  
  3. export HIVE_HOME=/home/hadoop/hive 
  4. export SPARK_CLASSPATH=$HIVE_HOME/lib/mysql-connector-java-5.1.31-bin.jar:$SPARK_CLASSPATH 
  5. 另外也可以設(shè)置一下Spark的log4j配置文件,使得屏幕中不打印額外的INFO信息(如果不想受干擾可設(shè)置為更高):  
  6. log4j.rootCategory=WARN, console  
  7. 進(jìn)入$SPARK_HOME/bin,執(zhí)行 ./spark-sql –master spark://127.0.0.1:7077 進(jìn)入spark-sql CLI: 
  8. [hadoop@hadoop spark]$ bin/spark-sql --help   
  9. Usage: ./bin/spark-sql [options] [cli option]   
  10. CLI options:   
  11.  -d,--define <keykey=value>          Variable subsitution to apply to hive   
  12.                                   commands. e.g. -d A=B or --define A=B   
  13.     --database <databasename>     Specify the database to use   
  14.  -e <quoted-query-string>         SQL from command line   
  15.  -f <filename>                    SQL from files   
  16.  -h <hostname>                    connecting to Hive Server on remote host   
  17.     --hiveconf <propertyproperty=value>   Use value for given property   
  18.     --hivevar <keykey=value>         Variable subsitution to apply to hive   
  19.                                   commands. e.g. --hivevar A=B   
  20.  -i <filename>                    Initialization SQL file   
  21.  -p <port>                        connecting to Hive Server on port number   
  22.  -S,--silent                      Silent mode in interactive shell   
  23.  -v,--verbose                     Verbose mode (echo executed SQL to the   
  24.                                   console)   

需要注意的是CLI不是使用JDBC連接,所以不能連接到ThriftServer;但可以配置conf/hive-site.xml連接到hive的metastore,然后對(duì)hive數(shù)據(jù)進(jìn)行查詢。下面我們接著說如何在python中連接hive數(shù)據(jù)表查詢。

3.配置pyspark和示例代碼

3.1 配置pyspark

  1. 打開/etc/profile: 
  2.         #PythonPath 將Spark中的pySpark模塊增加的Python環(huán)境中 
  3.          export PYTHONPATH=/opt/spark-hadoop/python 
  4.         source /etc/profile   

執(zhí)行./bin/pyspark ,打開Python到Spark的連接窗口,確認(rèn)沒有報(bào)錯(cuò)。

打開命令行窗口,輸入python,Python版本為2.7.6,如圖所示,注意Spark暫時(shí)不支持Python3。輸入import pyspark不報(bào)錯(cuò),證明開發(fā)前工作已經(jīng)完成。

3.2 啟動(dòng)ThriftServer

啟動(dòng)ThriftServer,使之運(yùn)行在spark集群中:

sbin/start-thriftserver.sh --master spark://localhost:7077 --executor-memory 5g

ThriftServer可以連接多個(gè)JDBC/ODBC客戶端,并相互之間可以共享數(shù)據(jù)。

3.3 請(qǐng)求示例

查看spark官方文檔說明,spark1.4和2.0對(duì)于sparksql調(diào)用hive數(shù)據(jù)的API變化并不大。都是用sparkContext 。

  1. from pyspark import SparkConf, SparkContext 
  2. from pyspark.sql import HiveContext 
  3.  
  4. conf = (SparkConf() 
  5.          .setMaster("spark://127.0.0.1:7077"
  6.          .setAppName("My app"
  7.          .set("spark.executor.memory""1g")) 
  8. sc = SparkContext(conf = conf) 
  9. sqlContext = HiveContext(sc) 
  10. my_dataframe = sqlContext.sql("Select count(1) from logs.fmnews_dim_where"
  11. my_dataframe.show() 

返回結(jié)果:

運(yùn)行以后在webUI界面看到j(luò)ob運(yùn)行詳情。

4.性能比較

截取了接近一個(gè)月的用戶行為數(shù)據(jù),數(shù)據(jù)大小為2G,總共接近1600w條記錄。

為了測(cè)試不同sql需求情況下的結(jié)果,我們選取了日常運(yùn)行的2類sql:

1.統(tǒng)計(jì)數(shù)據(jù)條數(shù):

  1. select count(1) from fmnews_user_log2; 

2.統(tǒng)計(jì)用戶行為:

  1. SELECT device_id, min_time FROM 
  2.         (SELECT device_id,min(import_time) min_time FROM fmnews_user_log2 
  3.             GROUP BY device_id)a 
  4.         WHERE from_unixtime(int(substr(min_time,0,10)),'yyyy-MM-dd') = '2017-03-02'

3. 用戶行為分析:

  1. select case when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '06:00' and '07:59' then 1 
  2.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '08:00' and '09:59' then 2 
  3.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '10:00' and '11:59' then 3 
  4.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '12:00' and '13:59' then 4 
  5.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '14:00' and '15:59' then 5 
  6.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '16:00' and '17:59' then 6 
  7.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '18:00' and '19:59' then 7 
  8.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '20:00' and '21:59' then 8 
  9.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '22:00' and '23:59' then 9 
  10.             else 0 end fmnews_time_type, count(distinct device_id) device_count,count(1) click_count 
  11.        from fmcm.fmnews_user_log2 
  12.      where from_unixtime(int(substr(import_time,0,10)),'yyyy-MM-dd') = '2017-03-02' 
  13.     group by case when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '06:00' and '07:59' then 1 
  14.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '08:00' and '09:59' then 2 
  15.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '10:00' and '11:59' then 3 
  16.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '12:00' and '13:59' then 4 
  17.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '14:00' and '15:59' then 5 
  18.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '16:00' and '17:59' then 6 
  19.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '18:00' and '19:59' then 7 
  20.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '20:00' and '21:59' then 8 
  21.             when from_unixtime(int(substr(fmnews_time,0,10)),'HH:mm'between '22:00' and '23:59' then 9 
  22.             else 0 end

第一條sql的執(zhí)行結(jié)果對(duì)比:hive 35.013 seconds

第一條sql的執(zhí)行結(jié)果對(duì)比:sparksql 1.218 seconds

第二條sql的執(zhí)行結(jié)果對(duì)比:hive 78.101 seconds

第二條sql的執(zhí)行結(jié)果對(duì)比:sparksql 8.669 seconds

第三條sql的執(zhí)行結(jié)果對(duì)比:hive 101.228 seconds

第三條sql的執(zhí)行結(jié)果對(duì)比:sparksql 14.221 seconds

可以看到,雖然沒有官網(wǎng)吹破天的100倍性能提升,但是根據(jù)sql的復(fù)雜度來看10~30倍的效率還是可以達(dá)到的。

不過這里要注意到2個(gè)影響因子:

1. 我們數(shù)據(jù)集并沒有采取全量,在數(shù)據(jù)量達(dá)到TB級(jí)別兩者的差距應(yīng)該會(huì)有所減小。同時(shí)sql也沒有針對(duì)hive做優(yōu)化。

2. spark暫時(shí)是單機(jī)(內(nèi)存足夠)并沒有搭建集群,hive使用的hadoop集群有4臺(tái)datanode。

責(zé)任編輯:武曉燕 來源: oschina博客
相關(guān)推薦

2016-05-09 10:16:14

MapReduce數(shù)據(jù)分析明星微博

2010-05-27 18:30:56

MySQL 命令行導(dǎo)入

2010-07-22 16:21:33

SQL Server使

2013-07-04 11:14:44

2015-11-28 10:15:36

WOT2015大數(shù)據(jù)互聯(lián)網(wǎng)+

2017-11-22 09:20:41

數(shù)據(jù)庫在線數(shù)據(jù)遷移Subscriptio

2021-02-09 09:51:58

異步傳遞數(shù)據(jù)

2023-11-30 11:45:07

大數(shù)據(jù)ODPS

2023-07-10 18:48:29

PySpark數(shù)據(jù)結(jié)構(gòu)

2013-04-23 14:36:54

2010-04-16 15:24:00

Oracle數(shù)據(jù)

2014-08-20 09:40:56

大數(shù)據(jù)實(shí)踐項(xiàng)目

2013-10-18 15:49:15

微軟大數(shù)據(jù)微軟

2023-01-31 08:34:19

2015-05-05 09:50:45

大數(shù)據(jù)實(shí)現(xiàn)塊數(shù)據(jù)

2021-02-26 05:21:56

MySQL數(shù)據(jù)設(shè)計(jì)

2022-11-17 11:52:35

pandasPySpark大數(shù)據(jù)

2009-03-10 08:54:19

RMANEXP、IMP數(shù)據(jù)轉(zhuǎn)移

2010-06-17 09:29:32

SQLServer 2

2010-01-12 14:02:14

VB.NET數(shù)據(jù)實(shí)體層
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 99精品久久 | 亚洲自拍偷拍视频 | 精品欧美乱码久久久久久 | 国产精品久久久久久久免费观看 | 人人爽日日躁夜夜躁尤物 | 国产乱一区二区三区视频 | 精品国产欧美一区二区 | 香蕉婷婷 | a在线观看 | 很黄很污的网站 | 国产高清免费 | 日韩精品人成在线播放 | 黄网站在线播放 | 久久精品国产久精国产 | 在线播放一区二区三区 | 日韩高清成人 | 久久不射电影网 | 欧美三级成人理伦 | 婷婷在线视频 | 毛片一级片 | 天堂色综合 | 色av一区二区三区 | 免费h视频 | 国产a区 | 国产清纯白嫩初高生视频在线观看 | 91国产在线视频在线 | 一区二区中文字幕 | 日韩高清国产一区在线 | 国产精品日韩高清伦字幕搜索 | 国产日韩欧美另类 | 欧美日韩一区在线 | 在线观看成人小视频 | 日韩精品一区二区三区中文字幕 | 天天干国产 | 国产黄色在线 | 天天综合网91 | 午夜精品视频在线观看 | 久久久精彩视频 | 亚洲国产aⅴ成人精品无吗 亚洲精品久久久一区二区三区 | 365夜爽爽欧美性午夜免费视频 | 欧美在线一二三 |