Small File Combination Tools¶
Tool Overview¶
In a large-scale Hadoop production cluster, HDFS metadata is stored in the NameNode memory, and the cluster scale is restricted by the memory limitation of each NameNode. If there are a large number of small files in the HDFS, a large amount of NameNode memory is consumed, which greatly reduces the read and write performance and prolongs the job running time. Based on the preceding information, the small file problem is a key factor that restricts the expansion of the Hadoop cluster.
This tool provides the following functions:
Checks the number of small files whose size is less than the threshold configured by the user in tables and returns the average size of all data files in the table directory.
Provides the function of combination table files. Users can set the average file size after combination.
Supported Table Types¶
Spark: Parquet, ORC, CSV, Text, and Json.
Hive: Parquet, ORC, CSV, Text, RCFile, Sequence and Bucket.
Note
After tables with compressed data are merged, Spark uses the default compression format Snappy for data compression. You can configure spark.sql.parquet.compression.codec (available values: uncompressed, gzip, lzo, and snappy) and spark.sql.orc.compression.codec (available values: uncompressed, zlib, lzo, and snappy) on the client to select the compression format for the Parquet and ORC tables. Compression formats available for Hive and Spark tables are different, except the preceding compression formats, other compression formats are not supported.
To merge bucket table data, you need to add the following configurations to the hive-site.xml file on the Spark2x client:
<property> <name>hive.enforce.bucketing</name> <value>false</value> </property> <property> <name>hive.enforce.sorting</name> <value>false</value> </property>
Spark does not support the feature of encrypting data columns in Hive.
Tool Usage¶
Download and install the client. For example, the installation directory is /opt/client. Go to /opt/client/Spark2x/spark/bin and run the mergetool.sh script.
Environment variables loading
source /opt/client/bigdata_env
source /opt/client/Spark2x/component_env
Scanning function
Command: sh mergetool.sh scan <db.table> <filesize>
The format of db.table is Database name,Table name. filesize is the user-defined threshold of the small file size (unit: MB). The returned result is the number of files that is smaller than the threshold and the average size of data files in the table directory.
Example: sh mergetool.sh scan default.table1 128
Combination function
Command: sh mergetool.sh merge <db.table> <filesize> <shuffle>
The format of db.table is Database name,Table name. filesize is the user-defined average file size after file combination (unit: MB). shuffle is a Boolean value, and the value is true or false, which is used to configure whether to allow data to be shuffled during the merge.
Example: sh mergetool.sh merge default.table1 128 false
If the following information is displayed, the operation is successful:
SUCCESS: Merge succeeded
Note
Ensure that the current user is the owner of the merged table.
Before combination, ensure that HDFS has sufficient storage space, greater than the size of the combined table.
Table data must be combined separately. If a table is read during table data combination, the file may not be found temporarily. After the combination is complete, this problem is resolved. During the combination, do not write data to the corresponding tables. Otherwise, data inconsistency may occur.
If an error occurs indicating that the file does not exist when the query of data in a partitioned table is performed on the session spark-beeline/spark-sql that is always in the connected status. You can run the refresh tableTable name command as prompted to query the data again.
Configure filesize based on the site requirements. For example, you can set filesize to a value greater than the average during file merging after obtaining the average file size by file scan. Otherwise, the number of files may increase after the file merging.
During the file merging, data in the original tables is removed to the recycle bin. In the case of any exception occurs on the data after file merging, the data in the original tables are used to replace the damaged data. If an exception occurs during the process, restore the data in the trash directory by using the mv command in HDFS.
In the HDFS router federation scenario, if the target NameService of the table root path is different from that of the root path /user, you need to manually clear the original table files stored in the recycle bin during the second combination. Otherwise, the combination fails.
This tool uses the configuration of the client. Performance optimization can be performed modifying required configuration in the client configuration file.
shuffle configuration
For the combination function, you can roughly estimate the change on the number of partitions before and after the combination.
Generally, if the number of old partitions is greater than the number of new partitions, set shuffle to false. However, if the number of old partitions is much greater than that of new partitions (for example, more than 100 times), you can set shuffle to true to increase the degree of parallelism and improve the combination speed.
Important
If shuffle is set to true (repartition), the performance is improved. However, due to the particularity of the Parquet and ORC storage modes, repartition will reduce the compression ratio and the total size of the table in HDFS increases by 1.3 times.
If shuffle is set to false (coalesce), the merged files may have some difference in size, which is close to the value of the configured filesize.
Log storage location
The default log storage path is /tmp/SmallFilesLog.log4j. To customize the log storage path, you can configure log4j.appender.logfile.File in /opt/client/Spark2x/spark/tool/log4j.properties.