【Spark-HDFS小文件合并】使用 Spark 实现 HDFS 小文件合并

news/2024/5/20 4:51:56 标签: spark, hdfs, 大数据, #HDFS小文件合并

【Spark-HDFS小文件合并】使用 Spark 实现 HDFS 小文件合并

  • 1)导入依赖
  • 2)代码实现
    • 2.1.HDFSUtils
    • 2.2.MergeFilesApplication

需求描述:

1、使用 Spark 做小文件合并压缩处理。

2、实际生产中相关配置、日志、明细可以记录在 Mysql 中。

3、core-site.xml、hdfs-site.xml、hive-site.xml、yarn-site.xmlx 等文件放在项目的 resources 目录下进行认证。

4、下面的案例抽取出了主体部分的代码,具体实现时需要结合 HDFS 工具类,利用好 Mysql 做好配置、日志、以及相关明细,结合各自业务进行文件合并。

1)导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test.cn.suitcase</groupId>
    <artifactId>mergefiles</artifactId>
    <version>4.0.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <encoding>UTF-8</encoding>
<!--        <spark.version>3.0.2</spark.version>-->
        <spark.version>2.4.8</spark.version>
        <scala.version>2.11.12</scala.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-compiler</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-reflect</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-launcher_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.32</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.14.2</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!-- Java Compiler -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <!-- We use the maven-shade plugin to create a fat jar that contains all necessary dependencies. -->
            <!-- Change the value of <mainClass>...</mainClass> if your program entry point changes. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <excludes>
                                    <exclude>org.apache.flink:force-shading</exclude>
                                    <exclude>com.google.code.findbugs:jsr305</exclude>
                                    <exclude>org.slf4j:*</exclude>
                                    <exclude>org.apache.logging.log4j:*</exclude>
                                </excludes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <groups>IntegrationTest</groups>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </build>

</project>

2)代码实现

2.1.HDFSUtils

public class HDFSUtils {

    private static Logger logger = LoggerFactory.getLogger(HDFSUtils.class);
    private static final Configuration hdfsConfig = new Configuration();
    private static FileSystem fs;

    public static void init() {
        System.out.println(Thread.currentThread().getContextClassLoader());
        try {
            hdfsConfig.addResource(Thread.currentThread().getContextClassLoader().getResource("./core-site.xml"));
            hdfsConfig.addResource(Thread.currentThread().getContextClassLoader().getResource("./hdfs-site.xml"));
            fs = FileSystem.get(hdfsConfig);
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
            logger.error("Load properties failed.");
        } catch (IOException ioe) {
            ioe.printStackTrace();
            logger.error(String.format("IOException: " + ioe.getMessage()));
        }
    }

    public static long getDirectorySize(String directoryPath) {
        final Path path = new Path(directoryPath);
        long size = 0;
        try {
            size = fs.getContentSummary(path).getLength();


        } catch (IOException ex) {

        }
        return size;
    }

    public static long getFileCount(String directoryPath) {
        final Path path = new Path(directoryPath);
        long count = 0;
        try {
            count = fs.getContentSummary(path).getFileCount();


        } catch (IOException ex) {

        }
        return count;
    }

    public static long getBlockSize() {
        return fs.getDefaultBlockSize(fs.getHomeDirectory());
    }

    public static String getFile(String filePath) {
        final Path path = new Path(filePath);
        FSDataInputStream dis = null;
        String fileName = null;
        try {
            if (fs.exists(path) && fs.isFile(path)) {
                dis = fs.open(path);
                StringWriter stringWriter = new StringWriter();
                IOUtils.copy(dis, stringWriter, "UTF-8");
                fileName = stringWriter.toString();
                return fileName;
            } else {
                throw new FileNotFoundException();
            }
        } catch (IOException ioException) {
            logger.error("Get file from hdfs failed: " + ioException.getMessage());

        } finally {
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException ex) {
                    logger.error("close FSDataInputStream failed: " + ex.getMessage());
                }
            }
        }
        return fileName;
    }

    public static Boolean exists(String filePath) {
        Path path = new Path(filePath);
        Boolean ifExists = false;
        try {
            ifExists = fs.exists(path);
            return ifExists;
        } catch (IOException ex) {
            logger.error(String.format("hdfs file %s not exists", filePath));

        }
        return ifExists;
    }

    public static boolean renameDir(String existingName, String newName) {
        final Path existingPath = new Path(existingName);
        final Path finalName = new Path(newName);
        try {
            if (exists(newName)) {
                logger.error(String.format("Path %s already exists when try to rename %s to %s.", newName, existingName, newName));
                return false;
            }
            return fs.rename(existingPath, finalName);
        } catch (IOException ex) {
            logger.error("Rename hdfs directory failed: " + ex.getMessage());
        }
        return false;
    }

    public static boolean removeDirSkipTrash(String dir) {
        Path path = new Path(dir);
        boolean rv = false;
        try {
            if (exists(dir)) {
                if (fs.delete(path, true)) {
                    logger.info(String.format("文件夹 %s 删除成功.", path));
                    rv = true;
                }
            } else {
                logger.error(String.format("要删除的文件夹 %s 不存在", dir));
                return false;
            }
        } catch (IOException ex) {
            logger.error("文件夹 %s 存在但是删除失败");
        }
        return rv;

    }

    public static List<String> listDirs(String baseDir) {
        Path path = new Path(baseDir);
        List<String> dirs = new ArrayList<>();
        try {
            FileStatus[] fileStatuses = fs.globStatus(path);

            for (int i = 0; i < fileStatuses.length; i++) {
                dirs.add(fileStatuses[i].getPath().toUri().getRawPath());
                }
            }
        } catch (Exception ex) {
            logger.error(String.format("List directories under %s failed.", baseDir));
        }
        return dirs;
    }

    public static void close() {
        try {
            fs.close();
        } catch (IOException ex) {
            logger.error("hdfs file system close failed: " + ex.getMessage());
        }
    }

}

2.2.MergeFilesApplication

下面的案例抽取出了主体部分的代码,具体实现时需要结合 HDFS 工具类,利用好 Mysql 做好配置、日志、以及相关明细,结合各自业务进行文件合并。

public class MergeFilesApplication {

    public static void main(String[] args) {
        System.out.println(Arrays.asList(args));
        //指定hadoop用户
        System.setProperty("HADOOP_USER_NAME", "hdfs");
        System.setProperty("user.name", "hdfs");
        //获取 SparkSession 对象
		SparkSession sparkSession = SparkSession.builder()
                .config("spark.scheduler.mode", "FAIR")//配置调度模式
                .config("spark.sql.warehouse.dir", "/warehouse/tablespace/external/hive")//配置warehouse目录
                .appName("MergeFilesApplication")
                .getOrCreate();
		//合并文件
        sparkSession.read()//spark读取
                    .parquet(sourceDir)//读取数据源目录
                    .coalesce(partitions)//配置spark分区数
                    .sortWithinPartitions("col1", "col2")//每个分区内按照指定需要的列进行排序
                    .write()//spark写入
                    .mode(SaveMode.Append)//写入模式为追加
                    .option("compression", "gzip")//压缩方式以为gzip
                    .parquet(targetMergedDir);//写入目标目录
    }
}

http://www.niftyadmin.cn/n/5288456.html

相关文章

云原生Kubernetes系列 | Kubernetes Secret及ConfigMap

云原生Kubernetes系列 | Kubernetes Secret及Configmap 1. Secret及Configmap使用背景简介2. Secret2.1. Secret创建方式2.1.1. 命令行方式2.1.2. 文件方式2.1.3. 变量方式2.1.4. YAML文件方式2.2. Secret使用方式2.2.1. 用于传递配置文件2.2.3. 用于传递变量3. ConfigMap1. Se…

电脑显卡驱动停止响应该怎么办?为什么会出现这种情况

显卡驱动停止响应的原因 当你电脑突然弹框说显卡驱动停止响应&#xff0c;你知道是出现什么问题了吗&#xff0c;下面我们为大家总结了有可能造成显卡驱动停止响应的原因。1. 过热&#xff1a;显卡在长时间高负载下可能会过热&#xff0c;导致驱动停止响应。过高的温度可能…

[spark] 存储到hdfs时指定分区

在 SparkSQL 中指定多个分区字段进行数据存储&#xff1a; 类似hive 分区存储 文章目录 代码示例 代码 import org.apache.spark.sql.SparkSessionval spark SparkSession.builder().appName("MultiPartitionedWriteExample").getOrCreate()// 假设你有一个 DataFr…

Java之遍历树状菜单

&#x1f607;作者介绍&#xff1a;一个有梦想、有理想、有目标的&#xff0c;且渴望能够学有所成的追梦人。 &#x1f386;学习格言&#xff1a;不读书的人,思想就会停止。——狄德罗 ⛪️个人主页&#xff1a;进入博主主页 &#x1f5fc;专栏系列&#xff1a;无 &#x1f33c…

LeetCode206反转链表(java实现)

今天带来的题目解析是leetcode206&#xff0c;反转链表&#xff0c;我们来看下题目描述 如何实现链表的反转呢&#xff1f;我在这里提供的思路是双指针的思路。 具体的思路如下&#xff1a; 假设我们的原链表如下 首先定义一个指针pre&#xff0c;用于指向head之前的位置&am…

Java开发框架和中间件面试题(6)

61.什么是Spring Batch&#xff1f; Spring batch是一个轻量级的&#xff0c;完善的批处理框架&#xff0c;他主要的目的在于帮助企业建立健壮&#xff0c;高效的批处理应用。Spring batch是Spring的一个子项目&#xff0c;他使用Java语言并基于Spring框架作为基础开发&#xf…

13.鸿蒙HarmonyOS App(JAVA)文本框组件按钮点击提示

13.鸿蒙HarmonyOS App(JAVA)文本框按钮点击提示 点击按钮触发组件状态&#xff0c;点击改变颜色 文本框组件&#xff0c;文本居中&#xff0c;斜体&#xff0c;左右对齐&#xff0c;点击显示提示信息 Button button(Button) findComponentById(ResourceTable.Id_btn_1); but…

cfa一级考生复习经验分享系列(十四)

首先说一下自己的背景&#xff0c;一个和金融没有半毛钱关系的数据分析师&#xff0c;之前考出了FRM。这次用一个半月突击12月的1级考试拿到了9A1B的成绩&#xff0c;纯属运气。以下纯属经&#xff08;chě&#xff09;验&#xff08;dn&#xff09;&#xff0c;请看看就好&…