Apache Beam configuration from environment variables - java

I was surprised to find that there doesn't seem to be a way to pull Beam configuration from Environment Variables (for the direct runner). In case it helps anyone, I've create the code snippet below as an answer - but I was wondering if there was a better (or more official) variant? This definitely feels hacky...

private static String[] AddArgsFromEnvironmentVariables(String[] args) {
ArrayList<String> argsWithEnvVariables = new ArrayList<String>(Arrays.asList(args));
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
if (envName.startsWith("BEAM_")) {
String argName = envName.substring(5);
argsWithEnvVariables.add(0, "--" + argName + "=" + env.get(envName));
}
}
return argsWithEnvVariables.toArray(args);
}
How to use:
PipelineOptionsFactory.fromArgs(AddArgsFromEnvironmentVariables(args))

Related

Pattern from String

i want to extract pattern from a string for ex:
string x== "1234567 - israel.ekpo#massivelogdata.net cc55ZZ35 1789 Hello Grok";
pattern its should generate is = "%{EMAIL:username} %{USERNAME:password} %{INT:yearOfBirth}"
basically i want to create patter for logs generated in the java application. any idea how to do that ?
In past i've do some with reguar expression, but in my case the string having ever the same composition pattern or order.
I this case, you can done 3 matching pattern and make the find operation 3 times in order of pattern.
If not so, you must use an text analyzer or search tool.
It's recommended to use grow library to extract data from logs.
Example:
public final class GrokStage {
private static final void displayResults(final Map<String, String> results) {
if (results != null) {
for(Map.Entry<String, String> entry : results.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}
}
public static void main(String[] args) {
final String rawDataLine1 = "1234567 - israel.ekpo#massivelogdata.net cc55ZZ35 1789 Hello Grok";
final String expression = "%{EMAIL:username} %{USERNAME:password} %{INT:yearOfBirth}";
final GrokDictionary dictionary = new GrokDictionary();
// Load the built-in dictionaries
dictionary.addBuiltInDictionaries();
// Resolve all expressions loaded
dictionary.bind();
// Take a look at how many expressions have been loaded
System.out.println("Dictionary Size: " + dictionary.getDictionarySize());
Grok compiledPattern = dictionary.compileExpression(expression);
displayResults(compiledPattern.extractNamedGroups(rawDataLine1));
}
}
Output:
username=israel.ekpo#massivelogdata.net
password=cc55ZZ35
yearOfBirth=1789
Note:
This are the patterns used before:
EMAIL %{\S+}#%{\b\w+\b}\.%{[a-zA-Z]+}
USERNAME [a-zA-Z0-9._-]+
INT (?:[+-]?(?:[0-9]+))
More info about grok-patterns: BuiltInDictionary.java

How get active directory domain name in java

Tell me please
How can i get active directory domain name from java
I tried this
System.out.println(System.getenv("USERDOMAIN"));
but I only get the name of the computer
======================
I did so
InetAddress inet = InetAddress.getLocalHost();
InetAddress[] ips = InetAddress.getAllByName(inet.getCanonicalHostName());
usernameId.setText(System.getProperty("user.name"));
if (ips != null) {
for (int i = 0; i < ips.length; i++) {
String[] str = ips[i].toString().split("/");
if (!(str[1].startsWith("169") || str[1].contains(":")))
System.out.println("Computer name: " + str[0] + "\nIp address: " + str[1]);
computernameId.setText(str[0]);
And i get ip address and computername.domainname
Try using
System.out.println(System.getenv("USERDNSDOMAIN"));
If that does not work, you can (as James Tanner said) try parsing through your system variables to find the one you want:
Map<String, String> envMap = System.getenv();
Iterator iter = envMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> pair = (Map.Entry<String, String>)iter.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
From this article, try checking the DomainName environment variable.
Or, from this question, try the LOGONSERVERvariable.
If that doesn't work, I'd recommend taking a look at your environment variables directly (directions vary depending on which version of Windows you're running) to find the one that actually contains the information you're looking for, then use that one.

Aspect breaking bytecode on specific class

I'm new to AOP, I've created an aspect to trace all methods or classes marked with #Trace annotation. I'm using compile time weaving. (Java 8, Aspectj 1.8, Spring 4)
TraceAspect.java
#Aspect
public class TraceAspect {
private static Map<String, Integer> threadMap = new HashMap<>();
#Pointcut("#within(Trace) || #annotation(Trace)")
void annotated(){}
#Around("annotated() && execution(* *(..))")
public Object trace(final ProceedingJoinPoint joinPoint) throws Throwable {
String threadName = Thread.currentThread().getName();
String indent = indent(inThread(threadName));
System.out.println(threadName + " : " + indent + "-> " + joinPoint.getSignature().toString());
long start = System.nanoTime();
Object ret = joinPoint.proceed();
long end = System.nanoTime();
System.out.println(threadName + " : " + indent + "<- " + joinPoint.getSignature().toString() + " ended (took " + (end - start) + " nanoseconds)");
outThread(threadName);
return ret;
}
private String indent(int depth) {
String result = "";
for (int index = 0; index < depth; index++) {
result += " ";
}
return result;
}
private int inThread(String threadName) {
if (threadMap.get(threadName) == null) {
threadMap.put(threadName, 0);
}
int stackDepth = threadMap.get(threadName) + 1;
threadMap.put(threadName, stackDepth);
return stackDepth;
}
private void outThread(String threadName) {
int stackDepth = threadMap.get(threadName) - 1;
threadMap.put(threadName, stackDepth);
}
}
The CryptsyExchange.java (which is a Spring Bean) when marked with #Trace, classloader throws ClassFormat error on build(..) method while initializing that bean in application context...
CryptsyExchange.java
#Trace
public class CryptsyExchange {
private static final Logger LOGGER = LoggerFactory.getLogger(CryptsyExchange.class);
private DataService dataService;
private Configuration config;
private Converter converter;
private Exchange exchange;
private List<CryptsyAccount> accounts = Collections.synchronizedList(new LinkedList<>());
private CryptsyAccount defaultAccount;
public static CryptsyExchange build(String name, DataService dataService, ConfigPathBuilder pathBuilder) {
condition(notNullOrEmpty(name) && notNull(dataService, pathBuilder));
CryptsyExchange cryptsyExchange = new CryptsyExchange();
cryptsyExchange.dataService = dataService;
// Loading configuration
final Configuration configuration = Configuration.load(pathBuilder.getExchangeConfigPath(name));
cryptsyExchange.config = configuration;
// Retrieve corresponding exchange from datastore
cryptsyExchange.exchange = dataService.registerExchange(cryptsyExchange.config.getString("exchange"));
// Get accounts from configuration
Map<String, Map<String, String>> accounts = configuration.getMap("accounts");
// Initialize accounts
accounts.entrySet().stream().forEach((entry) -> {
String key = entry.getKey();
Map<String, String> accountMap = entry.getValue();
// Retrieve corresponding datastore account
Account account = dataService.registerAccount(cryptsyExchange.exchange, key);
// Initialize cryptsy specific account
CryptsyAccount cryptsyAccount = new CryptsyAccount(account, accountMap.get("key"), accountMap.get("secret"));
cryptsyExchange.accounts.add(cryptsyAccount);
if (notNull(accountMap.get("isDefault")) && Boolean.valueOf(accountMap.get("isDefault"))) {
cryptsyExchange.defaultAccount = cryptsyAccount;
}
});
// Initializing Converter
cryptsyExchange.converter = cryptsyExchange.new Converter();
// Recover associations from configuration
Map<String, String> exchangeCurrencyToCurrency = configuration.getMap("exchangeCurrencyToCurrency");
Set<String> markedForRemoval = new HashSet<>();
exchangeCurrencyToCurrency.entrySet().stream().forEach((entry) -> {
String cryptsyCurrencyCode = entry.getKey();
String currencySymbol = entry.getValue();
com.jarvis.data.entity.Currency currency = dataService.getCurrency(currencySymbol);
if (notNull(currency)) {
cryptsyExchange.converter.associateCurrency(currency, cryptsyCurrencyCode);
} else {
LOGGER.debug("associated currency [" + currencySymbol + "] does not exist in database, removing from configuration");
markedForRemoval.add(cryptsyCurrencyCode);
}
});
// Removing currency associations missing from database
if (!markedForRemoval.isEmpty()) {
markedForRemoval.forEach((currency) -> configuration.remove("exchangeCurrencyToCurrency", currency));
}
Map<String, String> exchangeMarketToMarket = configuration.getMap("exchangeMarketToMarket");
markedForRemoval.clear();
exchangeMarketToMarket.entrySet().stream().forEach((entry) -> {
String cryptsyMarketId = entry.getKey();
String marketName = entry.getValue();
Market market = dataService.getMarket(marketName);
if (notNull(market)) {
cryptsyExchange.converter.associateMarket(market, Integer.valueOf(cryptsyMarketId));
} else {
LOGGER.debug("associated market [+" + marketName + "] does not exist, removing from configuration");
markedForRemoval.add(cryptsyMarketId);
}
});
// Removing market associations missing from database
if (!markedForRemoval.isEmpty()) {
markedForRemoval.forEach((market) -> configuration.remove("exchangeMarketToMarket", market));
}
// Update configuration
configuration.save();
return cryptsyExchange;
}
// Lot of other code there
}
And of course the stackTrace:
Exception in thread "main" java.lang.ClassFormatError: Illegal local variable table length 288 in method com.jarvis.exchange.cryptsy.CryptsyExchange.build_aroundBody0(Ljava/lang/String;Lcom/jarvis/data/service/DataService;Lcom/jarvis/util/ConfigPathBuilder;Lorg/aspectj/lang/JoinPoint;)Lcom/jarvis/exchange/cryptsy/CryptsyExchange;
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getDeclaredMethods(Class.java:1962)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:467)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:451)
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:663)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1396)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:382)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:353)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:82)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.jarvis.Jarvis.<clinit>(Jarvis.java:10)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
I've tried this on any other class on my project (annotation can be applied on Type or method) and it worked, but exactly with this bean build method I'm facing issues and can't find any workaround. Maybe current support for Java 8 by Aspectj is buggy and it actually corrupts bytecode. Or perhaps there is something wrong I've done there?
Some questions:
Do you use full-blown AspectJ or just Spring AOP with #AspectJ syntax?
Do you compile with source/target level 1.8 or maybe still 1.7?
Does the exact same code work with Java 7 and AspectJ 1.8.0?
If not, does it work with Java 7 and AspectJ 1.7.4?
Can you please provide a stand-alone minimal code sample reproducing the problem? Maybe even on GitHub with a Maven build?) Many of your referenced classes are invisible to me, so I cannot reproducte it.
AJ 1.8.0 is brandnew and some of its problems are actually caused by ECJ (Eclipse Java compiler). Some have already been fixed, so you could try a current developer build (select "Last Known Good developer build".
Update:
I was able to reproduce the problem with a small code sample, independent of any other classes. The problem is not annotations or simple forEach lambdas, but obviously nested forEach lambdas.
I filed an AspectJ bug on http://bugs.eclipse.org/bugs/show_bug.cgi?id=435446.
Update 2:
The bug ticket also describes how to work around the problem by excluding lambda calls from the pointcut.
Another workaround I just found is to run the JVM with parameter -noverify.
Update 3:
The bugfix is done and available as a development build. It will be part of the upcoming AspectJ 1.8.1.
I've found that the problem comes from lambda expression usage :(
replacing all lambdas with regular for fixes the problem
exchangeCurrencyToCurrency.entrySet().stream().forEach((entry) -> {
String cryptsyCurrencyCode = entry.getKey();
String currencySymbol = entry.getValue();
com.jarvis.data.entity.Currency currency = dataService.getCurrency(currencySymbol);
if (notNull(currency)) {
associateCurrency(currency, cryptsyCurrencyCode);
} else {
LOGGER.debug("associated currency [" + currencySymbol + "] does not exist in database, removing from configuration");
markedForRemoval.add(cryptsyCurrencyCode);
}
});
result
for(Map.Entry<String, String> entry : exchangeCurrencyToCurrency.entrySet()){
String cryptsyCurrencyCode = entry.getKey();
String currencySymbol = entry.getValue();
com.jarvis.data.entity.Currency currency = dataService.getCurrency(currencySymbol);
if (notNull(currency)) {
associateCurrency(currency, cryptsyCurrencyCode);
} else {
LOGGER.debug("associated currency [" + currencySymbol + "] does not exist in database, removing from configuration");
markedForRemoval.add(cryptsyCurrencyCode);
}
}

Hash of Hashes of Arrays in Java

I'm trying to implement Hash of Hashes of Arrays in Java and thought it would be nice if I will use anonymous blah blah(i forgot the exact term/I dont know how to call it).
HashMap<String, HashMap<String, String[]>> teams =
new HashMap<String, HashMap<String, String[]>>(){{
put("east", new HashMap<String, String[]>(){{
put("atlantic", new String[] { "bkn", "bos", "phi","tor", "ny" });
put("central", new String[] { "chi", "cle", "det", "ind", "mil" });
put("southeast", new String[] { "atl", "cha", "mia", "orl", "wsh" });
}});
put("west", new HashMap<String, String[]>(){{
put("northwest", new String[] { "den", "min", "okc", "por", "utah" });
put("pacific", new String[] { "gs", "lac", "lal", "phx", "sac" });
put("southwest", new String[] { "dal", "hou", "mem", "no", "sa" });
}});
}};
My question is if there is a another way to implement taking readability into consideration or completely perhaps completely change the implementation?
I know java is not the right tool but my boss told me to do so.
Also, please let me know of the right term. TIA
As long as we're not caring about run speed, why not use a language designed to express tiered data structures like JSON? JAVA has great external library support for it ...
Gson to the rescue!
#SuppressWarnings("unchecked")
HashMap teams =
new Gson().fromJson(
"{'east' : { 'atlantic' : ['bkn', 'bos', 'phi','tor', 'ny']," +
" 'central' : ['chi', 'cle', 'det', 'ind', 'mil']," +
" 'southeast' : ['atl', 'cha', 'mia', 'orl', 'wsh']}," +
" 'west' : { 'northwest' : ['den', 'min', 'okc', 'por', 'utah']," +
" 'pacific' : ['gs', 'lac', 'lal', 'phx', 'sac']," +
" 'southwest' : ['dal', 'hou', 'mem', 'no', 'sa']}}",
HashMap.class
);
http://code.google.com/p/google-gson/
Using a helper method
private void addTeams(String area, String codes) {
String[] areas = area.split("/");
Map<String, String[]> map = teams.get(areas[0]);
if (map == null) teams.put(areas[0], map = new HashMap<String, String[]>());
map.put(areas[1], codes.split(", ?"));
}
Map<String, Map<String, String[]>> teams = new HashMap<String, Map<String, String[]>>();{
addTeams("east/atlantic", "bkn, bos, phi, tor, ny");
addTeams("east/central", "chi, cle, det, ind, mil");
addTeams("east/southeast", "atl, cha, mia, orl, wsh");
addTeams("west/northwest", "den, min, okc, por, utah");
addTeams("west/pacific", "gs, lac, lal, phx, sac");
addTeams("west.southwest", "dal, hou, mem, no, sa");
}
You can replace
new String[] { "bkn", "bos", "phi","tor", "ny" }
with
"bkn,bos,phi,tor,ny".split(",");

ini4j - How to get all the key names in a setting?

I've decided to use ini file to store simple key-value pair configuration for my Java application.
I googled and searched stackoverflow and found that ini4j is highly recommended for parsing and interpreting ini files in Java. I spent some time reading the tutorial on ini4j site; however, I was not sure how to get all the key values for a setting in an ini file.
For instance, if I have a ini file like this:
[ food ]
name=steak
type=american
price=20.00
[ school ]
dept=cse
year=2
major=computer_science
and assume that I do not know names of keys ahead of time. How do I get the list of keys so that I can eventually retrieve the values according to keys? For instance, I would get an array or some kind of data structure that contains 'name', 'type', and 'price' if I get a list of keys for food.
Can someone show me an example where you would open an ini file, parse or interpret it so that an app knows all the structure and values of the ini file, and get the list of keys and values?
No guarantees on this one. Made it up in 5min.
But it reads the ini you provided without further knowledge of the ini itself (beside the knowledge that it consists of a number of sections each with a number of options.
Guess you will have to figure out the rest yourself.
import org.ini4j.Ini;
import org.ini4j.Profile.Section;
import java.io.FileReader;
public class Test {
public static void main(String[] args) throws Exception {
Ini ini = new Ini(new FileReader("test.ini"));
System.out.println("Number of sections: "+ini.size()+"\n");
for (String sectionName: ini.keySet()) {
System.out.println("["+sectionName+"]");
Section section = ini.get(sectionName);
for (String optionKey: section.keySet()) {
System.out.println("\t"+optionKey+"="+section.get(optionKey));
}
}
}
}
Check out ini4j Samples and ini4j Tutorials too. As often a not very well documented library.
I couldn't find anything in the tutorials so I stepped through the source, until I found the entrySet method. With that you can do this:
Wini ini = new Wini(new File(...));
Set<Entry<String, Section>> sections = ini.entrySet(); /* !!! */
for (Entry<String, Section> e : sections) {
Section section = e.getValue();
System.out.println("[" + section.getName() + "]");
Set<Entry<String, String>> values = section.entrySet(); /* !!! */
for (Entry<String, String> e2 : values) {
System.out.println(e2.getKey() + " = " + e2.getValue());
}
}
This code essentially re-prints the .ini file to the console. Your sample file would produce this output: (the order may vary)
[food]
name = steak
type = american
price = 20.00
[school]
dept = cse
year = 2
major = computer_science
The methods of interest are get() and keySet()
Wini myIni = new Wini (new File ("test.ini"));
// list section names
for (String sName : myIni.keySet()) {
System.out.println(sName);
}
// check for a section, section name is case sensitive
boolean haveFoodParameters = myIni.keySet().contains("food");
// list name value pairs within a specific section
for (String name : myIni.get("food").keySet() {
System.out.println (name + " = " + myIni.get("food", name)
}
In Kotlin:
val ini = Wini(File(iniPath))
Timber.e("Read value:${ini}")
println("Number of sections: "+ini.size+"\n");
for (sectionName in ini.keys) {
println("[$sectionName]")
val section: Profile.Section? = ini[sectionName]
if (section != null) {
for (optionKey in section.keys) {
println("\t" + optionKey + "=" + section[optionKey])
}
}
}

Categories

Resources