Support Questions

Find answers, ask questions, and share your expertise

Read # symbol in config.properties

avatar
Contributor

I want to use # symbol in config.properties .

I know that # is used for comment in config.properties but I want to use it as a part of keyword.

eg:

#google=google

#microsoft=microsoft

I need this for getting tweets having hashtag(#)of google.

Is this possible? If yes then how and if no then why?

Thanks in advance.

1 ACCEPTED SOLUTION

avatar
Contributor

Its working properly I have applied this logic

if (readConfiguration.getInstance() .getPropertyMap() .containsKey(word.replace("#", ""))) { String companyName = readConfiguration .getPropertyMap().get( word.replace("#", ""));

Thanks.

View solution in original post

6 REPLIES 6

avatar
Super Guru
@radhika mantri

instead of reading # keys from properties file why don you put null check in your producer or consumer and set this value there like this

if (properties.getProperty("#google") == null) {
    properties.setProperty("#google", "google");
}

avatar
Contributor

@Rajkumar Singh

config.properties

yahoo=yahoo google=google

#google=google(This is actually what I want to do but I know its comment in config.properties file)

Read configuration.class

public class ReadConfiguration implements Serializable {

private static ReadConfiguration readConfiguration; private static HashMap<String, String> propertyMap;

public static HashMap<String, String> getPropertyMap() {

return propertyMap; }

private ReadConfiguration() { }

public static synchronized ReadConfiguration getInstance() { if (readConfiguration == null) { readConfiguration = new ReadConfiguration(); propertyMap = readConfiguration .readConfiguration("/configuration.properties"); } return readConfiguration;

}

public HashMap<String, String> readConfiguration(String path) throws NullPointerException { HashMap<String, String> propertyMap = new HashMap<String, String>();

InputStream input = null;

try {

input = ReadConfiguration.class.getResourceAsStream(path);// /configuration

Properties properties = new Properties();

properties.load(input);

Set<String> propertyNames = properties.stringPropertyNames();

for (String Property : propertyNames) {

Enumeration<?> e = properties.propertyNames();

while (e.hasMoreElements()) { String key = (String) e.nextElement();

String value = properties.getProperty(key);

propertyMap.put(Property, properties.getProperty(Property)); } } } catch (IOException ex) { ex.printStackTrace(); } return propertyMap; } }

kafka.class

if (nullHandler.checkNull(tweetAsMap .get(TwitterProcessingPipelineConstants.TEXT)))

{ String tweetTextAsString = tweetAsMap.get( "text").toString();

tweetBean.setText(tweetTextAsString); logger.info("The Text --> " + tweetTextAsString);

for (String word : tweetTextAsString .toLowerCase().split(" "))

{ if (readConfiguration.getInstance() .getPropertyMap().containsKey(word))

{ String companyName = readConfiguration .getPropertyMap().get(word);//

tweetBean.setCompanyName(companyName);

break;

} }

This is what we have done in read config class we are taking company name from config.properties without any special character we are able to get the desired output.But as mentioned above for # how I can get the desired output.

Thanks in advance.

avatar
Super Guru

how many of this type(#google=google) of property you are going to have in your applicaiton, if these are fews then you can put in your ReadConfiguration while building propertyMap.

avatar
Contributor

for almost 50 different companies I have to use it eg:#google=google,#yahoo=yahoo and so on

avatar
Contributor

Its working properly I have applied this logic

if (readConfiguration.getInstance() .getPropertyMap() .containsKey(word.replace("#", ""))) { String companyName = readConfiguration .getPropertyMap().get( word.replace("#", ""));

Thanks.

avatar
Contributor

if (readConfiguration.getInstance() .getPropertyMap() .containsKey(word.replace("#", ""))) { String companyName = readConfiguration .getPropertyMap().get( word.replace("#", ""));

This logic is been implemented and getting the desired output.

Thanks.