hbase常用命令及使用方法(查看hbase表结构的命令)
程序员文章站
2023-12-05 19:17:04
首先通过docker安装hbasedocker search hbase docker pull harisekhon/hbase启动hbase镜像docker run -d -p 2181:2181...
首先通过docker安装hbase
docker search hbase docker pull harisekhon/hbase
启动hbase镜像
docker run -d -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301 -p 16030:16030 -p 16020:16020 --name hbase001 harisekhon/hbase
访问hbase
访问localhost:16010
hbase命令行操作
docker exec -it hbase001 bash hbase shell
如上命令操作之后我们就进入了hbase的命令行操作模式:
下面,我们创建一个namespace为default,表名为test,列族column family为cf的表: create ‘test’,’cf’ 通过list命令可以查看所有的表:list
- 删除表:先disable,再drop
disable 'test'
drop 'test'
- 新增数据操作
put 'test','1','cf:name','flume'
put 'test','1','cf:age','18'
put 'test','2','cf:name','hbase'
put 'test','2','cf:age','20'
put 'test','3','cf:name','hadoop'
put 'test','3','cf:age','22'
put 'test','11','cf:name','spark'
put 'test','11','cf:age','24'
put 'test','21','cf:name','hive'
put 'test','21','cf:age','30'
- 删除数据操作
deleteall 'test','3'; -- 删除该rowkey下的所有数据
delete 'test','3','cf:name' -- 删除某一cell的数据
- 查看数据
hbase中有两个用于查看数据的命令:
1. scan 命令,用于查看某个表的全部数据;
2. get 命令,用于查看表的某一行数据
- 修改数据同样也是put操作
比如将行键为 2015003 的学生 age 改为 25:put 'student','2015003','age','25'
如上是命令行的一些基本操作,详细操作可以查看官网:
http://abloz.com/hbase/book.html
下面是通过java api的方式操作hbase
- 添加maven依赖
<dependency>
<groupid>org.apache.hbase</groupid>
<artifactid>hbase-client</artifactid>
<version>2.1.3</version>
</dependency>
测试工具栏如下:
package com.example.redisiondemo.utils;
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.admin;
import org.apache.hadoop.hbase.util.bytes;
import java.io.ioexception;
import java.util.arraylist;
import java.util.list;
public class hbasetest {
private static admin admin;
private static final string columns_family_1 = "cf1";
private static final string columns_family_2 = "cf2";
public static connection inithbase() throws ioexception {
configuration configuration = hbaseconfiguration.create();
configuration.set("hbase.zookeeper.quorum", "127.0.0.1");
configuration.set("hbase.zookeeper.property.clientport", "2181");
configuration.set("hbase.master", "127.0.0.1:16010");
connection connection = connectionfactory.createconnection(configuration);
return connection;
}
//创建表 create
public static void createtable(tablename tablename, string[] cols) throws ioexception {
admin = inithbase().getadmin();
if (admin.tableexists(tablename)) {
system.out.println("table already exists!");
} else {
htabledescriptor htabledescriptor = new htabledescriptor(tablename);
for (string col : cols) {
hcolumndescriptor hcolumndescriptor = new hcolumndescriptor(col);
htabledescriptor.addfamily(hcolumndescriptor);
}
admin.createtable(htabledescriptor);
system.out.println("table create successful");
}
}
public static tablename gettbname(string tablename) {
return tablename.valueof(tablename);
}
// 删除表 drop
public static void deletetable(tablename tablename) throws ioexception {
admin = inithbase().getadmin();
if (admin.tableexists(tablename)) {
admin.disabletable(tablename);
admin.deletetable(tablename);
system.out.println("table delete successful");
} else {
system.out.println("table does not exist!");
}
}
//put 插入数据
public static void insertdata(tablename tablename, student student) throws ioexception {
put put = new put(bytes.tobytes(student.getid()));
put.addcolumn(bytes.tobytes(columns_family_1), bytes.tobytes("name"), bytes.tobytes(student.getname()));
put.addcolumn(bytes.tobytes(columns_family_1), bytes.tobytes("age"), bytes.tobytes(student.getage()));
inithbase().gettable(tablename).put(put);
system.out.println("data insert success:" + student.tostring());
}
// delete 删除数据
public static void deletedata(tablename tablename, string rowkey) throws ioexception {
delete delete = new delete(bytes.tobytes(rowkey)); // 指定rowkey
// delete = delete.addcolumn(bytes.tobytes(columns_family_1), bytes.tobytes("name")); // 指定column,也可以不指定,删除该rowkey的所有column
inithbase().gettable(tablename).delete(delete);
system.out.println("delete success");
}
// scan数据
public static list<student> allscan(tablename tablename) throws ioexception {
resultscanner results = inithbase().gettable(tablename).getscanner(new scan().addfamily(bytes.tobytes("cf1")));
list<string> list = new arraylist<>();
for (result result : results) {
student student = new student();
for (cell cell : result.rawcells()) {
string colname = bytes.tostring(cell.getqualifierarray(), cell.getqualifieroffset(), cell.getqualifierlength());
string value = bytes.tostring(cell.getvaluearray(), cell.getvalueoffset(), cell.getvaluelength());
}
}
return null;
}
// 根据rowkey get数据
public static student singleget(tablename tablename, string rowkey) throws ioexception {
student student = new student();
student.setid(rowkey);
get get = new get(bytes.tobytes(rowkey));
if (!get.ischeckexistenceonly()) {
result result = inithbase().gettable(tablename).get(get);
for (cell cell : result.rawcells()) {
string colname = bytes.tostring(cell.getqualifierarray(), cell.getqualifieroffset(), cell.getqualifierlength());
string value = bytes.tostring(cell.getvaluearray(), cell.getvalueoffset(), cell.getvaluelength());
switch (colname) {
case "name":
student.setname(value);
break;
case "age":
student.setage(value);
break;
default:
system.out.println("unknown columns");
}
}
}
system.out.println(student.tostring());
return student;
}
// 查询指定cell数据
public static string getcell(tablename tablename, string rowkey, string cf, string column) throws ioexception {
get get = new get(bytes.tobytes(rowkey));
string rst = null;
if (!get.ischeckexistenceonly()) {
get = get.addcolumn(bytes.tobytes(cf), bytes.tobytes(column));
try {
result result = inithbase().gettable(tablename).get(get);
byte[] resbyte = result.getvalue(bytes.tobytes(cf), bytes.tobytes(column));
rst = bytes.tostring(resbyte);
} catch (exception exception) {
system.out.printf("columnfamily or column does not exists");
}
}
system.out.println("value is: " + rst);
return rst;
}
public static void main(string[] args) throws ioexception{
student student = new student();
student.setid("1");
student.setname("hzp");
student.setage("18");
string table = "student";
// createtable(gettbname(table), new string[]{columns_family_1, columns_family_2});
// deletetable(gettbname(table));
insertdata(gettbname(table), student);
// deletedata(gettbname(table), "1");
// singleget(gettbname(table), "2");
// getcell(gettbname(table), "1", "cf1", "name");
}
}
student实体
package com.example.redisiondemo.utils;
public class student {
private string id;
private string name;
private string age;
public string getid() {
return id;
}
public void setid(string id) {
this.id = id;
}
public string getname() {
return name;
}
@override
public string tostring() {
return "student{" +
"id='" + id + ''' +
", name='" + name + ''' +
", age='" + age + ''' +
'}';
}
public void setname(string name) {
this.name = name;
}
public string getage() {
return age;
}
public void setage(string age) {
this.age = age;
}
}
下一篇: js回调函数原理(教你判断下跌还是回调)