分享一下spark streaming與flume整合的scala代碼。

來源:互聯網
上載者:User

標籤:style   blog   ar   io   color   os   使用   sp   for   

object LogicHandle {  def main(args: Array[String]) {    //添加這個不會報執行錯誤    val path = new File(".").getCanonicalPath()    System.getProperties().put("hadoop.home.dir", path);    new File("./bin").mkdirs();    new File("./bin/winutils.exe").createNewFile();    //val sparkConf = new SparkConf().setAppName("SensorRealTime").setMaster("local[2]")    val sparkConf = new SparkConf().setAppName("SensorRealTime")    val ssc = new StreamingContext(sparkConf, Seconds(20))    val hostname = "localhost"    val port = 2345    val storageLevel = StorageLevel.MEMORY_ONLY    val flumeStream = FlumeUtils.createStream(ssc, hostname, port, storageLevel)    val lhc = new LogicHandleClass();    //日誌格式化模板    val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    val sdfHour = new SimpleDateFormat("HH");    val sdfMinute = new SimpleDateFormat("mm")    //儲存資料的hash對象  key/value儲存  根據文檔規則,使用各統計指標的key/value    var redisMap = new HashMap[String, String]
  
     flumeStream.foreachRDD(rdd
=> { val events = rdd.collect() //println("event count:" + events.length) var i = 1 for (event <- events) { val sensorInfo = new String(event.event.getBody.array()) //單行記錄 //單行記錄格式化 val arrayFileds = sensorInfo.split(",") if (arrayFileds.length == 6) { val shopId = arrayFileds(0) //店內編號 val floorId = shopId.substring(0, 5) //樓層編號 val mac = arrayFileds(1) val ts = arrayFileds(2).toLong //時間戳記 val time = sdf.format(ts * 1000) var hour = sdfHour.format(ts * 1000) var minute = sdfMinute.format(ts * 1000) var allMinute = hour.toInt * 60 + minute.toInt val x = arrayFileds(3) val y = arrayFileds(4) val level = arrayFileds(5) //後邊就是我的業務代碼了,省略了 } } //儲存至redis中 lhc.SetAll(redisMap) }) ssc.start() ssc.awaitTermination() }}

 

分享一下spark streaming與flume整合的scala代碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.