您当前的位置: 首页 >  scala

宝哥大数据

暂无认证

  • 2浏览

    0关注

    1029博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

scala中的Short与Int问题

宝哥大数据 发布时间:2019-03-30 15:33:11 ,浏览量:2

import org.apache.hadoop.hbase.CellUtil
import org.apache.hadoop.hbase.client.Result
import org.apache.hadoop.hbase.util.Bytes

/**
  * Created by chb on 2019/3/30.
  */
object HbaseUtil {
    val defaultShort: Short = -1

    /**
      * 获取hbase表指定列的最新版本的value
      *
      * @param res
      * @param family
      * @param qualifier
      * @param defaultValue
      * @tparam T
      * @return
      */
    def getValueFromHbaseResult[T](res: Result, family: String, qualifier: String, defaultValue: T): T = {

        var outT: T = defaultValue

        if (defaultValue.isInstanceOf[Long]) {
            if (res.containsColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier))) {
                outT = Bytes.toLong(CellUtil.cloneValue(res.getColumnLatestCell(Bytes.toBytes(family), Bytes.toBytes(qualifier)))).asInstanceOf[T]
            }
        }  else if (defaultValue.isInstanceOf[Short]) {
            if (res.containsColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier))) {
                outT = Bytes.toShort(CellUtil.cloneValue(res.getColumnLatestCell(Bytes.toBytes(family), Bytes.toBytes(qualifier)))).asInstanceOf[T]
            }
        } else if (defaultValue.isInstanceOf[Int]) {
            if (res.containsColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier))) {
                outT = Bytes.toInt(CellUtil.cloneValue(res.getColumnLatestCell(Bytes.toBytes(family), Bytes.toBytes(qualifier)))).asInstanceOf[T]
            }
        }else if (defaultValue.isInstanceOf[String]) {
            if (res.containsColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier))) {
                outT = Bytes.toString(CellUtil.cloneValue(res.getColumnLatestCell(Bytes.toBytes(family), Bytes.toBytes(qualifier)))).asInstanceOf[T]
            }
        } else { //默认返回byte[]
            if (res.containsColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier))) {
                outT = CellUtil.cloneValue(res.getColumnLatestCell(Bytes.toBytes(family), Bytes.toBytes(qualifier))).asInstanceOf[T]
            }
        }
        outT
    }
}

在通过获取一个Short类型的数据Caused by: java.lang.IllegalArgumentException: offset (0) + length (4) exceed the capacity of the array: 2 非常困惑, 本来就是获取一个Short类型值, 将该值赋值与一个Int变量,

        SparkUtil.readFromHbase("test", "181102", "181102~")
            .map(t => {
                var s: Int = -1
                s = HbaseUtil.getValueFromHbaseResult(t._2, "v", "s", HbaseUtil.defaultShort)  //该方法设置的就是Short类型
                (s, 1)
            })
            .reduceByKey(_ + _)
            .foreach(println)

在这里插入图片描述

看报错的具体内容, 本应该走请求Short的语句, 却请求了Int, 但实际该数值就是Short类型, 导致hbase报错类型不匹配。 怀疑在此处defaultShort被提升成了Int, 导致在getValueFromHbaseResult请求Int类型,

在这里插入图片描述

我尝试将在getValueFromHbaseResult后添加toInt, 发现问题解决

在这里插入图片描述

传入的defaultShort类型没有变化, 是Short 在这里插入图片描述

关注
打赏
1587549273
查看更多评论
立即登录/注册

微信扫码登录

0.1962s