java.lang.IllegalArgumentException:offset(0)+ length(8)超出数组的容量:4

时间:2017-06-09 06:10:20

标签: scala hbase

在运行spark作业时从Hbase读取数据时出现此错误。感谢。

这是代码

val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat],
      classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
      classOf[org.apache.hadoop.hbase.client.Result])

    hBaseRDD.count()

    // transform (ImmutableBytesWritable, Result) tuples into an RDD of Result’s
    val resultRDD = hBaseRDD.map(tuple => tuple._2)
    resultRDD.count()
    // transform into an RDD of (RowKey, ColumnValue)s  the RowKey has the time removed
    val keyValueRDD = resultRDD.map(result => (Bytes.toString(result.getRow()).split(" ")(0), Bytes.toDouble(result.value)))
    keyValueRDD.take(3).foreach(kv => println(kv))

    // group by rowkey , get statistics for column value
    val keyStatsRDD = keyValueRDD.groupByKey().mapValues(list => StatCounter(list))
    keyStatsRDD.take(5).foreach(println)

2 个答案:

答案 0 :(得分:0)

我遇到这个问题大多数时候是类型错误。请仔细检查您的数据类型。

答案 1 :(得分:0)

此错误的一个选项是存储Int(4B)并尝试将其读取为Double或Long(8B)。

E.g。

// Write an Int
table.put(
    new Put(Bytes.toBytes(key))
        .addColumn(
            Bytes.toBytes("family"),
            Bytes.toBytes("qualifier"),
            Bytes.toBytes(1)))) // Write 1 instead of 1.0

// Attempt to read as a Double
val getResult = table.get(new Get(Bytes.toBytes(key)))
for (cell <- cells if (Bytes.toString(CellUtil.cloneQualifier(cell)) == "qualifier")) {

    val value = Bytes.toDouble(CellUtil.cloneValue(cell))
}

如果是这种情况,我所知道的可能的解决方案是:

  1. 在写入正确类型之前进行演员
  2. 在读取时,在转换为Double / Long之前检查值的Bytes.length,如果结果为4,则转换为Int而不是