I study xstream these days.
But I found the xstream json tutorial which in its homepage is very simple.
I have an array as follows:
{
"mails":[
{
"uid":"ZC2027-mXOmcAtkfiztS0sEeJlkU25",
"relatedCardNums":"8299,0000,1531|8299,0000,1531",
"houseHolder":"",
"subject":"no-subject",
"receiveTime":"2012-05-27 00:00:00",
"bankName":"上海银行",
"cards":[]
}
],
"dealedNum":330,
"nextRequestDelay":"1",
"percent":"0",
"filterNum":410,
"resCode":"01",
"dealedBillNum":43,
"resMsg":"正在解析"
}
I want to convert this json string to a GetMailsDataResponseDto, but I dont know how to do?
Could you help me out?
package com.fund.etrading.ebankapp.base.credit.cardniu.ecardniu.dto;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fund.etrading.ebankapp.base.credit.utils.FileUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class GetMailsDataResponseDto extends ResponseBaseDto{
protected int dealedNum;
protected String nextRequestDelay;
protected String percent;
protected int filterNum;
protected int dealedBillNum;
protected List mails = new ArrayList();
public List getMails() {
return mails;
}
public int getDealedNum() {
return dealedNum;
}
public String getNextRequestDelay() {
return nextRequestDelay;
}
public String getPercent() {
return percent;
}
public int getFilterNum() {
return filterNum;
}
public int getDealedBillNum() {
return dealedBillNum;
}
public void fromJson(String json){
try {
json = FileUtils.get_content("C:\\Documents and Settings\\Administrator\\workspace\\99fund_java\\src\\com\\fund\\etrading\\ebankapp\\base\\credit\\新建 文本文档 (2).txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
json = "{\"root\":" + json + "}";
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.alias("root", this.getClass());
//xstream.addImplicitCollection(this.getClass(), "mails");
xstream.alias("mail", MailDto.class);
//xstream.aliasField("cards", MailDto.class, "cards");
//xstream.aliasField("currencyData", CardDto.class, "currencyData");
//xstream.aliasField("data", CurrencyDataDto.class, "data");
xstream.fromXML(json, this);
}
}
package com.fund.etrading.ebankapp.base.credit.cardniu.ecardniu.dto;
import java.util.ArrayList;
import java.util.List;
import com.fund.etrading.ebankapp.base.credit.BaseDto;
public class MailDto extends BaseDto{
protected String uid;
protected String relatedCardNums;
protected String houseHolder;
protected String subject;
protected String receiveTime;
protected String bankName;
protected List cards = new ArrayList();
public String getUid() {
return uid;
}
public String getRelatedCardNums() {
return relatedCardNums;
}
public String getHouseHolder() {
return houseHolder;
}
public String getSubject() {
return subject;
}
public String getReceiveTime() {
return receiveTime;
}
public String getBankName() {
return bankName;
}
public List getCards() {
return cards;
}
}
thanks in advance!
If you want to convert json string to your custom class(ex.GetMailsDataResponseDto), I recommend Google Gson.
If you use Gson, yon don't need fromJosn() method in GetMailsDataResponseDto class.
If you only use json parsing and have experiences of java script, I recommend Djson parser(java library).
"Djson Parse version 0.8a" -- http://blog.indf.net/category/Apps/djson
j1.txt - tip: "none BOM & UTF-8"
....
public void fromJson(String json){
//(real-code)--start
//Var var = Djson.parse(json);
//(real-code)--end
//--test-code--start
Var var = null;
try {
var = Djson.parse(new File("d:\\j1.txt"));
} catch (IOException e) {
e.printStackTrace();
}
//--test-code--end
this.dealedNum = var.get("dealedNum").toInt();
this.nextRequestDelay = var.get("nextRequestDelay").toString();
this.percent = var.get("percent").toString();
this.filterNum = var.get("filterNum").toInt();
this.dealedBillNum = var.get("dealedBillNum").toInt();
for(int i=0; i<var.get("mails").size(); i++) {
this.mails.add(var.get("mails").get(i).toObject()); // MAP type setting...
}
}
Related
Here is a java class CreateDoc which is sent from One web service that is producer side to another web service which is consumer side as List with content-type:Json
Below is the class representation
class CreateDoc{
DocMetData dMetaData;
DocContent dCont;
}
Class DocMetData {
String docNamel
String docType;
}
Class DocContent {
String data;
}
Once i receive the List as json in the consumer side i am not able to use this as a java Object and the content type is array with json nested inside an array.
Below is the Representation:
[
[
{
"dMetaData":{
"docName":"string",
"docType":"pdf"
},
"dCont":{
"data":"abc"
}
},
{
"dMetaData":{
"docName":"string",
"docType":"pdf"
},
"dCont":{
"data":"def"
}
},
{
"dMetaData":{
"docName":"string",
"docType":"pdf"
},
"dCont":{
"data":"ghk"
}
}
]
]
Question is how to process this and be able to use the data and represent as List.
Here's some sample code that shows how you can use the Jackson ObjectMapper to parse the data. Note that the code assumes the data is stored in a file, you can modify it as needed to suit your needs.
Here's the main class:
package parsing.arrayofarray;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ArrayOfArray {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String data = null;
try {
data = new String(Files.readAllBytes(Paths.get("src/main/resources/jsonArrayOfArray.json")));
} catch (IOException e1) {
e1.printStackTrace();
}
List<List<CreateDoc>> results = null;
try {
results = mapper.readValue(data, new TypeReference<List<List<CreateDoc>>>(){});
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(results);
}
}
and here are the supporting classes, first CreateDoc:
package parsing.arrayofarray;
public class CreateDoc {
DocMetData dMetaData;
DocContent dCont;
public DocMetData getdMetaData() {
return dMetaData;
}
public void setdMetaData(DocMetData dMetaData) {
this.dMetaData = dMetaData;
}
public DocContent getdCont() {
return dCont;
}
public void setdCont(DocContent dCont) {
this.dCont = dCont;
}
#Override
public String toString() {
return "CreateDoc [dMetaData=" + dMetaData + ", dCont=" + dCont + "]";
}
}
and DocContent:
package parsing.arrayofarray;
public class DocContent {
#Override
public String toString() {
return "DocContent [data=" + data + "]";
}
String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
and the DocMetData:
package parsing.arrayofarray;
public class DocMetData {
String docName;
String docType;
public String getDocNamel() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
#Override
public String toString() {
return "DocMetData [docNamel=" + docName + ", docType=" + docType + "]";
}
public String getDocType() {
return docType;
}
public void setDocType(String docType) {
this.docType = docType;
}
}
The output from the println is:
[[CreateDoc [dMetaData=DocMetData [docNamel=string, docType=pdf], dCont=DocContent [data=abc]], CreateDoc [dMetaData=DocMetData [docNamel=string, docType=pdf], dCont=DocContent [data=def]], CreateDoc [dMetaData=DocMetData [docNamel=string, docType=pdf], dCont=DocContent [data=ghk]]]]
You can use JSONArray(org.json) to parse the first list, and parse with GSON the inside list to create a List of CreatDoc. You can use only GSON to parse the first array too
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
public class Deserializer {
public static void main(String[] args) {
JSONArray jsonArray = new JSONArray(
"[[{\"dMetaData\": {\"docName\": \"string\",\"docType\": \"pdf\"},\"dCont\": {\"data\": \"abc\"}},{\"dMetaData\": {\"docName\": \"string\",\"docType\": \"pdf\"},\"dCont\": {\"data\": \"def\"}},{\"dMetaData\": {\"docName\": \"string\",\"docType\": \"pdf\"},\"dCont\": {\"data\": \"ghk\"}}]]");
JSONArray docsArray = jsonArray.getJSONArray(0);
List<CreateDoc> docsList = new Gson().fromJson(docsArray.toString(),
new TypeToken<ArrayList<CreateDoc>>() {}.getType());
docsList.forEach(System.out::println);
}
public static class CreateDoc {
DocMetData dMetaData;
DocContent dCont;
#Override
public String toString() {
return this.dMetaData.toString() + " " + this.dCont.toString();
}
}
public static class DocMetData {
String docName;
String docType;
#Override
public String toString() {
return "name: " + this.docName + " type: " + this.docType;
}
}
public static class DocContent {
String data;
#Override
public String toString() {
return "data: " + this.data;
}
}
}
You can use GSON to parse the message into a JSONArray with JSONObjects. Then create a parser for each class to convert the fields from the JSONObject into Java objects. Similar question is anwered here.
I think the problem is you are trying to map json to CreateDoc instead of CreateDoc List. If you are using spring boot to manage rest layer in your application use #Requestbody List CreateDoc in the method to convert your json. This will use Jackson converter internally. Otherwise you can use Jackson converter jar to convert your json to objects.
I have a simple wrapper class.
class Wrapper {
int id;
Object command;
}
command could be an object that I get from the outside, and I cannot create an interface to hold the possible types together.
I'd like to serialize it simply:
String json = objectMapper.writeValueAsString(wrapper);
So that I get:
{"id":"1","command":{"type" : "objectType", "key0": "val0", ... other properties...}}
Ideally I'd build a registry with the possible values of type and the corresponding class names as values, so I could deserialize it like this:
Wrapper wrapper = objectMapper.readValue(bytes, Wrapper.class);
(objectMapper is com.fasterxml.jackson.databind.ObjectMapper)
Is there a way to achieve this with Jackson?
You can use the Jackson polymorphic type handling. You can declare which type the command property can be using #JsonTypeXXX annotations.
Here is a complete example:
public class JacksonTypeInfoOnObject {
public static class Bean {
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
#JsonSubTypes({
#JsonSubTypes.Type(Command1.class),
#JsonSubTypes.Type(Command2.class)
})
public final Object command;
#JsonCreator
public Bean(#JsonProperty("command") final Object command) {this.command = command;}
#Override
public String toString() {
return "Bean{" +
"command=" + command +
'}';
}
}
#JsonTypeName("cmd1")
public static class Command1 {
#Override
public String toString() {
return "Command1{}";
}
}
#JsonTypeName("cmd2")
public static class Command2 {
#Override
public String toString() {
return "Command2{}";
}
}
public static void main(String[] args) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
final List<Bean> list = Arrays.asList(
new Bean(new Command1()),
new Bean(new Command2()));
final String json = mapper.writeValueAsString(list);
System.out.println(json);
final List<Bean> values = mapper.readValue(json, new TypeReference<List<Bean>>() {});
System.out.println(values);
}
}
Output:
[{"command":{"type":"cmd1"}},{"command":{"type":"cmd2"}}]
[Bean{command=Command1{}}, Bean{command=Command2{}}]
I changed the type of your command property to a Map<String, Object> and the Wrapper object can be serialized/deserialized as expected.
Below, is the output generated by the Main class:
SERIALIZE: {"id":1,"command":{"key0":"val0","type":"objectType"}}
DESERIALIZE: Wrapper [id=1, command={key0=val0, type=objectType}]
Main.java
package json;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
static ObjectMapper objectMapper = new ObjectMapper();
private static Wrapper createWrapper() {
Wrapper wrapper = new Wrapper();
Map<String, Object> command = new HashMap<String, Object>();
command.put("type", "objectType");
command.put("key0", "val0");
wrapper.id = 1;
wrapper.command = command;
return wrapper;
}
private static String serializeWrapper(Wrapper wrapperObj) {
try {
return objectMapper.writeValueAsString(wrapperObj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
private static Wrapper deserializeWrapper(String wrapperJsonStr) {
try {
return objectMapper.readValue(wrapperJsonStr, Wrapper.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Wrapper wrapper = createWrapper();
String wrapperJsonStr = serializeWrapper(wrapper);
System.out.printf("SERIALIZE: %s%n", wrapperJsonStr);
Wrapper wrapperObj = deserializeWrapper(wrapperJsonStr);
System.out.printf("DESERIALIZE: %s%n", wrapperObj);
}
}
Wrapper.java
package json;
import java.util.Map;
public class Wrapper {
public int id;
public Map<String, Object> command;
#Override
public String toString() {
return "Wrapper [id=" + id + ", command=" + command + "]";
}
}
I need to create a class library which enables me to read different files (.dat-files with different data representations inside them) and create objects with their content (for every line one object).
I also have to create a unit test which starts the reading of the file, so I dont have to read to whole file first and save the content in an array. I want to use the factory pattern.
Here is my implementation of the class that implements the Iterator-Interface
package klassenbibliothek;
public class MyReader implements Iterator<Object>
{
BufferedReader reader;
MyReader(BufferedReader myReader)
{
reader = myReader;
}
#Override
public boolean hasNext() // aus Stackoverflow, von mir abgeändert
{
try {
return reader.ready();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
throw new NoSuchElementException();
}
}
#Override
public String next()
{
//return SubstancesFileObjectCreator(reader.readLine());
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
// return null;
throw new NoSuchElementException();
}
}
}
My question is: why do I get this error message "finally block does not complete normally"? I am not returning something, I am just throwing an exception.
I want to use the methods hasNext() and next() in my unit test, so that the unit test can controll when it starts to read the file. The unit test is in a different package.
Here are my other classes:
class AbstractFileObjectCreator
package klassenbibliothek;
public abstract class AbstractFileObjectCreator
{
public abstract AbstractFileObject createFileObject(String line);
}
class SubstancesFileObjectCreator
package klassenbibliothek;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SubstancesFileObjectCreator extends AbstractFileObjectCreator
{
MyReader myReader;
public void makeReader() throws IOException
{
String dataFileName = "C:/temp/Substances.dat";
BufferedReader bReader = new BufferedReader(new FileReader(dataFileName));
myReader = new MyReader(bReader);
}
#SuppressWarnings("null")
public AbstractFileObject createFileObject(String line)
{
AbstractFileObject mySubstance = null;
String lineValues[] = myReader.next().split("\t");
if(lineValues[0].equals("R"))
{
boolean dutyToDeclare_local;
boolean isUnwanted_local;
boolean isProhibited_local;
boolean isReach_local;
boolean isDeleted_local;
boolean isHidden_local;
String nodeidRaw = lineValues[1];
float nodeid = Float.parseFloat(nodeidRaw);
String casNrRaw = lineValues[2];
String euIndexCodeRaw = lineValues[3];
String einecsCodeRaw = lineValues[4];
String dutyToDeclareRaw = lineValues[5];
if(dutyToDeclareRaw.equals(1))
{
dutyToDeclare_local = true;
}
else
{
dutyToDeclare_local = false;
}
String isUnwantedRaw = lineValues[6];
if(isUnwantedRaw.equals("1"))
{
isUnwanted_local = true;
}
else
{
isUnwanted_local = false;
}
String isProhibitedRaw = lineValues[7];
if(isProhibitedRaw.equals("1"))
{
isProhibited_local = true;
}
else
{
isProhibited_local = false;
}
String isReachRaw = lineValues[8];
if(isReachRaw.equals("1"))
{
isReach_local = true;
}
else
{
isReach_local = false;
}
String isDeletedRaw = lineValues[9];
if(isDeletedRaw.equals("1"))
{
isDeleted_local = true;
}
else
{
isDeleted_local = false;
}
String isHiddenRaw = lineValues[10];
if(isHiddenRaw.equals("1"))
{
isHidden_local = true;
}
else
{
isHidden_local = false;
}
mySubstance = new Substance(nodeid, casNrRaw, euIndexCodeRaw, einecsCodeRaw, dutyToDeclare_local, isUnwanted_local, isProhibited_local, isReach_local, isDeleted_local, isHidden_local);
// und weiter...
}
else
{
String languageCode = lineValues[1];
String name = lineValues[2];
// Synonym-Objekt erzeugen und zu Substance-Objekt hinzufügen
Synonym newSynonym = new Synonym(languageCode, name);
mySubstance.addAppendix(newSynonym);
while(myReader.hasNext())
{
String lineValues_synonyms[] = myReader.next().split("\t");
String lineValuesZero = lineValues_synonyms[0];
if(lineValuesZero.equals("R"))
{
break; // nicht so gut glaube ich!!!
}
String languageCode_next = lineValues_synonyms[1];
String name_next = lineValues_synonyms[2];
Synonym newSynonym_next = new Synonym(languageCode_next, name_next);
mySubstance.addAppendix(newSynonym_next);
}
}
return mySubstance;
}
}
class AbstractFileObject
package klassenbibliothek;
public abstract class AbstractFileObject
{
boolean isDeleted;
public AbstractFileObject(boolean isDeleted)
{
this.isDeleted = isDeleted;
}
public boolean getIsDeleted()
{
return isDeleted;
}
public abstract void addAppendix(Object newAppendix);
}
class Substance
public class Substance extends AbstractFileObject
{
private float nodeid;
private String casNr;
private String euIndexCode;
private String einecsCode;
private boolean dutyToDeclare;
private boolean isUnwanted;
private boolean isProhibited;
private boolean isReach;
private boolean isDeleted;
private boolean isHidden;
private ArrayList<Synonym> synonymList;
public Substance(float nodeid, String casNr, String euIndexCode, String einecsCode,
boolean dutyToDeclare, boolean isUnwanted, boolean isProhibited, boolean isReach,
boolean isDeleted, boolean isHidden)
{
super(isDeleted);
this.nodeid = nodeid;
this.casNr = casNr;
this.euIndexCode = euIndexCode;
this.einecsCode = einecsCode;
this.dutyToDeclare = dutyToDeclare;
this.isUnwanted = isUnwanted;
this.isProhibited = isProhibited;
this.isReach = isReach;
//this.isDeleted = isDeleted;
this.isHidden = isHidden;
}
// getter and setter
}
class Synonym
package klassenbibliothek;
public class Synonym
{
private String languageCode;
private String name;
public Synonym(String languageCode, String name)
{
this.languageCode = languageCode;
this.name = name;
}
public String getLanguageCode()
{
return languageCode;
}
public String getName()
{
return name;
}
}
unit test
package klassenbibliothek.test;
import static org.junit.Assert.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class SubstancesTest {
#Test
public void test() {
//fail("Not yet implemented");
long startTimeNanos = System.nanoTime();
/*
* While... iterator over data file
*/
}
}
Am I using the factory pattern in the right way? I'm very confused.
A finally block always executes if there is a try-block before it. So yours always throws a NoSuchElementException().
finally
{
// return null;
throw new NoSuchElementException();
}
You should do something in it and not throw an Exception.
Finally blocks are for cleanup. They should not specifically throw exceptions like that. Move the exception throwing out of the finally block.
Remove the throw exception from the finally block and put it in catch block or some other place. Finally block is to release resources that you might be using in your program.
I am reading some huge data from some csv files and putting into database(Mongodb). But after reading some of the files i am getting heap memory error in java.I don't understand what is wrong with my program.The below is my program -
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import au.com.bytecode.opencsv.CSVReader;
public class Csvread {
MongoClient mongoClient = null;
DB db = null;
DBCollection coll =null;
static Date myDate = new Date();
public Csvread()
{
try {
mongoClient = new MongoClient("localhost" ,27017);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db = mongoClient.getDB( "mahout" );
coll = db.getCollection("data_flowers_all");
DBObject fields=new BasicDBObject("product_url",1);
DBObject options=new BasicDBObject("unique",true);
// options.put("dropDups", true);
//
coll.createIndex(fields,options);
coll.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
}
public static void main(String[] args) {
String parentPath="/media/bqadmin/D87CFB1B7CFAF35C/flowersjack/csv files";
File parentFolder = new File(parentPath);
String[] files = parentFolder.list();
Csvread cvobj= new Csvread();
try {
for(String file : files)
{
System.out.println();
System.out.println();
System.out.println();
System.out.println("files are:" +parentPath+File.separator+file);
CSVReader reader = new CSVReader(new FileReader(parentPath+File.separator+file),',','"');
String [] nextLine;
try {
while ((nextLine = reader.readNext()) != null &&nextLine.length!=0) {
Encapsulation encap=new Encapsulation();
// System.out.println("product name"+nextLine[1]);
encap.setId(nextLine[0]);
encap.setProduct_name(nextLine[1]);
encap.setProduct_url(nextLine[6]);
encap.setProduct_image(nextLine[3]);
encap.setProduct_price(nextLine[5]);
encap.setProduct_src("www.flowersus.com");
encap.setCountry("USA");
encap.setDate(myDate);
encap.setCategory(nextLine[8]);
cvobj.DBConnection(encap);
}
} catch (IOException e) {
System.out.println("reading exception");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("closing reader");
e.printStackTrace();
}
System.out.println("inside forloop");
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("filenotfound");
e.printStackTrace();
}
}
public void DBConnection(Encapsulation enc ) throws IOException{
try {
System.out.println("Inside database connectivity");
BasicDBObject document=new BasicDBObject();
// System.out.println("BasicDBObject created");
document.put("product_id",enc.getId());
// System.out.println("product_id is" +enc.getId());
document.put("product_name",enc.getProduct_name() );
// System.out.println("product_name is" +enc.getProduct_name());
document.put("product_url",enc.getProduct_url());
System.out.println("product_url is" +enc.getProduct_url());
document.put("product_img", enc.getProduct_image());
// System.out.println("product_img is" +enc.getProduct_image());
document.put("product_price",enc.getProduct_price());
// System.out.println("price is" +enc.getProduct_price());
document.put("country", "India");
// System.out.println("country" +enc.getCountry());
document.put("date",new SimpleDateFormat("MM-dd-yyyy").format(myDate));
document.put("category", enc.getCategory());
// System.out.println("categoriy is" +enc.getCategory());
System.out.println(enc);
System.gc();//for clearing the object
coll.insert(document);
System.out.println("insertion complete");
}
catch (MongoException e) {
System.out.println("duplicate");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
In this i have called System.gc(); to clear objects...Can anyone tell what is wrong with my program.Any help will be highly appreciable.....
public class Encapsulation {
private String id;
private String product_name;
private String product_url;
private String product_image;
private String product_price;
private String product_src;
private String country;
private Date date;
private String Category;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getProduct_url() {
return product_url;
}
public void setProduct_url(String product_url) {
this.product_url = product_url;
}
public String getProduct_image() {
return product_image;
}
public void setProduct_image(String product_image) {
this.product_image = product_image;
}
public String getProduct_price() {
return product_price;
}
public void setProduct_price(String product_price) {
this.product_price = product_price;
}
public String getProduct_src() {
return product_src;
}
public void setProduct_src(String product_src) {
this.product_src = product_src;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Date getDate() {
return date;
}
#Override
public String toString() {
return "Encapsulation [id=" + id + ", product_name=" + product_name + ", product_url=" + product_url
+ ", product_image=" + product_image + ", product_price=" + product_price + ", product_src="
+ product_src + ", country=" + country + ", date=" + date + ", Category=" + Category + "]";
}
public void setDate(Date myDate) {
this.date = myDate;
}
public String getCategory() {
return Category;
}
public void setCategory(String category) {
Category = category;
}
}
Please note:after inserting data for some time ,the heap memory issue arises.
I am not sure, but it could be a memory leak in the Java-MongoDB-driver.
One possible way to resolve your issue and/or increase the performance of your code. Use:
public WriteResult insert(List<DBObject> list)
or any other method, which allows to add multiple data records at once.
(http://api.mongodb.org/java/2.13/)
I know, that u will have problems figuring out, what the duplicate entries are this way. I am not such much into MongoDB to know the best way to handle duplicate entries when adding multiple data records at once. But in your case, i definitly would try that approach.
The second thing:
Do the standard stuff to handle heap errors. Increase at least your heap size.
I dont know, how big your string arrays get, but it could be, that they make your too small heap explode. Especially with a bad implementation of CSVReader and a slow garbage collector.
Thats all guessing, but what isnt, when it comes to memory leaks... :)
Hope it helps.
Is there any module in Java equivalent to python's shelve module? I need this to achieve dictionary like taxonomic data access. Dictionary-like taxonomic data access is a powerful way to save Python objects in a persistently easy access database format. I need something for the same purpose but in Java.
I also needed this, so I wrote one. A bit late, but maybe it'll help.
It doesn't implement the close() method, but just use sync() since it only hold the file open when actually writing it.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
public class Shelf extends HashMap<String, Object> {
private static final long serialVersionUID = 7127639025670585367L;
private final File file;
public static Shelf open(File file) {
Shelf shelf = null;
try {
if (file.exists()) {
final FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
shelf = (Shelf) ois.readObject();
ois.close();
fis.close();
} else {
shelf = new Shelf(file);
shelf.sync();
}
} catch (Exception e) {
// TODO: handle errors
}
return shelf;
}
// Shelf objects can only be created or opened by the Shelf.open method
private Shelf(File file) {
this.file = file;
sync();
}
public void sync() {
try {
final FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this);
oos.close();
fos.close();
} catch (Exception e) {
// TODO: handle errors
}
}
// Simple Test Case
public static void main(String[] args) {
Shelf shelf = Shelf.open(new File("test.obj"));
if (shelf.containsKey("test")) {
System.out.println(shelf.get("test"));
} else {
System.out.println("Creating test string. Run the program again.");
shelf.put("test", "Hello Shelf!");
shelf.sync();
}
}
}
You could use a serialisation library like Jackson which serialises POJOs to JSON.
An example from the tutorial:
Jackson's org.codehaus.jackson.map.ObjectMapper "just works" for
mapping JSON data into plain old Java objects ("POJOs"). For example,
given JSON data
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
}
It takes two lines of Java to turn it into a User instance:
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
User user = mapper.readValue(new File("user.json"), User.class);
Where the User class looks something like this (from an entry on Tatu's blog):
public class User {
public enum Gender { MALE, FEMALE };
public static class Name {
private String _first, _last;
public String getFirst() { return _first; }
public String getLast() { return _last; }
public void setFirst(String s) { _first = s; }
public void setLast(String s) { _last = s; }
}
private Gender _gender;
private Name _name;
private boolean _isVerified;
private byte[] _userImage;
public Name getName() { return _name; }
public boolean isVerified() { return _isVerified; }
public Gender getGender() { return _gender; }
public byte[] getUserImage() { return _userImage; }
public void setName(Name n) { _name = n; }
public void setVerified(boolean b) { _isVerified = b; }
public void setGender(Gender g) { _gender = g; }
public void setUserImage(byte[] b) { _userImage = b; }
}