1. GET请求用来根据行键从表中获取一行数据
因为HBase行键即索引,所以GET的性能很快。GET也可以指定需要获取数据的列族、列、时间范围和版本号等。
关键一句:Get get = new Get(Bytes.toBytes(“1001”));
public class GetValue {public static void main(String[] args) {Configuration configuration = HBaseConfiguration.create();configuration.set("hbase.zookeeper.quorum", "121.37.70.214");configuration.set("hbase.master", "121.37.70.214:16010");configuration.set("hbase.zookeeper.property.clientPort", "2181");try {Connection connection = ConnectionFactory.createConnection(configuration);Table table = connection.getTable(TableName.valueOf("bigdata:stu"));Get get = new Get(Bytes.toBytes("1001"));Result result = table.get(get);byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));byte[] value1 = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));String name = Bytes.toString(value);String age = Bytes.toString(value1);System.out.println("name:" + name + ", age:" + age);table.close();connection.close();} catch (IOException e) {e.printStackTrace();}}}
2. Delete用来删除数据
可以删除整行、列族、列以及某个时间戳之前的所有版本的数据,使用简单。
关键一句:Delete delete = new Delete(Bytes.toBytes(“1001”));
public class DeleteValue {
public static void main(String[] args) {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "121.37.70.214");
configuration.set("hbase.master", "121.37.70.214:16010");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
try {
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf("bigdata:stu"));
Delete delete = new Delete(Bytes.toBytes("1001"));
delete.addFamily(Bytes.toBytes("info1"));
table.delete(delete);
System.out.println("data had been deleted...");
table.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. Put用来向表中插入数据
关键一句:Put put = new Put(Bytes.toBytes(“1001”));
public class PutValue {
public static void main(String[] args) {
// 获取配置文件
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "121.37.70.214");
config.set("hbase.master", "121.37.70.214:16010");
config.set("hbase.zookeeper.property.clientPort", "2181");
try {
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("bigdata:stu"));
// 插入数据
Put put = new Put(Bytes.toBytes("1001"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("21"));
table.put(put);
System.out.println("Data has been updated!");
table.close();
admin.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. Scan读取多行数据
Scan与Get都可以用来查询数据,区别是Scan可以用来读取多行数据,Scan结果的遍历类似于关系型数据库的游标。
关键一句:Scan scan = new Scan();
public class ScanTable {
public static void main(String[] args) {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "112.124.0.168");
configuration.set("hbase.master", "112.124.0.168:16010");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
try {
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf("bigdata:test"));
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes("info1"));
// Getting the scan result
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
Cell[] cells = result.rawCells();
for (Cell cell: cells){
// get RowKey
System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
results.close();
table.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
