hbase JavaAPI 基本操作 表格的增删改查

2 篇文章 0 订阅

HBase 常见API
1, admin 创建表 列出表 使表无效有效 添加表列族名 删除表列族名
Admin a = new conn.getAdmin();
2, HTableDescriptor 结构对象 包含表的名字及对应的表的列名
3,HColumnDescriptor 表列族结构对象 建立表或者为表添加列族
4,Table 用于连接单独的Hbase表 提供对表数据的增删改查方法
5,put 用于对单个行执行添加操作
6,Get 获取单个行的相关信息
7,Scan 用于获取整个表给的数据或指定区域的数据
8,Resilt 存储Get /Scan操作后过去的单行值

  • 创建连接
//创建连接
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://ip:端口/hbase");
Connection conn = ConnectionFactory.createConnection(conf);
  • 创建表格
//创建表格
Admin admin = conn.getAdmin();
TableName tableName = TableName.valueOf("student");
//创建列族
HTableDescriptor ht = new HTableDescriptor(tableName);
ht.addFamily(new HColumnDescriptor("f1"));
ht.addFamily(new HColumnDescriptor("info"));
byte[][] spiltbytes = new byte[][]{
"10".getBytes(),
"20".getBytes(),
"30".getBytes(),
"40".getBytes()
};
admin.createTable(ht,spiltbytes);
//创建职工表
Admin admin = conn.getAdmin();
TableName tableName = TableName.valueOf("emp");
HTableDescriptor ht = new HTableDescriptor(tableName);
ht.addFamily(new HColumnDescriptor("info"));
ht.addFamily(new HColumnDescriptor("money").setMaxVersions(12));
if (admin.tableExists(tableName)){
if (admin.isTableEnabled(tableName)){
admin.disableTable(tableName);
}
admin.deleteTable(tableName);
}
admin.createTable(ht);
  • 删除列族/添加列族
//Student表中删除f1列族,添加score列族
Admin admin = conn.getAdmin();
TableName tableName = TableName.valueOf("student");
if (admin.tableExists(tableName)) {
if (admin.isTableEnabled(tableName)) {
admin.disableTable(tableName);
}
HTableDescriptor ht = admin.getTableDescriptor(tableName);
ht.removeFamily("f1".getBytes());
ht.addFamily(new HColumnDescriptor("score"));
admin.modifyTable(tableName, ht);
admin.enableTable(tableName);
}
  • 插入数据
//插入数据
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(tableName);
Put put1 = new Put("95001".getBytes());
put1.addColumn("info".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put1.addColumn("info".getBytes(), "age".getBytes(), "20".getBytes());
put1.addColumn("score".getBytes(), "math".getBytes(), "80".getBytes());
put1.addColumn("score".getBytes(), "english".getBytes(), "90".getBytes());
table.put(put1);
Put put2 = new Put("95002".getBytes());
put2.addColumn("info".getBytes(), "name".getBytes(), "lisi".getBytes());
put2.addColumn("info".getBytes(), "age".getBytes(), "18".getBytes());
put2.addColumn("score".getBytes(), "math".getBytes(), "75".getBytes());
put2.addColumn("score".getBytes(), "art".getBytes(), "90".getBytes());
table.put(put2);
//导入emp.txt中的数据
TableName tableName = TableName.valueOf("emp");
String file = "D:/下载文件堆放区/emp.txt";
Table table = conn.getTable(tableName);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
String[] cols = new String[]{
       "info:ename",
       "info:job",
       "info:mgr",
       "info:hiredate",
       "money:sal",
       "money:credit",
       "info:depton"
};
Put put;
while ((line = br.readLine()) != null) {
   String[] lines = line.split(",");
   put = new Put(lines[0].getBytes());
   for (int i = 1;i<lines.length;i++){
       put.addColumn(cols[i-1].split(":")[0].getBytes(),cols[i-1].split(":")[1].getBytes(),lines[i].getBytes());
   }
   table.put(put);
   }
  • 查询数据
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(tableName);
Get get1 = new Get("95001".getBytes());
Result result = table.get(get1);
for (Cell cell : result.rawCells()){
System.out.println(new String(CellUtil.cloneRow(cell))+"\t"
+new String(CellUtil.cloneFamily(cell)) +"\t"
+new String(CellUtil.cloneQualifier(cell))+"\t"
+new String(CellUtil.cloneValue(cell))
);
}
//        查找student表中学号大于95001的学生信息
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(tableName);
Scan scan = new Scan();
BinaryComparator binaryComparator = new BinaryComparator("95001".getBytes());
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER, binaryComparator);
scan.setFilter(rowFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(new String(CellUtil.cloneRow(cell))+"\t"
+new String(CellUtil.cloneFamily(cell)) +"\t"
+new String(CellUtil.cloneQualifier(cell))+"\t"
+new String(CellUtil.cloneValue(cell))
);
}
}
//        查找student表中成绩大于80的信息
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(tableName);
Scan scan = new Scan();
BinaryComparator comparator = new BinaryComparator("80".getBytes());
Filter filter = new ValueFilter(CompareFilter.CompareOp.GREATER,comparator);
scan.addFamily("score".getBytes());
scan.setFilter(filter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
System.out.println(new String(CellUtil.cloneRow(cell))+"\t"
+new String(CellUtil.cloneFamily(cell)) +"\t"
+new String(CellUtil.cloneQualifier(cell))+"\t"
+new String(CellUtil.cloneValue(cell))
);
}
}
  • 查看表格
//查看所有的表
Admin admin = conn.getAdmin();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
  • 删除数据
//        删除表中95001的数据
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(tableName);
Delete delete = new Delete("95001".getBytes());
table.delete(delete);
//删除student表中学号为95001学生的成绩
TableName tableName = TableName.valueOf("student");
Table table = conn.getTable(tableName);
Delete delete = new Delete("95001".getBytes());
delete.addFamily("score".getBytes());
table.delete(delete);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值