大数据实验(HBase基础操作)
(一)Hadoop提供的HBase Shell命令完成任务
要想将这些编码显示为中文,我们需要在get命令后添加一个属性:{FORMATTER => 'toString'}
(1)列出hbase所有表信息
(2)打印表的所有数据
(3)添加、删除指定列族或列
(4)清空指定表的数据(先禁用表在清空)
(5)统计行数
(二)HBase数据库操作
1.根据给出的表数据在HBase建表并存入数据
数据比较多可以先在txt文件里面写好全部的语句然后复制粘贴到hbase执行
Student学生表
course表:
sc选课表:
2.编程实现
编程实现参考:
大数据之Hadoop学习(十)HBase Java API编程_addrecord(string tablename, string row, string[] f-CSDN博客
准备工作:
先创建一个maven项目,在pom.xml导入依赖
<repositories><!-- 代码库 --> <repository> <id>aliyun</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> <updatePolicy>never</updatePolicy> </snapshots> </repository> </repositories> <dependencies> <!-- HBase的Java客户端 --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.3</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <target>1.8</target> <source>1.8</source> </configuration> </plugin> </plugins> </build>
2.将虚拟机的hbase-site.xml、core-site.xml、log4j.properties复制到resource目录中
从Linux中下载:sz /export/server/hbase-2.1.0/conf/hbase-site.xml
从Linux中下载:sz /export/server/hadoop-2.7.5/etc/hadoop/core-site.xml
编程实现
因为我导入了hbase-site.xml、core-site.xml、log4j.properties到resource目录,所以在连接上我可以直接这样子写
public static void init() { Configuration configuration = HBaseConfiguration.create(); try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (Exception ex){ ex.printStackTrace(); } } public static void close() { try{ if(admin!=null){ admin.close(); } if(connection!=null){ connection.close(); } }catch (Exception ex){ ex.printStackTrace(); } }
连接成功,HBase数据表(操作后)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2022-11-23 sort
2022-11-23 正则