Java read properties configuration information tool class writing

0 Preface

In the project, some configuration information is usually written in the configuration file, which is convenient to modify the parameters and can achieve the purpose of process automation. This article explains in detail how the Properties class reads the configuration file information

1 java properties object

(1) There is a more important class Properties (java.util.Properties) in Java, which represents a persistent set of detailed properties. Properties can be saved to a stream or loaded from a stream. Here are the main points about attributes:

  • Each key and its corresponding value in the attribute list is a string.

  • An attribute list can contain another attribute list as its "default", and the second attribute can be searched in the list if there is no attribute key found in the original attribute list.

  • This class is thread-safe; multiple threads can share a Properties object without external synchronization

(2) The main methods of this category are as follows:

 

 

(3) It is mainly used to read Java configuration files and store some frequently used data for programmers to modify. The configuration file is a text file with the suffix (.properties),

The content format of the file is "key=value", and the text comment can be annotated with "#". As shown below:

(4) Java Properties instance

  •    Obtain the input stream object from the target path test.properites. The suffix must be .properties
  •    Use the load() method of the Properties class to get data from the byte input stream
  •   Use the getProperty(String key) method of the Properties class to obtain the value according to the parameter key

2 Compilation of tools

package testproperties;

import org.apache.hadoop.conf.Configuration;
import org.apache.log4j.Logger;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.net.URI;

public class PropertyFileReader {

    private static final Logger logger = Logger.getLogger(PropertyFileReader.class);
    private static Properties prop = new Properties();

    public static void main(String[] args) throws IOException {

        PropertyFileReader pfr = new PropertyFileReader();
        Properties pro = pfr.readLocalPropertyFile("test.properties");
        String url = pro.getProperty("url");
        System.out.println(url);
        

    }

//从本地读取
    public static Properties readLocalPropertyFile(String filename) throws IOException {

        if (prop.isEmpty()) {

            InputStream input = PropertyFileReader.class.getClassLoader().getResourceAsStream(filename);
            try {
                prop.load(input);
            } catch (IOException e) {
                logger.error(e);
                throw e;
            } finally {
                if (input != null) {
                    input.close();
                }

            }

        }
        return prop;
    }
//从HDFS读取
    public static Properties readHdfsPropertyFile(String defaultFS, String path) throws Exception {
        {
            if (prop.isEmpty()) {
                Configuration config = new Configuration();
                config.set("fs.defaultFS", defaultFS);
                FileSystem hdfs = FileSystem.get(new URI(defaultFS), config, "test");
                FSDataInputStream fs = hdfs.open(new Path(path));
                InputStream input = fs.getWrappedStream();
                try {
                    prop.load(input);
                } catch (IOException ex) {
                    logger.error(ex);
                    throw ex;
                } finally {
                    if (input != null) {
                        input.close();
                    }
                }
            }
            return prop;
        }
    }
}

The test results are as follows:

Extension : For shell scripts to read configuration files and HDFS files, it is relatively simple and can be obtained directly through the command line. As follows:

cat /home/centos/phm/JTTL_ETL_COMMON/oozieurl.txt | grep oozie_url | awk -F'=' '{ print $2 }' | sed s/[[:space:]]//g

Read the /home/centos/phm/JTTL_ETL_COMMON/oozieurl.txt file and filter out the oozie_url line, and take the second column separated by "=".

Note: sed s/[[:space:]]//g must be used here to remove extra spaces to prevent errors caused by formatting.

If you want to read the content on HDFS, replace the above command with the hadoop command, such as:

hadoop fs -cat /home/centos/phm/JTTL_ETL_COMMON/oozieurl.txt | grep oozie_url | awk -F'=' '{ print $2 }' | sed s/[[:space:]]//g

3 summary

Generally, in an online production environment, we tend to place some parameters in a configuration file or cluster shared directory (such as HDFS), which is convenient for programmers to modify, and it is also an implementation of an automated process. This article explains how to read configuration file information through the Properties class. One is to obtain it from a local resource file, and the other is to obtain it from an HDFS shared directory.

Guess you like

Origin blog.csdn.net/godlovedaniel/article/details/114449144