Spark Streaming 项目实战(6)——数据库访问DAO层方法实现

1 源码

  • CourseClickCount.scala
package streamingproject.domian
/*
* 实战课程点击数
*
* */
case class CourseClickCount(day_course: String, click_count: Long)

  • CourseClickCountDAO.scala
package streamingproject.dao

import org.apache.hadoop.hbase.client.Get
import org.apache.hadoop.hbase.util.Bytes
import streamingproject.domian.CourseClickCount
import streamingproject.utils.HBaseUtils

import scala.collection.mutable.ListBuffer

/*
* 实战课程数据访问层
* */
object CourseClickCountDAO {
  val tableName = "course_clickcount"
  val cf = "info"
  val qualifer = "click_count"

  /**
    * @Description:
    * @param list CourseClickCount集合
    * @return: void
    **/
  def save(list: ListBuffer[CourseClickCount]): Unit = {
    val table = HBaseUtils.getInstance().getTable(tableName)
    for (ele <- list) {
      table.incrementColumnValue(Bytes.toBytes(ele.day_course),
        Bytes.toBytes(cf),
        Bytes.toBytes(qualifer),
        ele.click_count)
    }
  }

  /*
  * 根据rowkey 查询值
  *
  * */
  def count(day_course: String): Long = {
    val table = HBaseUtils.getInstance().getTable(tableName)
    val get = new Get(Bytes.toBytes(day_course))
    val value = table.get(get).getValue(cf.getBytes, qualifer.getBytes)
    if (value == null) {
      0l
    } else {
      Bytes.toLong(value)
    }
  }

  def main(args: Array[String]): Unit = {
    val list = new ListBuffer[CourseClickCount]
    list.append(CourseClickCount("20181111_8",8))
    list.append(CourseClickCount("20181111_9",9))
    list.append(CourseClickCount("20181111_1",100))

    save(list)

  }
}

Spark Streaming 项目实战(6)——数据库访问DAO层方法实现_第1张图片

1.1 调整源码测试

  • 直接打印
    Spark Streaming 项目实战(6)——数据库访问DAO层方法实现_第2张图片
  • 修改源码再次运行
    Spark Streaming 项目实战(6)——数据库访问DAO层方法实现_第3张图片

你可能感兴趣的:(#,Spark)