【问题标题】:How to handle newline character in HIVE on HBase?如何在 HBase 上的 HIVE 中处理换行符?
【发布时间】:2015-09-21 06:56:54
【问题描述】:
我正在从我的 java 程序将数据插入到hbase。由于我们需要将所有内容转换为字节数组以插入到 hbase 中,所以我正在这样做。但是当我的输入字符串中有任何换行符时,它会在 hbase 中存储十六进制值(例如:我尝试插入字符串 "prasad\r\nchowdary" 但在 hbase 中它就像 prasad\x0D\x0Achowdary)。
我的问题是当 hbase 中的数据是这样的时候,当我尝试从 hive 查询这个表时,我的 jdbc 结果集对单行重复了两次。
那么如何避免在插入hbase时将\r\n转换为十六进制。
【问题讨论】:
标签:
java
hbase
apache-hive
【解决方案1】:
如果您想在字符串中添加换行符或任何其他字符集,请修改它们,以便 java 将它们视为字符串。
将“prasad\r\nchowdary”转换为“prasad\r\nchowdary”
这只是在换行之前使用转义字符“\”然后将其转换为字节。它应该看起来像。
String name = "prasad\\r\\nchowdary";
put.add(Bytes.toBytes("family"),Bytes.toBytes("qualifier"),Bytes.toBytes("name"));
【解决方案2】:
String str = "prasad\r\nchowdary";
str = StringEscapeUtils.escapeJava(str);
Put p = new Put(Bytes.toBytes(str));
JSONObject json = new JSONObject(p.toJSON());
System.out.println(StringEscapeUtils.unescapeJava(json.getString("row")));