运用Java的API存储汉字到hbase中,代码如下:
public static void insterRow(String tableName,String rowkey,String colFamily,String col,String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put);
//批量插入
/* List<Put> putList = new ArrayList<Put>();
puts.add(put);
table.put(putList);*/
table.close();
close();
}
参数直接穿得就是"参数"这种格式的
然后从Hbase中读取数据代码如下:
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addFamily(Bytes.toBytes(colFamily));
get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
Result result = table.get(get);
Cell[] cells = result.rawCells();
String str ="NULL";
String str1 ="NULL";
if(cells.length > 0)
str = new String(CellUtil.cloneValue(cells[0]));
// Bytes.toString(CellUtil.cloneValue(cells[0])+" ");
// CellUtil.cloneValue(cells[0]).toString();
showCell(result);
str1 = new String(str.getBytes("gbk"), "utf-8");
table.close();
close();
System.out.println( Bytes.toString(Bytes.toBytes(str)));
System.out.println(str1);
return str1;
获取的这个str1中控制台打印有一部分汉字是乱码有一部分是??
|
|