接昨天未完待續(xù)糠馆,繼續(xù):
首先,我要完成功能是:將下面的電影的links數(shù)據(jù)怎憋,在Spark上處理又碌,處理結(jié)果存入到Hive中
這個是最后成功的圖
功能流程如下圖:
涉及的代碼如下:
object ETL {
def main(args: Array[String]): Unit = {
val localClusterURL = "local[2]"
val clusterMasterURL = "spark://s1:7077"
val conf = new SparkConf().setAppName("ETL").setMaster(clusterMasterURL)
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
val hc = new HiveContext(sc)
import sqlContext.implicits._
hc.sql("use moive_recommend")
// 設置RDD的partition的數(shù)量一般以集群分配給應用的CPU核數(shù)的整數(shù)倍為宜。
val minPartitions = 8
val links = sc.textFile(args(0),minPartitions).filter{ !_.endsWith(",")}.map(_.split(","))
.map(x =>Links(x(0).trim.toInt,x(1).trim().toInt,x(2).trim().toInt)).toDF()
links.write.mode(SaveMode.Overwrite).parquet("/home/spark/temp/moiveRec/links")
hc.sql("drop table if exists links")
hc.sql("create table if not exists links(moiveId int,imdbId int,tmdbId int) stored as parquet" )
hc.sql("load data inpath '/home/spark/temp/moiveRec/links' overwrite into table links")
}
}
從一開始绊袋,我就有一個疑惑:
如何建立Windows毕匀、Linux、HDFS癌别、Spark皂岔、Hive、MySql間的聯(lián)系展姐,進行通訊?
后面所有的行為都是為了解決這個問題躁垛。
1 Windows和Linux間的聯(lián)系
用的這個剖毯,不多說,百度教馆。
所有的文件逊谋、jar都是通過這個與Linux交互。
2 Windows和Spark交互
需要將最開始的代碼土铺,打成jar包胶滋,通過上面的WinSCP傳入spark的lib(自己指定)下。
而IDEA編譯Scala代碼需要在Maven中做如下配置
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<recompileMode>incremental</recompileMode>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<mainClass>org.brave.spark.streaming.Producer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
然后舒憾,做如圖右側(cè)選擇镀钓,將項目打成左側(cè)jar包
3 HDFS和Spark交互
接下來到了比較困難的部分,主要是下面5行代碼镀迂。
敲黑板啦6〗Α!探遵!
val links = sc.textFile(args(0),minPartitions).filter{ !_.endsWith(",")}.map(_.split(","))
.map(x =>Links(x(0).trim.toInt,x(1).trim().toInt,x(2).trim().toInt)).toDF()
links.write.mode(SaveMode.Overwrite).parquet("/home/spark/temp/moiveRec/links")
hc.sql("drop table if exists links")
hc.sql("create table if not exists links(moiveId int,imdbId int,tmdbId int) stored as parquet" )
hc.sql("load data inpath '/home/spark/temp/moiveRec/links' overwrite into table links")
我最終運行的命令如下:
最后一部分參數(shù)窟赏,是links.txt文件的地址,文件存在Linux上箱季,也上傳在HDFS上
/home/spark/temp/moiveRec/links.txt
我不懂:
怎么將HDFS的數(shù)據(jù)傳入到spark上運行涯穷?或者說怎么區(qū)分到底傳入的是Linux的數(shù)據(jù)還是HDFS數(shù)據(jù)?
寫HDFS會報錯藏雏,如果寫Linux本地的數(shù)據(jù)拷况,后面的
hc.sql("drop table if exists links")
hc.sql("create table if not exists links(moiveId int,imdbId int,tmdbId int) stored as parquet" )
hc.sql("load data inpath '/home/spark/temp/moiveRec/links' overwrite into table links")
語句會找不到數(shù)據(jù)庫,無法新建數(shù)據(jù)表掘殴。
于是問題變?yōu)椋篠park如何與Hive交互(最難的地方赚瘦,也是突破點)
4 Hive與Mysql交互
要解決這個問題,首先還要解決Hive與Mysql間的交互問題奏寨。詳情見:
配置一臺Hive + Mysql元數(shù)據(jù)庫
5 Spark和Hive交互
OK起意,最后一步的交互。
首先病瞳,配置一臺Hive + Mysql元數(shù)據(jù)庫里面最后關于
講述不對揽咕,需要這一部分的配置,不能為空套菜,原因如下:
https://www.cnblogs.com/linbingdong/p/5829369.html
最后配置如下:
繼續(xù)啟動Hive亲善,報錯如下:
解決方法:
https://blog.csdn.net/blueheart20/article/details/38460541
到此,Hive已經(jīng)配置好了Metastore逗柴。
最終的關鍵問題的解決還是依靠官方文檔
就是說蛹头,要把hive-site.xml,core-site.xml,hdfs-site.xml都放入到Spark的conf目錄下掘而。
還需要將mysql的驅(qū)動放入到lib目錄下(高版本是jars目錄)
成功
最終再次運行下面代碼
沒在報錯,成功寫入到Hive中于购。
總結(jié):遇到問題時袍睡,要善于對其轉(zhuǎn)化,轉(zhuǎn)化為能用簡單的keywords描述肋僧。然后帶著keywords:(1)首先去官網(wǎng)查斑胜;(2)然后是搜索;
END