本篇文章主要记录Hadoop分布式数据库HBase相关操作,记录了相关操作的Shell指令和通过API进行操作的相关代码,便于日后查找。
Shell指令:
启动HDFS集群:start-dfs.sh
停止HDFS集群:stop-dfs.sh
启动HBase集群:start-hbase.sh
停止HBase集群:stop-hbase.sh
进入HBase命令行:bin/hbase shell (hbase目录下的bin目录)
退出shell:exit
Shell指令: CREATE 表名,列族1,列族2,…
例:create ‘student’,‘college’,‘profile’
API操作代码:
注意不要导错包!!!
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/*
记录使用API创建表操作
*/
public class CreateTable {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
//创建命名空间
hBaseAdmin.createNamespace(NamespaceDescriptor.create("my_ns").build());
//表名:student
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("my_ns:student"));
//添加列族->转化为字节数组
HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes("college"));
hTableDescriptor.addFamily(hcd);
hcd = new HColumnDescriptor(Bytes.toBytes("profile"));
hTableDescriptor.addFamily(hcd);
//创建表
hBaseAdmin.createTable(hTableDescriptor);
hBaseAdmin.close();
}
}
首先禁用表,再删除
Shell指令:
DISABLE 表名
DROP 表名
例:
disable ‘student’
drop ‘student’
API操作代码:
注意不要导错包!!!
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import java.io.IOException;
/*
记录删除表操作
*/
public class DeleteTable {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
if(hBaseAdmin.tableExists(tableName)){
//先禁用表
hBaseAdmin.disableTable(tableName);
//再删除表
hBaseAdmin.deleteTable(tableName);
}
}
}
首先设置表为不可用状态,再进行操作,最后设置为可用状态
Shell指令:
设置表为不可用状态: disable ‘student’
添加列族‘info’ 操作: alter ‘student’,name=>‘info’,versions=>5
删除列族‘info’操作: alter ‘student’,name=>‘info’,method=>‘delete’
启用表:enable ‘student’
Shell指令: describe ‘student’
Shell指令: list
Shell指令: exists ‘student’
Shell指令: is_enabled ‘student’
Shell指令: count ‘student’
Shell指令:
PUT 表名,行键,列族:列名,值
例:
put ‘sudent’,‘19052002’,‘profile:height’,‘173’
API操作代码:
注意不要导错包!!!
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class PutSingleRow {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
HTable table = new HTable(conf, tableName);
//创建新put示例,由行键绑定,表示一个学生
Put put = new Put(Bytes.toBytes("19052002"));
//添加一个单元值,分别为列族,列名,值
put.add(Bytes.toBytes("college"),Bytes.toBytes("school"),Bytes.toBytes("Big Data"));
put.add(Bytes.toBytes("profile"),Bytes.toBytes("name"),Bytes.toBytes("zhaosi"));
table.put(put);
table.close();
}
}
API操作代码:
注意不要导错包!!!
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PutRows {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
HTable table = new HTable(conf, tableName);
List<Put> listPuts = new ArrayList<Put>();
//第一个对象
Put put1 = new Put(Bytes.toBytes("19052002"));
//添加一个单元值,分别为列族,列名,值
put1.add(Bytes.toBytes("college"),Bytes.toBytes("school"),Bytes.toBytes("Big Data"));
listPuts.add(put1);
//第二个对象
Put put2 = new Put(Bytes.toBytes("19052003"));
//添加一个单元值,分别为列族,列名,值
put2.add(Bytes.toBytes("college"),Bytes.toBytes("school"),Bytes.toBytes("Big Data"));
listPuts.add(put2);
//直接将list中的对象一次插入表中
table.put(listPuts);
table.close();
}
}
Shell指令:
delete 表名,行键,列族:列名
例:
delete ‘student’,‘19052006’,‘profile:height’
API操作代码:
注意不要导错包!!!
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.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class DeleteSingleRow {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
HTable table = new HTable(conf, tableName);
//指定行键删除
Delete delete = new Delete(Bytes.toBytes("19052002"));
table.delete(delete);
table.close();
}
}
Shell指令:
truncate 表名
例:
truncate ‘student’
Shell指令:
put 表名,行键,列族:列名,新的值
例:
put ‘stuednt’,19052006’,‘profile:weight’,‘135’
API操作代码:
注意不要导错包!!!
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class UpdateRow {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
HTable table = new HTable(conf, tableName);
//指定行键
Put put = new Put(Bytes.toBytes("19952003"));
//列族,列名,新的值
put.addColumn(Bytes.toBytes("college"), Bytes.toBytes("school"),Bytes.toBytes("Math"));
put.addColumn(Bytes.toBytes("profile"), Bytes.toBytes("height"),Bytes.toBytes("182"));
table.put(put);
table.close();
}
}
Shell指令:
获取一个对象的数据:get 表名,行键
例:get ‘student’,‘19052003’
获取某行数据一个列族的所有数据:get 表名,行键,列族
例:get ‘student’,‘19052003’,‘profile’
获取某行数据一个列族中一个列的所有数据:get 表名,行键,列族:列名
例:get ‘student’,‘19052003’,‘profile:name’
API操作代码:
注意不要导错包!!!
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.HBaseAdmin;
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 GetRow {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
HTable table = new HTable(conf, tableName);
//获取指定行键的数据
Get get = new Get(Bytes.toBytes("19052003"));
Result rs = table.get(get);
//获取指定列族和列
byte[] value = rs.getValue("profile".getBytes(), "name".getBytes());
System.out.println("student: "+"19052003 "+"name "+Bytes.toString(value));
table.close();
}
}
注意不要导错包!!!
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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 GetRows {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
HTable table = new HTable(conf, tableName);
//获取指定行键的数据
Get get = new Get(Bytes.toBytes("19052003"));
Result rs = table.get(get);
for (Cell cell : rs.rawCells()) {
System.out.println("列族:"+new String(CellUtil.cloneFamily(cell)));
System.out.println("列:"+new String(CellUtil.cloneQualifier(cell)));
System.out.println("值:"+new String(CellUtil.cloneValue(cell)));
System.out.println("时间戳:"+cell.getTimestamp());
}
table.close();
}
}
API操作代码:
注意不要导错包!!!
import org.apache.commons.lang.math.NumberUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class GetRowsByTimeStamp {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
//指定时间戳范围
scan.setTimeRange(NumberUtils.toLong("141387822100"), NumberUtils.toLong("1418627712342"));
ResultScanner scanner = table.getScanner(scan);
//得到指定时间戳范围内的若干行
for (Result result : scanner) {
Cell[] cells = result.rawCells();
int i = 0;
int len = cells.length;
//按行遍历
System.out.println("行键:"+ Bytes.toString(CellUtil.cloneRow(cells[i])));
for(;i<len;i++){
System.out.println("列族:"+new String(CellUtil.cloneFamily(cells[i])));
System.out.println("列:"+new String(CellUtil.cloneQualifier(cells[i])));
System.out.println("值:"+new String(CellUtil.cloneValue(cells[i])));
System.out.println("时间戳:"+cells[i].getTimestamp());
}
System.out.println();
}
table.close();
}
}
Shell指令:
全表扫描:scan 表名
API操作代码:
注意不要导错包!!!
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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 GetRows {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
//分布式场景下设置ZooKeeper地址
conf.set("hbase.ZooKeeper.quorum","master,slave1,slave2");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
String tableName = "student";
HTable table = new HTable(conf, tableName);
//获取指定行键的数据
Get get = new Get(Bytes.toBytes("19052003"));
Result rs = table.get(get);
for (Cell cell : rs.rawCells()) {
System.out.println("列族:"+new String(CellUtil.cloneFamily(cell)));
System.out.println("列:"+new String(CellUtil.cloneQualifier(cell)));
System.out.println("值:"+new String(CellUtil.cloneValue(cell)));
System.out.println("时间戳:"+cell.getTimestamp());
}
table.close();
}
}
以上就是HBase的相关指令操作。
参考《Hadoop大数据原理与运用》徐鲁辉