I need to make a simple java application, and now I am working on the architecture for this. Please, help me to build way to make my app. I only need advice on how to make this (what classes I need, what methods to include), but the code I will write myself. If it is not difficult for you, please write your opinion about the best and right way to make my app. Thanks!
My technical task below:
Given: TEST table in any database (use in memory databases are not recommended), containing one integer column (FIELD).
You must write a console application in Java, using standard library JDK7 (preferably) or JDK8 and implements the following functionality:
The main application class must follow the rules of JavaBean, that is initialized through the setters. Initialization parameters - the data to connect to the database and the number N.
Upon launch, the application inserts a TEST N records with values 1..N. If the TEST table were recording, they are removed before inserting.
The application then requests the data from TEST.FIELD and generates the correct XML-type document
<entries>
<Entry>
<Field> field to field </ field>
</ Entry>
...
<Entry>
<Field> field to field </ field>
</ Entry>
</ entries>
(with N nested elements ) The document is saved in the file system as "1.xml".
By means of XSLT, the application converts the contents of the "1.xml" to the
following form:
< entries> < entry> < field>value of field 'field' < /entry> ...
< entry> < field>value of field 'field' < /entry>
(with N nested elements ) The new document is saved in the file system as "2.xml".
The application parses "2.xml" and outputs the arithmetic sum of the values of all the attributes field in the console.
For large N (~ 1,000,000) while the application should not be more than five minutes.
Related
I have a task to do. Just for practice. Would you please give ma any advises how to start it ( I am not begging for a code :) )
What should I do? Any suggestions, hints?
Import list of files csv, xml.
Every order I have to write in (in-memory) database
The order should have fields like: Client_Id, Name, Price etc....
Program has to generate reports e.g.: quantity of orders, average amount of order, total amount of orders etc.
Every raport can be displayed or written to (any) file
Database can not be divided between every start-up
Incorrect lines in order are ignored but the information about bad format is displayed.
the csv and xml files contains e.g.:
>Client_id,Other_id,Name,Amount
> 1,1,Joe,25,
> 1,2,Mike,34
> 2,2,Maria,10
> 2,1,Elizabeth,5
>
> xml <reuqests> <request>
> <client_id>1</client_Id>
> <name>Joe</name> etc.......
> </request>
> </requests>
What I might know is:
1. Import csv using csvopen. Sounds easy but, do I need to import them separately or can I use the List
2/3 Can I create schema using hibernate?
4 ? I am not sure. Can I use "switch"?
5. What library can I use to write reports? (Can I use Jesper Reports?)
6/7 What does it mean?
I've a field:
When I put it in :
It does not get generated sequentially
Any reason why the sequence numbers are not in order? I'll add the logs in 2 seperate posts because they are too big for this post.
1:1422624487.413000 [s~server-test-killesk/12.381882801386260762].<stdout>: 13:28:07,413 DEBUG Request FC14E205 ValueGeneration:debug:58 - Creating ValueGenerator instance of "com.google.appengine.datanucleus.valuegenerator.SequenceGenerator" for "com.eurekaapp.server.dao.mappedobjects.DAOJobEvent.sequence"
1:1422624487.424000 [s~server-test-killesk/12.381882801386260762].<stdout>: 13:28:07,423 DEBUG Request FC14E205 ValueGeneration:debug:58 - Generated value for field "com.eurekaapp.server.dao.mappedobjects.DAOJobEvent.sequence" using strategy="sequence" (Generator="com.google.appengine.datanucleus.valuegenerator.SequenceGenerator") : value=1,050,002
1:1422624487.624000 [s~server-test-killesk/12.381882801386260762].<stdout>: 13:28:07,624 DEBUG Request FC14E205 ValueGeneration:debug:58 - Generated value for field "com.eurekaapp.server.dao.mappedobjects.DAOJobEvent.sequence" using strategy="sequence" (Generator="com.google.appengine.datanucleus.valuegenerator.SequenceGenerator") : value=1,040,003
1:1422624487.908000 [s~server-test-killesk/12.381882801386260762].<stdout>: 13:28:07,907 DEBUG Request FC14E205 ValueGeneration:debug:58 - Generated value for field "com.eurekaapp.server.dao.mappedobjects.DAOJobEvent.sequence" using strategy="sequence" (Generator="com.google.appengine.datanucleus.valuegenerator.SequenceGenerator") : value=3,010,003
IdGeneratorStrategy.SEQUENCE is implemented on top of DatastoreService.allocateIds() which is how Cloud Datastore internally assigns Ids. Everything that applies to Cloud Datastore auto-Id allocation applies to SEQUENCE.
Sequences are only guaranteed to be unique, not monotonically increasing.
I'm trying to convert a large Map> to some JavaBean. The key of map corresponds to some property of JavaBean, and the value somehow is decoded to property value. So I decided to use some util for that, but don't know what will work. There are some requirements I have to this util (or framework):
all configuration must be in separate files
should be a possibility to map dynamic quantity of keys:
there is a map:
key | value
quan | n
key_1| value_1
key_2| value_2
........ | .............
key_n| value_n
where n - is any number
and the JavaBean has a List of some beans. They have a property. value_1, value_2, ... must be mapped in this property, and in the end there must be so much beans in list, as these keys and values in map.
3.. should be a possibility to set up custom decoder for property mapping, because in most cases the value in map is a List with 1 value, so I need to get the first item of list (if it's not empty).
4.. should be a possibility run some script to execute extraordinary mappings, for example:
there is a map, that is described in 2d point.
and the JavaBean has a property of type HashMap, where value_1 is mapped to Bean1 and some analogous value from input map is mapped to Bean2.
I've tried to use smooks, but when I've started, all these requirements were not clear yet and the smooks was something new, I haven't worked with it until now. So the smooks config doesn't contain the whole business-logic (because of second req.) and looks ugly, I don't like that. I can show the most ugliest fragment for 2d point:
<jb:bean beanId="javaBean" class="com.example.JavaBean" createOnElement="map">
<jb:wiring property="someBeans" beanIdRef="someBeanItems"/>
</jb:bean>
<jb:bean beanId="someBeanItems" class="java.util.ArrayList" createOnElement="map/entry">
<jb:wiring beanIdRef="someBeanItem"/>
</jb:bean>
<jb:bean beanId="someBeanItem" class="com.example.someBeanItem" createOnElement="map/entry">
<condition>map.quan[0]>0</condition>
<jb:expression property="property1">
index = map.quan[0]-1;
value = additionalProperties.property1_List[index];
map.quan[0] = map.quan[0] - 1;
return value;
</jb:expression>
</jb:bean>
Here "property1_List" is builded before executing smooks.
Now I look for something more nice and need your help: maybe you know how to make that better using smooks? Or what another frameworks for mapping can you recommend for my issue?
I would like to add entities to documents like you can do with the data-config.
At the moment I'm indexing every page of my documents as a single document.
Now :
<solrDoc>
<id>1</id>
<docname>test.pdf</docmname>
<pagenumber>1</pagenumber>
<pagecontent>blablabla</pagecontent>
</solrDoc>
<solrDoc>
<id>2</id>
<docname>test.pdf</docmname>
<pagenumber>2</pagenumber>
<pagecontent>blablabla</pagecontent>
</solrDoc>
As you can see the data related to the document is stored x pages times. I would like to get documents like this:
<doc>
<id>1</id>
<docname>test.pdf</docmname>
<pageEntries> //multivaluefield
<pageEntry><pagenumber>1</pagenumber><pagecontent>blablabla</pagecontent></pageEntry>
<pageEntry><pagenumber>2</pagenumber><pagecontent>blablabla</pagecontent></pageEntry>
</pageEntries>
</doc>
I don't know how to make something like pageEntry. I saw that solr can import entities from databases but I'm wondering how I can do the same? (or something similar)
I'm using solr 3.6.1. The page extraction is done by myself using pdfbox.
Java code:
SolrInputDocument solrDoc = new SolrInputDocument();
solrDoc.setField("id", 1);
solrDoc.setField("filename", "test");
for (int p : pages) {
solrDoc.addField("page", p);
}
for (String pc : pagecont) {
solrDoc.addField("pagecont", pc);
}
If the extraction is performed by you, you can club all the pages and feed it as a single Solr document with the pagenumber & pagecontent being multivalued fields.
You can use the same id for all the pages (with the id not being a primary field in the schema definition) and use Grouping (Field Collapsing) to group the results for the documents.
I have a XML file with many copies of table node structure as below:
<databasetable TblID=”123” TblName=”Department1_mailbox”>
<SelectColumns>
<Slno>dept1_slno</Slno>
<To>dept1_to</To>
<From>dept1_from</From>
<Subject>dept1_sub</Subject>
<Body>dept1_body</Body>
<BCC>dept1_BCC</BCC>
<CC>dept1_CC</CC>
</SelectColumns>
<WhereCondition>MailSentStatus=’New’</WhereCondition>
<UpdateSuccess>
<MailSentStatus>’Yes’</MailSentStatus>
<MailSentFailedReason>’Mail Sent Successfully’</MailSentFailedReason>
</UpdateSuccess>
<UpdateFailure>
<MailSentStatus>’No’</MailSentStatus>
<MailSentFailedReason>’Mail Sending Failed ’</MailSentFailedReason>
</ UpdateFailure>
</databasetable>
As it is not an efficient manner to traverse the file for each time to fetch the details of each node for the queries in the program, I used the nested hashmap concept to store the details while traversing the XML file for the first time. The structure I used is as below:
MapMaster
Key Value
123 MapDetails
Key Value
TblName Department1_mailbox
SelectColumns mapSelect
Key Value
Slno dept1_slno
To dept1_to
From dept1_from
Subject dept1_sub
Body dept1_body
BCC dept1_BCC
CC dept1_CC
WhereCondition MailSentStatus=’New’
UpdateSuccess mapUS
MailSentStatus ’Yes’
MailSentFailedReason ’Mail Sent Successfully’
UpdateFailure mapUF
MailSentStatus ’No’
MailSentFailedReason ’Mail Sending Failed’
But the problem I’m facing now is regarding retrieving the Value part using the nested Keys. For example,
If I need the value of Slno Key, I have to specify TblID, SelectColumns, Slno in nested form like:
Stirng Slno = ((HashMap)((HashMap)mapMaster.get(“123”))mapDetails.get(“SelectColumns”))mapSelect.get(“Slno”);
This is unconvinent to use in a program. Please suggest a solution but don’t tell that iterators are available. As I’ve to fetch the individual value from the map according to the need of my program.
EDIT:my program has to fetch the IDs of the department for which there is privilege to send mails and then these IDs are compared with the IDs in XML file. Only information of those IDs are fetched from XML which returned true in comparison. This is all my program. Please help.
Thanks in advance,
Vishu
Never cast to specific Map implementation. Better use casting to Map interface, i.e.
((Map)one.get("foo")).get("bar")
Do not use casting in your case. You can define collection using generics, so compiler will do work for you:
Map<String, Map> one = new HashMap<String, Map>();
Map<String, Integer> two = new HashMap<String, Integer>();
Now your can say:
int n = one.get("foo").get("bar");
No casting, no problems.
But the better solution is not to use nested tables at all. Create your custom classes like SelectColumns, WhereCondition etc. Each class should have appropriate private fields, getters and setters. Now parse your XML creating instance of these classes. And then use getters to traverse the data structure.
BTW if you wish to use JAXB you do not have to do almost anything! Something like the following:
Unmarshaller u = JAXBContext.newInstance(SelectColumns.class, WhereCondition.class).createUnmarshaller();
SelectColumns[] columns = (SelectColumns[])u.unmarshal(in);
One approach to take would be to generate fully qualified keys that contain the XML path to the element or attribute. These keys would be unique, stored in a single hashmap and get you to the element quickly.
Your code would simply have to generate a unique textual representation of the path and store and retrieve the xml element based on the key.