gpt4 book ai didi

java - scala/spark —如何使用指定的任意字节数组创建字符串?

转载 作者:行者123 更新时间:2023-12-02 21:04:09 25 4
gpt4 key购买 nike

使用spark访问HBase时,需要在HBaseConfiguration中指定正确的扫描范围,然后使用HBaseConfiguration创建RDD。如下所示:

val conf = HBaseConfiguration.create()
conf.set(TableInputFormat.INPUT_TABLE, inputTable)
conf.set(TableInputFormat.SCAN_ROW_START, start_row_string)
conf.set(TableInputFormat.SCAN_ROW_STOP, end_row_string)
val hBaseRDD = sc.newAPIHadoopRDD(
conf,
classOf[TableInputFormat],
classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
classOf[org.apache.hadoop.hbase.client.Result]
)

之后,我可以以任何方式操作hBaseRDD。
但是代码中的start_row_string必须是字符串会导致问题。
在HBase中,我的行键是由一个以Int开头的字节数组创建的,也就是说:
 val row_key = byte array of Int ++ arbitrary byte array

在创建row_key字节数组并将行键转换为字符串以传递到上述HBaseConfiguration之后,我发现我错了:
val row_key_string = new String(row_key)

因为row_key_string.getBytes不等于row_key.so,所以HBase没有获得正确的开始行键,并且HBase返回了错误的数据。
例:
val arr = Array(0,0,15,-77)  //which is the array byte of 4018
val str = new String(arr)
arr.getBytes //return Array(0, 0, 15, -17, -65, -67)
arr.getBytes("UTF-16BE") //return Array(0,0,15,-77)

arr.getBytes(“UTF-16BE”)返回正确的答案。因为spark调用了getBytes方法,我无法指定getBytes的字符集。

如果我不能解决问题。我必须放弃NewAPIHadoopRDD。我可以在每个执行器中建立一个连接,然后使用Scan通过字节数组指定HBase Client提供的开始行键,但这很丑。

最佳答案

我已经通过使用TableInputFormat.SCAN解决了我的问题,它是BASE64字符串。
并且将任意字节数组传输到字符串是错误的,因为它的行为不受控制。

关于java - scala/spark —如何使用指定的任意字节数组创建字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42445075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com