Home  >  Article  >  Database  >  HBase在处理中文字符串时的问题

HBase在处理中文字符串时的问题

WBOY
WBOYOriginal
2016-06-07 17:27:21996browse

文中可能涉及到的API: Hadoop/HDFS:http://hadoop.apache.org/common/docs/current/api/ HBase: http://hbase.apache.org/apido

文中可能涉及到的API:

Hadoop/HDFS:

HBase: ?overview-summary.html

Begin!

 

在设置scan的startRowKey与endRowKey时,经常需要在某个条件字符串后面补充出一个范围。(再比如SingleColumnValueFilter也会用到)

比如:我的条件字符串是“abc”,scan时我需要将下述内容都囊括到我scan的范围内。

abc123

abcdabc

abccca

....

这时候我startRowKey使用“abc”即可,,上述字符串按字典序都比“abc”要大,“abc”串c之后的值是0嘛~

而endRowKey最初我使用了“abc~”,因为我查ASCII码表时‘~’是倒数第二个,值为127,足够大,肯定大于上述串中的1、d、c等字符。

这样做,在处理英文数据时就足够了,系统运行正常。

但当我处理中文数据时,中文一般都是以UTF-8格式处理的,一个汉字表示出来类似“0xe6,0xc2,0xe1”。0xe6大于127。所以使用‘~’遇到中文必然悲催。

我的解决方法:

使用UltraEdit,进入十六进制编辑模式,将值改为FF。然后回到文本模式,将刚才的字符复制下来。这个字符应该是一个不可显示的字符,看着好像两个空格的长度。

然后在设置endRowKey时

new String(name + " "); //这里只是示例,引号间就是刚才复制的那个字符。将这个字符串作为endRowKey,果然所有的中文字符就囊括在内了。

另外一定要注意:使用HBase API时不要使用str.getBytes将String转化为byte[] ,而应该使用Bytes.toBytes(str);同样使用Bytes.toString(bytes);完成逆向转换。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn