话单分析之 HbaseDao

object HbaseDao {

private val sdf1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

private val sdf2 =new SimpleDateFormat("yyyyMMddHHmmss")

//实现批量添加

  private val cacheList =new util.ArrayList[Put]

//创建Habse环境对象

  var conf: Configuration = HBaseConfiguration.create()

//读取region数

  private val regions: Integer = Integer.valueOf(PropertiesUtil.getProperty("hbase.calllog.regions"))

//读取命名空间

  private val nameSpace:String = PropertiesUtil.getProperty("hbase.calllog.namespace")

//读取表名

  private val tableName:String = PropertiesUtil.getProperty("hbase.calllog.tablename")

var table:HTable =null

  //首先创建命名空间,在创建表

  if(!HBaseUtil.isExistTable(conf,tableName)){

HBaseUtil.initNamespace(conf,nameSpace)

HBaseUtil.createTable(conf,tableName,regions,"f1","f2")

}

/**

  * 把数据写入到hbase

  * ori数据样式: 18576581848,17269452013,2017-08-14 13:38:31,1761

  * rowkey样式:01_18576581848_20170814133831_17269452013_1_1761

  * HBase表的列:call1  call2  build_time  build_time_ts  flag  duration

*

*

*/

  def put(str:String) = {

if(cacheList.size ==0){

val conn = ConnectionInstance.getConnection(conf)

table = conn.getTable(TableName.valueOf(tableName)).asInstanceOf[HTable]

table.setAutoFlushTo(false)

table.setWriteBufferSize(2 *1024 *1024)

}

//对数据进行切割

    val splitOri = str.split(",")

val call1:String = splitOri(0)

val call1_name:String = splitOri(1)

val call2:String = splitOri(2)

val call2_name:String = splitOri(3)

val buildTime:String = splitOri(4)

val duration:String = splitOri(5)

val flag:String = splitOri(6)

//获取region编码

    val regionCode = HBaseUtil.genRegionCode(call1, buildTime,regions)

//建立通话时间

    val buildTimeReplace =sdf2.format(sdf1.parse(buildTime))

val buildTimeTs:String = String.valueOf(sdf1.parse(buildTime).getTime)

//生成rowKey

    val rowkey = HBaseUtil.genRowKey(regionCode, call1, buildTimeReplace, call2, flag, duration)

//向表中插入数据

    val put: Put =new Put(Bytes.toBytes(rowkey))

//主叫号码

    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("call1"),Bytes.toBytes(call1))

//主叫名称

    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("call1_name"),Bytes.toBytes(call1_name))

//被叫号码

    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("call2"),Bytes.toBytes(call2))

//被叫名称

    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("call2_name"),Bytes.toBytes(call2_name))

//通话日期

    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("build_time"),Bytes.toBytes(buildTime))

//通话时间

    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("build_time_ts"),Bytes.toBytes(buildTimeTs))

//通话标识

    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("flag"),Bytes.toBytes(flag))

//通话时间

    put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("duration"),Bytes.toBytes(duration))

cacheList.add(put)

if(cacheList.size >0){

table.put(cacheList)

table.flushCommits()

cacheList.clear()

}

}

你可能感兴趣的:(话单分析之 HbaseDao)