目录
修改hosts文件
导入jar包
配置hbase信息,连接hbase数据库
创建表
删除表
获取namespace
获取tables
添加数据
查询表中的数据
查询表中所有数据
关闭流
位置:C:\Windows\System32\drivers\etc\hosts
win+R:ping一下IP地址和虚拟机名看一下能不能ping通
创建maven项目,在pom.xml 里面添加jar包
org.apache.hbase
hbase-client
2.3.5
org.apache.hbase
hbase-server
2.3.5
//定义一个config,用于获取配置对象
static Configuration config = null;
//获取连接
private Connection conn = null;
Admin admin = null;
public void init() throws IOException {
//配置hbase信息,连接hbase数据库
config = HBaseConfiguration.create();
config.set(HConstants.HBASE_DIR, "hdfs://192.168.152.192:9000/hbase");
config.set(HConstants.ZOOKEEPER_QUORUM, "192.168.152.192");
config.set(HConstants.CLIENT_PORT_STR, "2181");
//hbase连接工厂
conn = ConnectionFactory.createConnection(config);
//拿到admin
admin = conn.getAdmin();
}
创建命名空间
public void createNameSpace() {
NamespaceDescriptor test = NamespaceDescriptor.create("test").build();
try {
//执行创建对象
admin.createNamespace(test);
} catch (IOException e) {
e.printStackTrace();
}
}
public void createTable() throws IOException {
//创建表的描述类
TableName tableName = TableName.valueOf("test:student");
//获取表格描述器
HTableDescriptor desc = new HTableDescriptor(tableName);
//创建列族的描述类,添加列族
HColumnDescriptor family1 = new HColumnDescriptor("info1");
HColumnDescriptor family2 = new HColumnDescriptor("info2");
desc.addFamily(family1);
desc.addFamily(family2);
admin.createTable(desc);
}
public void deleteTable() throws IOException {
//禁用
admin.disableTable(TableName.valueOf("test:student"));
//删除
admin.deleteTable(TableName.valueOf("test:student"));
}
public void getAllNamespace() throws IOException {
String[] namespaces = admin.listNamespaces();
String s = Arrays.toString(namespaces);
System.out.println(s);
}
public void getAllNamespace() throws IOException {
List tableDescriptors =
admin.listTableDescriptorsByNamespace("test".getBytes());
System.out.println(tableDescriptors);
}
public void insertData() throws IOException {
//获取表的信息
Table table = conn.getTable(TableName.valueOf("test:student"));
//设置行键
Put put = new Put(Bytes.toBytes("student1"));
//设置列的标识以及列值
put.addColumn("info1".getBytes(), "name".getBytes(), "zs".getBytes());
put.addColumn("info2".getBytes(), "school".getBytes(), "xwxx".getBytes());
//执行添加
table.put(put);
//使用集合添加数据
Put put2 = new Put(Bytes.toBytes("student2"));
put2.addColumn("info1".getBytes(), "name".getBytes(), "zss".getBytes());
put2.addColumn("info2".getBytes(), "school".getBytes(), "xwxx".getBytes());
Put put3 = new Put(Bytes.toBytes("student3"));
put3.addColumn("info1".getBytes(), "name".getBytes(), "zsr".getBytes());
put3.addColumn("info2".getBytes(), "school".getBytes(), "xwxx".getBytes());
List list = new ArrayList<>();
list.add(put2);
list.add(put3);
table.put(list);
}
public void queryData() throws IOException {
Table table = conn.getTable(TableName.valueOf("test:student"));
Get get = new Get(Bytes.toBytes("student1"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
System.out.println("姓名:" + Bytes.toString(value));
value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
System.out.println("学校:" + Bytes.toString(value));
}
public void close() throws IOException {
if (admin != null) {
admin.close();
}
if (conn != null) {
conn.close();
}
}