中文 乱码
如果是中文,在 hbase shell 显示乱码
解决
执行 scan 时 增加 {FORMATTER => 'toString'},如下:
scan 'orders',{FORMATTER => 'toString'}
注意: 大小写必须同上面
数值型 乱码
如果是通过 程序 向hbase中保存 数值型 数据,在 hbase shell 显示乱码,通过 程序 读取数据时也是乱码
注意
在 hbase 中,一般都转成字符串,然后再保存
类似 价格、年龄 这类数字,前面补 0,转成定长的,再保存,这样便于过滤
java代码
public void put() throws IOException {
//获取table对象
Table table = conn.getTable(TableName.valueOf("orders"));
//创建put对象,对应put命令,封装rowkey、列族、列名、列值
Put put = new Put(Bytes.toBytes("1000"));
put.addColumn(Bytes.toBytes("c1"),Bytes.toBytes("name"),Bytes.toBytes("java从入门到放弃"));
put.addColumn(Bytes.toBytes("c1"),Bytes.toBytes("price"),Bytes.toBytes(87.5));
//执行put命令
table.put(put);
//关闭table
table.close();
}
查看数据
scan 'orders'
执行结果如下:
通过 java api 保存 数值型 数据,在 hbase shell 中显示是乱码
解决
通过程序读取时,做转换
public void get() throws IOException {
//获取table对象
Table table = conn.getTable(TableName.valueOf("orders"));
//根据rowkey构建Get对象
Get get = new Get(Bytes.toBytes("1000"));
//执行get方法
Result result=table.get(get);
//获取rowkey
String rowkey=Bytes.toString(result.getRow());
System.out.println(rowkey);
//获取所有单元格
List<Cell> list=result.listCells();
for(Cell cell:list){
String family=Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
String qualifire=Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
// String value=Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
Object value=null;
if("price".equals(qualifire)){
//转成double类型
value = Bytes.toDouble(cell.getValueArray());
}else {
value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
}
System.out.println(family+":"+qualifire+":"+value);
}
table.close();
}
能够正常显示,但显示成 科学计数法,price值为 4.8805903194E-313
hbase shell中保存数字
在 hbase shell 环境下,保存 数值型 数据,会默认转成 字符串
