当前位置:   article > 正文

【大数据】HDFS、HBase操作教程(含指令和JAVA API)_hdfs create database java api

hdfs create database java api

目录

1.前言

2.HDFS

2.1.指令操作

2.2.JAVA API

3.HBase

3.1.指令操作

3.2.JAVA API


1.前言

本文是作者大数据专栏系列的其中一篇,前文中已经详细聊过分布式文件系统HDFS和分布式数据库HBase了,本文将会是它们的实操讲解。

HDFS相关前文:

【大数据】分布式文件系统HDFS-CSDN博客

【大数据】大数据概论与Hadoop_大数据导论与hadoop-CSDN博客

HBase相关前文:

【大数据】分布式数据库HBase-CSDN博客

【大数据】分布式数据库HBase下载安装教程-CSDN博客

2.HDFS

2.1.指令操作

创建目录:

hdfs dfs -mkdir /user/mydir

递归创建目录:

hdfs dfs -mkdir -p /user/mydir/subdir

上传文件到HDFS:

hdfs dfs -put localfile.txt /user/mydir/

下载文件到本地:

hdfs dfs -get /user/mydir/file.txt localdir/

删除文件:

hdfs dfs -rm /user/mydir/file.txt

递归删除目录:

hdfs dfs -rm -r /user/mydir

查看目录内容:

hdfs dfs -ls /user/mydir

递归查看目录内容:

hdfs dfs -lsr /user/mydir

查看文件详细信息:

hdfs dfs -stat /user/mydir/file.txt

移动或重命名文件:

hdfs dfs -mv /user/mydir/file.txt /user/mydir/newfile.txt

复制文件、目录:

hdfs dfs -cp /user/mydir/file.txt /user/mydir2/

查看文件内容:

hdfs dfs -cat /user/mydir/file.txt

2.2.JAVA API

首先这里有个巨坑:

一定要把core-site.xml里面的fs.defaultFS换成真实IP地址,不能用localhsot

  1. <configuration
  2. <property>
  3. <name>hadoop.tmp.version</name>
  4. <value>file:/usr/local/hadoop/tmp</value>
  5. </property>
  6. <property>
  7. <name>fs.defaultFS</name>
  8. <value>hdfs://localhost:9000</value>
  9. </property>
  10. </configuration>

如果JAVA API的client端会先找HDFS拿到fs.defaultFS,然后再去访问拿到的地址上的HDFS,如果JAVA API的client端和HDFS不在一台机器上,JAVA API的Client就会去访问它本地的localhost的9000端口上的服务,会直接报错:

Connection refused: no further information

依赖:

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>3.1.3</version>
</dependency>

代码示例:

  1. import java.io.*;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.FileSystem;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.IOUtils;
  6. public class HDFSSample {
  7.   public static void main(String[] args) throws IOException {
  8.       Configuration conf = new Configuration();
  9.       FileSystem fs = FileSystem.get(conf);
  10.       // 创建目录
  11.       createDirectory(fs, "/user/hadoop/testdir");
  12.       // 上传文件
  13.       uploadFile(fs, "/user/hadoop/testfile.txt", "C:/localfile.txt");
  14.       // 下载文件
  15.       downloadFile(fs, "/user/hadoop/testfile.txt", "C:/downloadedfile.txt");
  16.       // 列出目录内容
  17.       listDirectory(fs, "/user/hadoop");
  18.       // 删除文件
  19.       deleteFile(fs, "/user/hadoop/testfile.txt");
  20.       // 删除目录
  21.       deleteDirectory(fs, "/user/hadoop/testdir");
  22.       // 关闭文件系统
  23.       fs.close();
  24.   }
  25.   private static void createDirectory(FileSystem fs, String dirPath) throws IOException {
  26.       fs.mkdirs(new Path(dirPath));
  27.       System.out.println("Directory created: " + dirPath);
  28.   }
  29.   private static void uploadFile(FileSystem fs, String hdfsPath, String localFilePath) throws IOException {
  30.       Path hdfsPathObj = new Path(hdfsPath);
  31.       Path localPathObj = new Path(localFilePath);
  32.       fs.copyFromLocalFile(false, true, localPathObj, hdfsPathObj);
  33.       System.out.println("File uploaded: " + localFilePath + " to " + hdfsPath);
  34.   }
  35.   private static void downloadFile(FileSystem fs, String hdfsPath, String localFilePath) throws IOException {
  36.       Path hdfsPathObj = new Path(hdfsPath);
  37.       Path localPathObj = new Path(localFilePath);
  38.       fs.copyToLocalFile(true, hdfsPathObj, localPathObj);
  39.       System.out.println("File downloaded: " + hdfsPath + " to " + localFilePath);
  40.   }
  41.   private static void listDirectory(FileSystem fs, String dirPath) throws IOException {
  42.       for (FileStatus file : fs.listStatus(new Path(dirPath))) {
  43.           System.out.println("File/Directory: " + file.getPath().toString());
  44.       }
  45.   }
  46.   private static void deleteFile(FileSystem fs, String filePath) throws IOException {
  47.       Path filePathObj = new Path(filePath);
  48.       if (fs.exists(filePathObj)) {
  49.           fs.delete(filePathObj, false);
  50.           System.out.println("File deleted: " + filePath);
  51.       } else {
  52.           System.out.println("File not found: " + filePath);
  53.       }
  54.   }
  55.   private static void deleteDirectory(FileSystem fs, String dirPath) throws IOException {
  56.       Path dirPathObj = new Path(dirPath);
  57.       if (fs.exists(dirPathObj)) {
  58.           fs.delete(dirPathObj, true);
  59.           System.out.println("Directory deleted: " + dirPath);
  60.       } else {
  61.           System.out.println("Directory not found: " + dirPath);
  62.       }
  63.   }
  64. }

3.HBase

3.1.指令操作

创建一个列族为info的student表:

create 'Student', 'info'

往表里插数据:

put 'Student', '1', 'info:id', '1'

put 'Student', '1', 'info:name', 'Alice' put 'Student', '1', 'info:age', '20'

put 'Student', '1', 'info:major', 'Computer Science'

put 'Student', '2', 'info:id', '2'

put 'Student', '2', 'info:name', 'Bob' put 'Student', '2', 'info:age', '21'

put 'Student', '2', 'info:major', 'Mathematics'

查询单个:

get 'Student', '1'

查询批量:

scan 'Student'

条件批量查询:

scan 'Student', {FILTER => "SingleColumnValueFilter('info','age', >=, 'binary:20')"}

在HBase中,Scan对象用于定义在表上进行扫描时的参数,包括哪些行和列需要被检索,以及如何处理这些数据。Filter是Scan的一部分,用于在服务器端对返回的数据进行过滤,以减少网络传输的数据量,提高查询效率。 Filter类提供了一种方式来指定复杂的过滤逻辑,允许你基于行键(Row Key)、列族、列限定符和时间戳来筛选结果。以下是一些常见的Filter类型及其用法:

  • RowFilter: 用于基于行键的比较,如RowFilter(=, 'binary:rowKey'),匹配特定的行键。

  • SingleColumnValueFilter: 用于基于列族和列限定符的值进行比较,如SingleColumnValueFilter('cf', 'qualifier', CompareOp.GREATER_OR_EQUAL,BinaryComparator.valueOf(Bytes.toBytes(20))),匹配特定列族和列限定符的值大于或等于给定值的行。

  • PrefixFilter: 用于匹配以特定前缀开头的行键,如PrefixFilter(Bytes.toBytes('row-prefix'))。

  • RegexStringComparator: 用于基于正则表达式匹配行键,如RowFilter(CompareOp.EQUAL, RegexStringComparator('.pattern.'))。

  • MultipleColumnPrefixFilter: 用于匹配具有相同前缀的多个列,如MultipleColumnPrefixFilter(Bytes.toBytes('col-prefix'))。

  • PageFilter: 用于限制返回结果的数量,这对于大数据量的扫描很有用,如PageFilter(pageSize),pageSize是你希望一次返回的最大行数。

  • TimestampsFilter: 用于指定返回的行必须包含特定时间戳范围内的版本,如TimestampsFilter(timestamps),timestamps是一个包含多个时间戳的列表。

  • ValueFilter 和 QualifierFilter: 分别基于列值和列限定符进行过滤。

使用不同类型的过滤器的指令示例:

RowFilter(基于行键过滤)

scan 'Student', {FILTER => "RowFilter(=, 'regexstring:^1')"}

SingleColumnValueFilter(基于特定列的值过滤)

scan 'Student', {FILTER => "SingleColumnValueFilter ('info', 'age', >=, 'binary:20')"}

PrefixFilter(基于列前缀过滤)

scan 'Student', {FILTER => "PrefixFilter(Bytes.toBytes('info'))"}

RegexStringComparator(基于列值的正则表达式过滤)

scan 'Student', {FILTER => "RowFilter(=, 'regexstring:.Alice.')"}

MultipleColumnPrefixFilter(基于多列前缀过滤)

scan 'Student', {FILTER => "MultipleColumnPrefixFilter(Bytes.toBytes('info'))"}

ValueFilter(基于列值的比较过滤)

scan 'Student', {FILTER => "ValueFilter(=, 'binary:Alice')"}

QualifierFilter(基于列限定符的比较过滤)

scan 'Student', {FILTER => "QualifierFilter(=, 'binary:age')"}

清理表:

delete 'Student', '1' delete 'Student', '2' delete 'Student', '3' disable 'Student' drop 'Student'

3.2.JAVA API

HBase也要注意和HDFS中相似的问题,hbase-site.xml中也要用真实的IP地址,不然JAVA API的Client端和HBase不在一台机器上的会,就会访问不到HBase,下面的代码中作为演示代码并没有用真实IP,仍然用的LocalHost,这点要注意。

依赖:

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.2.2</version>
</dependency>
 

代码示例:

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.hbase.TableName;
  4. import org.apache.hadoop.hbase.client.Connection;
  5. import org.apache.hadoop.hbase.client.ConnectionFactory;
  6. import org.apache.hadoop.hbase.client.Delete;
  7. import org.apache.hadoop.hbase.client.Get;
  8. import org.apache.hadoop.hbase.client.Put;
  9. import org.apache.hadoop.hbase.client.Result;
  10. import org.apache.hadoop.hbase.client.Table;
  11. import org.apache.hadoop.hbase.util.Bytes;
  12. public class HBaseExample {
  13. public static void main(String[] args) {
  14. Configuration config = HBaseConfiguration.create();
  15. config.set("hbase.zookeeper.quorum", "localhost"); // 设置ZooKeeper地址
  16. config.set("hbase.zookeeper.property.clientPort", "2181"); // 设置ZooKeeper端口
  17. try (Connection connection = ConnectionFactory.createConnection(config);
  18. Table table = connection.getTable(TableName.valueOf("students"))) {
  19. // 创建表
  20. table.createIfNotExists();
  21. // 插入数据
  22. Put put1 = new Put(Bytes.toBytes("student1"));
  23. put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
  24. put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("20"));
  25. put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("major"), Bytes.toBytes("CS"));
  26. table.put(put1);
  27. Put put2 = new Put(Bytes.toBytes("student2"));
  28. put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Bob"));
  29. put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("21"));
  30. put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("major"), Bytes.toBytes("Math"));
  31. table.put(put2);
  32. // 查询数据
  33. Get get = new Get(Bytes.toBytes("student1"));
  34. Result result = table.get(get);
  35. System.out.println("Name: " + Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
  36. System.out.println("Age: " + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"))));
  37. System.out.println("Major: " + Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("major"))));
  38. // 根据条件删除数据
  39. Delete delete = new Delete(Bytes.toBytes("student1"));
  40. table.delete(delete);
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/854548
推荐阅读
相关标签
  

闽ICP备14008679号