1 启动hadoop:start-all.sh(/usr/local/hadoop下)
2 启动hbase:bin/start-hbase.sh(/usr/local/hadoop/hbase下)
3 启动shell:bin/hbase shell(/usr/local/hadoop/hbase下)
4 关闭shell:exit
5 关闭hbase:bin/stop-hbase.sh
6 关闭hadoop:stop-all.sh
- 启动eclipse:./eclipse(/usr/local/eclipse下)
- 使用hdfs:hdfs dfs -
- 使用mapreduce:保证hdfs dfs -ls(hdfs文件系统的/user/hadoop)下面没有output
一、shell
1 create创建学生表:create 'student','S_No','S_Name','S_Sex','S_Age'
2 put插入一行数据:
put 'students', '001', 'basic_info:name', 'Alice' (列组+列情况)
put 'student','r001','S_No','2015001'
put 'student','r001','S_Name','Zhangsan'
put 'student','r001','S_Sex','male'
put 'student','r001','S_Age','23'
[basic_info:name, S_No, S_Name, S_Sex]就组成了fields,所以如果fields某一项无:,那它就只是一个列族,如果有:则是一个列族+列的情况
3 get查看某表某一行数据:get 'student','r001'(r001为一个行键)
4 scan查看某表全部数据:scan 'student'
二、java API
1 init()建立连接
** Admin
接口主要用于执行HBase集群的管理操作,如创建表、删除表、修改表结构、获取表描述符等。这些操作通常涉及到HBase元数据的变化,比如修改表的schema信息。因此,Admin
接口提供了管理HBase集群状态所需的方法和工具。
** Connection
接口则用于执行数据操作,如读取、写入数据等。当你需要获取一个Table
对象以进行数据访问时,你会通过Connection
接口来实现。这是因为Table
对象封装了与HBase表进行交互所需的所有数据操作方法,如get
、put
、scan
等。
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
2 close()关闭连接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
3 createTable()创建表
public static void createTable(String myTableName,String[] colFamily) throws IOException {
TableName tableName = TableName.valueOf(myTableName);
//如果存在表,则输出存在表
if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
//否则创建,TableDescriptorBuilder 的对象根据表名new,设置列族
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
for(String str:colFamily){
ColumnFamilyDescriptor family =
ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
tableDescriptor.setColumnFamily(family);
}
//admin对象创建
admin.createTable(tableDescriptor.build());
}
}
4 insertData()
插入一行数据
(1)单个单元格数据插入:
public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
//行键
Put put = new Put(rowKey.getBytes());
//列族,列,值
put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());
table.put(put);
table.close();
}
(2)一行数据插入:
public static void addRecord(String tableName,String rowKey,String[] fields,String[] values) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
//fields为列族,形式为[列族名:列名, 列族名:列名, 列族名:列名]
//遍历列族
for (int i = 0; i < fields.length; i++) {
//根据行键确定一个行
Put put = new Put(rowKey.getBytes());
String [] cols = fields[i].split(":");
//只有列族的情况,如sno,sage
if(cols.length==1){
//列族,列,内容,类似put 'students', '001', 'name', 'Alice'
put.addColumn(cols[0].getBytes(), "".getBytes(), values[i].getBytes());
}
else {
//列族,列,内容,类似put 'students', '001', 'info:name', 'Alice'
put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
}
table.put(put);
}
table.close();
}
5 getData()
(1)获取一个具体单元格的数据
public static void getData(String tableName,String rowKey,String colFamily, String col)throws IOException{
Table table = connection.getTable(TableName.valueOf(tableName));
//行键给get
Get get = new Get(rowKey.getBytes());
//列族,列给get
get.addColumn(colFamily.getBytes(),col.getBytes());
//获取到表、行键、列族、列下单元格内容
Result result = table.get(get);
//如果该内容为null(或者说该列为null),则输出null,否则输出内容
System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
table.close();
}
(1)获取一列或一个列族的数据
//查看数据
public static void getData(String tableName, String column)throws IOException{
Table table = connection.getTable(TableName.valueOf(tableName));
// 扫描整个表
Scan scan = new Scan();
try (ResultScanner scanner = table.getScanner(scan)) {
for (Result result : scanner) {
byte[] rowKey = result.getRow();
String rowKeyString = Bytes.toString(rowKey);
// 检查是否包含特定的列数据
if (column.contains(":")) {
// 如果column是一个具体的列名(包括列族和列限定符)
String[] parts = column.split(":");
String columnFamily = parts[0];
String columnQualifier = parts[1];
Cell cell = result.getColumnLatestCell(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier));
if (cell != null) {
// 列存在,打印值
System.out.println("Row key: " + rowKeyString + ", Column: " + column + ", Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
} else {
// 列不存在,打印null
System.out.println("Row key: " + rowKeyString + ", Column: " + column + " does not exist. Value: null");
}
} else {
// 如果column是一个列族名
String columnFamily = column;
// 遍历该列族下的所有列
for (Cell cell : result.rawCells()) {
if (Bytes.equals(Bytes.toBytes(columnFamily), CellUtil.cloneFamily(cell))) {
String columnQualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String fullColumnName = columnFamily + ":" + columnQualifier;
System.out.println("Row key: " + rowKeyString + ", Column: " + fullColumnName + ", Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
}
}
}