使用Java操作Hbase
创始人
2024-05-30 15:51:33
0

目录

修改hosts文件

导入jar包

配置hbase信息,连接hbase数据库

创建表

删除表

获取namespace

获取tables

添加数据

查询表中的数据

查询表中所有数据

关闭流


修改hosts文件

位置:C:\Windows\System32\drivers\etc\hosts

win+R:ping一下IP地址和虚拟机名看一下能不能ping通 

 

导入jar包

创建maven项目,在pom.xml 里面添加jar包

    org.apache.hbasehbase-client2.3.5org.apache.hbasehbase-server2.3.5

配置hbase信息,连接hbase数据库

    //定义一个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);//拿到adminadmin = 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"));
}

获取namespace

    public void getAllNamespace() throws IOException {String[] namespaces = admin.listNamespaces();String s = Arrays.toString(namespaces);System.out.println(s);}

 

获取tables

    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();}}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...