Reading an Object from environment.getProperty() - java

I am trying to read from environment.getProperty(). I am able to read to pass in a key and retrive, but now i am trying to read an object(so as to make the code generic).
So environment values are loaded from a JSON.
say:
{name:"xyz",class:"12"}
so when I pass in environment.getProperty("name") I get back xyz.
But I want to read something of this nature.
{student:[student1:{name:"xyz",class"12"}, student2:{name:"abc",class:"11"}]}
I tried using environment.getProperty("student",List.class) but that didn't resolve this.
Any comments?

Related

Validate JSON before further processing

I have the following csv file (In production the number of records can range from 20k-100k and has many fields )
id,firstname,lastname,email,profession
100,Betta,Wandie,Betta.Wandie#gmail.com,developer
101,Janey,Firmin,Janey.Firmin#gmail.com,doctor
I need to convert this to json and do further processing.
CSV->JSON->PROCESS FURTHER
.I am able to convert it to JSON directly using the code given here
directly convert CSV file to JSON file using the Jackson library
But i want do validations for json like if lastname has null value then ignore that record or id is missing then ignore that record.
How can i handle the validation?I am using Java 8 and spring boot latest version
I have done something similar by using JavaScript (Nashorn). Yes, that is nasty, but it works, and it is astonishingly fast!
Unfortunately, I do not have the source code at hand …
Why I did it that way had the same reasons as #chrylis-on strike implies in their comment: the validation is much easier if you have an object for each JSON record. But as I was lazy, and there was definitely no need for the Java object I had to create for this, I had the idea with the JavaScript script inside my Java program.
Basically, a JSON String is the source for a JavaScript program; you can assign it directly to a JavaScript variable and then you can access the records as array elements and the fields by their name. So your JavaScript code walks through the records and drops those that do not match your validation rules.
Now the Java part: the keyword here is JSR223; it is an API that allows you to execute scripts inside your Java environment. In your case, you have to provide the converted JSON in the context, then you start the script that writes the modified JSON back to the context.
If the converted JSON is too large, you can even use the same technique to check record by record; if you compile the script, it is nearly as fast as native Java.
You can even omit Jackson and let JavaScript do the conversion …
Sorry that I cannot provide code samples; I will try to get hold on them and add them if I get them.

Building JSON url String

This is going to be a pretty ridiculous question so brace yourselves.
I need to build an URL string which will update some status variables in a website.
Basically, since the structure of the URL must be in JSON's structure, I'm having a lot of trouble getting variables in the middle of the string.
"http://mywebsite.com/index.php?data={\"Number\":\"1234567890001\",\"TS\":\"15/11/15%2008:08:31:44\",\"SER\":\"53543D303B44723D4E616F\"}"
Basically, I need to replace "1234567890001\" for the variable numberX and "53543D303B44723D4E616F\" for the variable serX.
Can someone lend me a hand?
I've tried everything with +'s & company but I'm not being able to pull it up.
Thanks!
Firstly, It's hard to understand why you are not using GET parameter. It will be a lot more easy to get all parameters
However, if you want to stay with this approach, you can simply get your data value and make a replace, like : replace("\\","")
This will provide a String similar to this : {"Number":"1234567890001","TS":"15/11/15%2008:08:31:44","SER":"53543D303B44723D4E616F"}
And then, you can simply parse it using JSON Library like GSON.

How we can have access to an array into a mapper?

I'm totally new in MapReduce programming and in my first MR code, I have this question. In my mapper, I need to have access to a 2D array that has been created and filled before the mapper in the main class. How can I have access to it? Should I export it into txt and try to read it in the mapper? If so, how should in insert it into mapper? I have no idea how should I make it available? My code is in Java.
You can do this couple of ways.
After you created the 2D array, you can load this file into HDFS and then use DistributedCache in Java M/R API to access this data in your mapper/reducer code. Take a look at this: http://developer.yahoo.com/hadoop/tutorial/module5.html
If your data is not too large and you have an object that represents this data which is serializable and quite small you can pass it along via the job Configuration. Serialize it and include a base64 encoded version of it in the Configuration. Then you can access this data in mapper/reducer: http://hadoop.apache.org/docs/stable/api/org/apache/hadoop/conf/Configuration.html#set(java.lang.String, java.lang.String)

Android - How to Store HashMap with Custom Key (and custom data) to internal storage?

I was wondering if it was possible to store a hash map with a special key function (similar to the solution posted by Jon Skeet at Using a byte array as Map key) and thus data wrapper inside the android internal storage
And how to get them out again.
Namely, the data underlying it all is char [], but that char [] is wrapped around in this custom class that is used in the hashmap.
The value part is simple string, but the key is the important bit where I need the data inside it to be preserved on each opening of the app.
Do I need to overwrite certain functions in the wrapper to make sure it works with the FileOutputStream? How do I import it back again?
Use Gson to convert the map to a JSON string. Then write that string to disk. To get it back, use Gson again. Prob could accomplish this 5 lines of code. No messing with convertors or parsing needed.
I don't have a direct answer for you. However I can suggest simply saving your hashmap as an xml file. Writing the xml is pretty easy. You will need to write the higher level parts of parsing to read it back.

How can a value be stored before the jvm exits?

When my program exits I want to store an integer value that is loaded again when theh program starts.
Is serialization an option?
You have following options:
Serialize and store the value in a file and read it when you restart the application.
Store it in DB before closing app and reload when restarting the app.
Yes you need to look into serialization.
Serialization helps you write the state of the object in a file
Use deserialization when you want to read the state of the object.
Serialization is definitely an option. In the init of your class having the variable, you have to load the value from somewhere (server, file, etc.). Since its that simple (one int) I suggest to load it from the file or Properties.
Take a look at this Properties example:
http://www.mkyong.com/java/java-properties-file-examples/
So, you want a variable to retain its value between program executions. Save it to a file at program exit and attempt to read it from the same file at program startup (or initialize it to a default value if the file isn't there).
You could store it to a .txt file and have it read it back in and set the variable.

Categories

Resources