开发者社区> 问答> 正文

E-MapReduce Spark + Hbase是什么?



Spark + Hbase



Spark 接入 Hbase


下面这个例子演示了 Spark 如何向 Hbase 写数据。需要指出的是,计算集群需要和 Hbase 集群处于一个安全组内,否则网络无法打通。在 E-Mapreduce 创建集群时,请注意选择 Hbase 集群所处的安全组。

  1. [backcolor=transparent]    [backcolor=transparent]object[backcolor=transparent] [backcolor=transparent]ConnectionUtil[backcolor=transparent] [backcolor=transparent]extends[backcolor=transparent] [backcolor=transparent]Serializable[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]      [backcolor=transparent]private[backcolor=transparent] val conf [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]HBaseConfiguration[backcolor=transparent].[backcolor=transparent]create[backcolor=transparent]()
  3. [backcolor=transparent]      conf[backcolor=transparent].[backcolor=transparent]set[backcolor=transparent]([backcolor=transparent]HConstants[backcolor=transparent].[backcolor=transparent]ZOOKEEPER_QUORUM[backcolor=transparent],[backcolor=transparent]"ecs1,ecs1,ecs3"[backcolor=transparent])
  4. [backcolor=transparent]      conf[backcolor=transparent].[backcolor=transparent]set[backcolor=transparent]([backcolor=transparent]HConstants[backcolor=transparent].[backcolor=transparent]ZOOKEEPER_ZNODE_PARENT[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"/hbase"[backcolor=transparent])
  5. [backcolor=transparent]      [backcolor=transparent]private[backcolor=transparent] val connection [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]ConnectionFactory[backcolor=transparent].[backcolor=transparent]createConnection[backcolor=transparent]([backcolor=transparent]conf[backcolor=transparent])
  6. [backcolor=transparent]      [backcolor=transparent]def[backcolor=transparent] getDefaultConn[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]Connection[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] connection
  7. [backcolor=transparent]    [backcolor=transparent]}
  8. [backcolor=transparent]    [backcolor=transparent]//创建数据流 unionStreams
  9. [backcolor=transparent]    unionStreams[backcolor=transparent].[backcolor=transparent]foreachRDD[backcolor=transparent]([backcolor=transparent]rdd [backcolor=transparent]=>[backcolor=transparent] [backcolor=transparent]{
  10. [backcolor=transparent]      rdd[backcolor=transparent].[backcolor=transparent]map[backcolor=transparent]([backcolor=transparent]bytes [backcolor=transparent]=>[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]([backcolor=transparent]bytes[backcolor=transparent]))
  11. [backcolor=transparent]        [backcolor=transparent].[backcolor=transparent]flatMap[backcolor=transparent]([backcolor=transparent]line [backcolor=transparent]=>[backcolor=transparent] line[backcolor=transparent].[backcolor=transparent]split[backcolor=transparent]([backcolor=transparent]" "[backcolor=transparent]))
  12. [backcolor=transparent]        [backcolor=transparent].[backcolor=transparent]map[backcolor=transparent]([backcolor=transparent]word [backcolor=transparent]=>[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]word[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]1[backcolor=transparent]))
  13. [backcolor=transparent]        [backcolor=transparent].[backcolor=transparent]reduceByKey[backcolor=transparent]([backcolor=transparent]_ [backcolor=transparent]+[backcolor=transparent] _[backcolor=transparent])
  14. [backcolor=transparent]        [backcolor=transparent].[backcolor=transparent]mapPartitions [backcolor=transparent]{[backcolor=transparent]words [backcolor=transparent]=>[backcolor=transparent] [backcolor=transparent]{
  15. [backcolor=transparent]          val conn [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]ConnectionUtil[backcolor=transparent].[backcolor=transparent]getDefaultConn
  16. [backcolor=transparent]          val tableName [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]TableName[backcolor=transparent].[backcolor=transparent]valueOf[backcolor=transparent]([backcolor=transparent]tname[backcolor=transparent])
  17. [backcolor=transparent]          val t [backcolor=transparent]=[backcolor=transparent] conn[backcolor=transparent].[backcolor=transparent]getTable[backcolor=transparent]([backcolor=transparent]tableName[backcolor=transparent])
  18. [backcolor=transparent]          [backcolor=transparent]try[backcolor=transparent] [backcolor=transparent]{
  19. [backcolor=transparent]            words[backcolor=transparent].[backcolor=transparent]sliding[backcolor=transparent]([backcolor=transparent]100[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]100[backcolor=transparent]).[backcolor=transparent]foreach[backcolor=transparent]([backcolor=transparent]slice [backcolor=transparent]=>[backcolor=transparent] [backcolor=transparent]{
  20. [backcolor=transparent]              val puts [backcolor=transparent]=[backcolor=transparent] slice[backcolor=transparent].[backcolor=transparent]map[backcolor=transparent]([backcolor=transparent]word [backcolor=transparent]=>[backcolor=transparent] [backcolor=transparent]{
  21. [backcolor=transparent]                println[backcolor=transparent]([backcolor=transparent]s[backcolor=transparent]"word: $word"[backcolor=transparent])
  22. [backcolor=transparent]                val put [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Put[backcolor=transparent]([backcolor=transparent]Bytes[backcolor=transparent].[backcolor=transparent]toBytes[backcolor=transparent]([backcolor=transparent]word[backcolor=transparent].[backcolor=transparent]_1 [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]currentTimeMillis[backcolor=transparent]()))
  23. [backcolor=transparent]                put[backcolor=transparent].[backcolor=transparent]addColumn[backcolor=transparent]([backcolor=transparent]COLUMN_FAMILY_BYTES[backcolor=transparent],[backcolor=transparent] COLUMN_QUALIFIER_BYTES[backcolor=transparent],
  24. [backcolor=transparent]                  [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]currentTimeMillis[backcolor=transparent](),[backcolor=transparent] [backcolor=transparent]Bytes[backcolor=transparent].[backcolor=transparent]toBytes[backcolor=transparent]([backcolor=transparent]word[backcolor=transparent].[backcolor=transparent]_2[backcolor=transparent]))
  25. [backcolor=transparent]                put
  26. [backcolor=transparent]              [backcolor=transparent]}).[backcolor=transparent]toList
  27. [backcolor=transparent]              t[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]puts[backcolor=transparent])
  28. [backcolor=transparent]            [backcolor=transparent]})
  29. [backcolor=transparent]          [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]finally[backcolor=transparent] [backcolor=transparent]{
  30. [backcolor=transparent]            t[backcolor=transparent].[backcolor=transparent]close[backcolor=transparent]()
  31. [backcolor=transparent]          [backcolor=transparent]}
  32. [backcolor=transparent]          [backcolor=transparent]Iterator[backcolor=transparent].[backcolor=transparent]empty
  33. [backcolor=transparent]        [backcolor=transparent]}}.[backcolor=transparent]count[backcolor=transparent]()
  34. [backcolor=transparent]    [backcolor=transparent]})
  35. [backcolor=transparent]    ssc[backcolor=transparent].[backcolor=transparent]start[backcolor=transparent]()
  36. [backcolor=transparent]    ssc[backcolor=transparent].[backcolor=transparent]awaitTermination[backcolor=transparent]()


附录


完整示例代码请看:

展开
收起
nicenelly 2017-10-27 16:08:10 1375 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Hybrid Cloud and Apache Spark 立即下载
Scalable Deep Learning on Spark 立即下载
Comparison of Spark SQL with Hive 立即下载

相关实验场景

更多