標籤:資源 target bsp blank 外部 path 檔案 conf art
關於spark 的詳細操作請參照spark官網
scala 版本:2.11.8
1.添加spark maven依賴,如需訪問hdfs,則添加hdfs依賴
groupId = org.apache.sparkartifactId = spark-core_2.11version = 2.3.2groupId = org.apache.hadoopartifactId = hadoop-clientversion = <your-hdfs-version>
2.sparkcontext 的建立
val conf = new SparkConf().setAppName("example").setMaster("local[*]") val sc = new SparkContext(conf) sc.stop()
sc 使用結束,記得關閉
3.建立rdd
1)parallelized 方法
val words = sc.parallelize(Array("dong","jason","puma","large"),2)
2)讀取外部資料
val rdd = sc.textFile("path_to_file(local or hdfs)")
一個放重要的概念,partitions,spark在邏輯上回對資料進行分區,每個分區會安排一個task來處理,textfile 如果讀取的時hdfs,則預設partitions 是 檔案的block數,
一般情況下為資源中每個cpu分配 2-4 個task為宜
4. SparkContext.wholeTextFiles
val rdd = sc.wholeTextFiles("./") rdd.take(1).foreach(println)-----------------------------------(file:/C:/notos/code/sailertest/aa.csv,name,agejason,29dong,27)
其輸出結果時一個元組,(filepath,filecontent)
spark 基本操作整理