spark 基本操作整理

來源:互聯網
上載者:User

標籤:資源   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 基本操作整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.