簡體   English   中英

在運行時從屬性文件讀取值

[英]Reading Value from property file at runtime

我有以下功能:

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);

}

在這里,我將在運行時獲取諸如termyearmonthdaydayhourdayofyearweekofyearproductcount類的所有值,並將其插入Cassandra DB中。

由於此字符串是常量,因此不會更改,因此我想將其分離出來並放入屬性文件中。

我已經將表結構放入屬性文件中並從那里獲取它。這樣,將來如果我需要更改表結構,則不必編輯代碼,而只需編輯屬性文件即可。

但是,如何將insertUpdateTable存儲在屬性文件中,以便它在運行時讀取數據並更新DB?

要讀取屬性文件,可以使用Properties類。

Properties類表示一組持久的屬性。 可以將屬性保存到流或從流加載。 屬性列表中的每個鍵及其對應的值都是一個字符串。

但是我不確定將SQL(或其他代碼)放入屬性文件是否是一個好主意。 編輯Java或屬性文件以更改SQL沒什么區別。 畢竟,這是代碼更改,需要測試和部署。 無論如何,在每個不重要的項目中,您都想自動執行此步驟(ant,maven等)。

在xyz.properties中將這樣的值

查詢= *************

現在嘗試這個

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM