在运行时从属性文件中读取值

时间:2015-07-24 05:38:28

标签: java properties cassandra runtime properties-file

我有以下功能:

public void execute(Tuple input, BasicOutputCollector collector) {

    String term = input.getString(0);
    int year = input.getInteger(1);
    int month = input.getInteger(2);
    int day = input.getInteger(3);
    int hour = input.getInteger(4);
    int dayofyear = input.getInteger(5);
    int weekofyear = input.getInteger(6);
    int productcount = input.getInteger(7);

    /*
     * Inserting Values In Cassandra
     */

    String insertUpdateTable = "UPDATE TopQuery SET count = count + 1 "
            + "where term = \'" + term + "\' AND year = " + year
            + " AND month = " + month + " AND day = " + day
            + " AND hour = " + hour + " AND dayofyear = " + dayofyear
            + " AND weekofyear = " + weekofyear + " AND productcount = "
            + productcount + ";";

    session.executeAsync(insertUpdateTable);

}

在这里,我将获得所有值,例如termyearmonthdayhourdayofyear,{ {1}}和weekofyear在运行时将其插入到Cassandra DB中。

因为,这个String是常量而且没有改变我想把它分开并把它放到属性文件中。

我已经将表结构放入属性文件并从那里获取它。所以,以后如果我需要更改表结构我不必编辑代码,而是我可以简单地编辑属性文件

但是,如何将productcount存储在属性文件中,以便它在运行时读取数据并更新数据库?

2 个答案:

答案 0 :(得分:0)

要阅读属性文件,您可以使用Properties类。

  

Properties类表示一组持久的属性。该   属性可以保存到流中或从流中加载。每把钥匙   并且它在属性列表中的相应值是一个字符串。

但我不确定将SQL(或其他代码)放入属性文件是否是个好主意。编辑java或属性文件以更改SQL没有区别。毕竟它是一个需要测试和部署的代码更改。在每个非平凡的项目中,您都希望自动执行此步骤(ant,maven,...)。

答案 1 :(得分:0)

在xyz.properties中放置像这样的值

query = *************

现在试试这个

getProperties(){
                Properties prop = new Properties();
                String propFileName = "xyz.properties";

                inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

                prop.load(inputStream);
                String query= prop.getProperty("query");
              }