1) insert
public static void insertData(String tableName) throws Exception {
System.out.println("start insert data ......");
// HTablePool pool = new HTablePool(configuration, 1000);
// HTable table = (HTable) pool.getTable(tableName);
// Put put = new Put("112233bbbcccc".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
// put.add("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列
// put.add("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列
// put.add("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列
//
// HTablePool pool = new HTablePool(configuration, 1000);
HTable table = new HTable(configuration, tableName);
Put put = new Put("123456".getBytes());
put.add("columnFamily1".getBytes(), "name".getBytes(), "kate".getBytes());
put.add("columnFamily1".getBytes(), "age".getBytes(), "1221".getBytes());
put.add("columnFamily2".getBytes(), "teacher".getBytes(), "T1".getBytes());
try {
// table.put(put);
// table.put(put);
table.checkAndPut(Bytes.toBytes("123456"), Bytes.toBytes("columnFamily1"), Bytes.toBytes("name"), Bytes.toBytes("kate1"), put);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end insert data ......");
}
checkAndPut()方法,是当后面column family,column name, column value和即将PUT的数据相同时,才会put进hbase,否则忽略这条即将插入的记录;
如果chekcout的rowkey和要put的rowkey不相同,则会抛异常,只有检查相同时,才会去check后面的参数;