**一个result代表一条数据,一个row_key 使用listcell获取该条数据所有cell,拿到每一个cell,遍历cell后数据是一列一列的cell,按列一个个取cell
for (Result result : scanner) {
//获取所有的小cell,每一列
List| cells = result.listCells();**
|
1、scan获取所有数据
@Test
public void getScan()throws Exception{
Table table = connection.getTable(TableName.valueOf("students"));
//创建一个scan对象,设置限制条件
Scan scan = new Scan();
scan.withStartRow("1500100800".getBytes());
scan.withStopRow("1500100810".getBytes());
scan.setLimit(5);
//获取所有数据,传入一个scan对象
ResultScanner scanner = table.getScanner(scan);
//获取一条条数据
for (Result result : scanner) {
String row = Bytes.toString(result.getRow());
String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes()));
String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));
String string = row+"t"+name+"t"+age+"t"+gender+"t"+clazz;
System.out.println(string);
}
connection.close();
2、使用IO流批量写入数据
@Test
public void putAll() throws Exception {
//创建一张students表
Admin admin = connection.getAdmin();
if (!admin.tableExists(TableName.valueOf("students"))) {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("students"));
HColumnDescriptor info = new HColumnDescriptor("info");
tableDescriptor.addFamily(info);
admin.createTable(tableDescriptor);
}
Table table = connection.getTable(TableName.valueOf("students"));
//使用list集合批处理快一些
ArrayList list = new ArrayList();
//IO流读写数据
BufferedReader br = new BufferedReader(
new FileReader("D:\Program Files\IDEA\IdeaProject\shujiabigdata\data\students.txt"));
String line;
int batche=10;
while ((line = br.readLine()) != null) {
String[] split = line.split(",");
String id = split[0];
String name = split[1];
String age = split[2];
String gender = split[3];
String clazz = split[4];
Put put = new Put(id.getBytes());
put.addColumn("info".getBytes(), "name".getBytes(), name.getBytes());
put.addColumn("info".getBytes(), "age".getBytes(), age.getBytes());
put.addColumn("info".getBytes(), "gender".getBytes(), gender.getBytes());
put.addColumn("info".getBytes(), "clazz".getBytes(), clazz.getBytes());
list.add(put);
//10条数据写一次
if (list.size()==batche){
table.put(list);
list=new ArrayList();
}
}
if (list.size()!=0){
table.put(list);
}
connection.close();
}
3、可能出现的问题,有一条数据不规整
适用数据结构不唯一的情况,获取每一个cell
001,sex:asd
输出可能会出现空值
//获取所有数据,传入一个scan对象
ResultScanner scanner = table.getScanner(scan);
//获取一条条数据
for (Result result : scanner) {
//获取所有的小cell,每一列
List| cells = result.listCells();
//获取row key
String row_key = Bytes.toString(result.getRow());
System.out.print(row_key+"t");
//遍历,得到每一个cell
for (Cell cell : cells) {
//获取每一个cell的值
String string = Bytes.toString(CellUtil.clonevalue(cell));
//获取每一个cell的字段名
String que = Bytes.toString(CellUtil.cloneQualifier(cell));
if ("age".equals(que)){
if (Integer.parseInt(string)>=20){
string="成年";
}
}
System.out.print(string+"t");
}
System.out.println();
}
connection.close();
|
4、获取多版本数据
设置get.setMaxVersions(3);
结果集需要使用list得到
Result result = table.get(get);
List cells = result.listCells();
@Test
public void getGet()throws Exception{
Get get = new Get("47BE1E866CFC071DB19D5E1C056BE28AE24C16E7".getBytes());
get.setMaxVersions(3);
Result result = table.get(get);
List cells = result.listCells();
ArrayList jingduList = new ArrayList();
ArrayList weiduList = new ArrayList();
byte[] row = result.getRow();
String rows = Bytes.toString(row);
for (Cell cell : cells) {
//获取值
byte[] bytes = CellUtil.clonevalue(cell);
String value = Bytes.toString(bytes);
//获取列名
byte[] bytes1 = CellUtil.cloneQualifier(cell);
String que = Bytes.toString(bytes1);
if ("jingdu".equals(que)){
jingduList.add(value);
}else {
weiduList.add(value);
}
}
for (int i = 0; i < 3; i++) {
System.out.println(rows+"t"+jingduList.get(i)+"t"+weiduList.get(i));
}
}
| 


