随笔 - 352  文章 - 0  评论 - 5  阅读 - 63642

大数据实验(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数据表(操作后)

 

posted on   201812  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2022-11-23 sort
2022-11-23 正则
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示