hbase学习-- 5 使用HBase客户端API读取,删除数据,扫描数据
2017-11-03 14:44
525 查看
1 读取数据
使用HTable类中的get()方法。这种方法需要Get类的一个实例。
data"))方法,
要得到一个特定的列族的特定列,使用
get.addColumn(Bytes.toBytes("professional data"),Bytes.toBytes("name"))方法,
如果不采用高亮部分的代码的话,使用hTable.get(get)方法得到的结果
result 默认获取表中行号为 1 的所有数据。
2 删除数据
使用HTable类的delete()方法删除HBase表数据
如果不选择要删除的数据的话,默认删除表中 行号为 1 的所有数据
3 扫描数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class ScanTable {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable hTable = new HTable(conf, "People");
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes("personal data"));
ResultScanner scanner = hTable.getScanner(scan);
for (Result result = scanner.next(); result != null; result = scanner.next()) {
System.out.println(result);
}
scanner.close();
System.out.println("scanner closed");
}
}
输出结果:
keyvalues={1/personal data:name/1509697491235/Put/vlen=3/seqid=0}
keyvalues={2/personal data:name/1509697491247/Put/vlen=3/seqid=0}
scanner closed
使用HTable类中的get()方法。这种方法需要Get类的一个实例。
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class ReadTable { public static void main(String[] args) throws IOException { //实例化Configuration类 Configuration conf = HBaseConfiguration.create(); //实例化HTable类 HTable hTable = new HTable(conf, "People"); //实例化获得类 Get get = new Get(Bytes.toBytes("1"));
//选择获取的数据
get.addFamily(Bytes.toBytes("professional data")); // get.addColumn(Bytes.toBytes("personal data"),Bytes.toBytes("name")); get.addColumn(Bytes.toBytes("personal data"),Bytes.toBytes("age"));
//获取结果 Result result = hTable.get(get); byte[] name = result.getValue(Bytes.toBytes("personal data"), Bytes.toBytes("name")); byte[] age = result.getValue(Bytes.toBytes("personal data"), Bytes.toBytes("age")); System.out.println("name: " + Bytes.toString(name) + " age: " + Bytes.toString(age)); } }注意代码中间高亮的部分为选择获取的数据:要得到一个特定的列族的所有列,使用 get.addFamily(Bytes.toBytes("professional
data"))方法,
要得到一个特定的列族的特定列,使用
get.addColumn(Bytes.toBytes("professional data"),Bytes.toBytes("name"))方法,
如果不采用高亮部分的代码的话,使用hTable.get(get)方法得到的结果
result 默认获取表中行号为 1 的所有数据。
2 删除数据
使用HTable类的delete()方法删除HBase表数据
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class DeleteTable { public static void main(String[] args) throws IOException { //实例化HBaseConfiguration Configuration conf = HBaseConfiguration.create(); //实例化HTable HTable hTable = new HTable(conf,"People"); //实例化Delete Delete delete = new Delete(Bytes.toBytes("1")); //选择要删除的数据 delete.addFamily(Bytes.toBytes("professional data")); delete.addColumn(Bytes.toBytes("personal data"),Bytes.toBytes("name")); //删除数据 hTable.delete(delete); hTable.close(); System.out.println("data deleted"); } }
如果不选择要删除的数据的话,默认删除表中 行号为 1 的所有数据
3 扫描数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class ScanTable {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable hTable = new HTable(conf, "People");
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes("personal data"));
ResultScanner scanner = hTable.getScanner(scan);
for (Result result = scanner.next(); result != null; result = scanner.next()) {
System.out.println(result);
}
scanner.close();
System.out.println("scanner closed");
}
}
输出结果:
keyvalues={1/personal data:name/1509697491235/Put/vlen=3/seqid=0}
keyvalues={2/personal data:name/1509697491247/Put/vlen=3/seqid=0}
scanner closed
相关文章推荐
- HBase 学习之一 <<HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行>>
- hbase学习-- 4 使用HBase客户端API 创建和更新数据
- ZooKeeper之Java客户端API使用—读取数据。
- 2018-08-19期 Hbase客户端API操作( 删除数据)
- 使用Hive读取Hbase中的数据
- JPA学习笔记---JPA数据的操作:增加,删除,修改,获取,使用JPQL进行查询
- ZendFramework学习第三章(核心组件—使用配置数据之从ini配置文件中读取数据)
- HBase客户端Java API使用总结
- ZendFramework学习第三章(核心组件—使用配置数据之从php配置文件中读取数据)
- 使用hive读取hbase数据
- SMP3.0学习笔记之十二 使用AppBuilder读取OData服务的数据
- Java统计学习之道:HBase在数据统计应用中的使用心得
- 使用Hive读取Hbase中的数据
- hive 0.12 使用hbase读取数据的函数调用栈
- 使用客户端对象模型读取SharePoint列表数据
- ZendFramework学习第三章(核心组件—使用配置数据之从php配置文件中读取数据)
- 使用hive读取hbase数据
- 使用Hive读取Hbase中的数据
- Hbase 学习(十一)使用hive往hbase当中导入数据
- 官方NotePad实例学习--读取数据Cursor的使用