Solved

Storing Text type into Datastore

Posted on 2013-11-17
3
480 Views
Last Modified: 2013-11-23
I have a project where I am reading a RSS file and putting it into the GAE Datastore. I run into the problem with the "description" fields of the RSS file because they are more than 500 characters and will not load into Datastore. If I read correctly the workaround is to use the com.google.appengine.api.datastore.Text but I am struggling to make it work.

       DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
        URL u = new URL("http://feeds.feedburner.com/HeadlineNewsLiberiaAndItsNeighbors"); // your feed url 
        Document doc = builder.parse(u.openStream()); 
        NodeList nodes = doc.getElementsByTagName("item"); 
        for(int i=0;i<nodes.getLength();i++) { 
            Element element = (Element)nodes.item(i); 
            rssTitle = (String) getElementValue(element, "title");
            rssLink = (String) getElementValue(element, "link");
            rssContent = (String) getElementValue(element, "description");
            
            log.info("Title: " + getElementValue(element, "title"));
            log.info("Link: " + getElementValue(element, "link"));
            log.info("Description: " + getElementValue(element, "description"));
            
            // Add to Datastore
            log.info ("Starting Datastore");
            Key rssLinkKey = KeyFactory.createKey("RSS", "32390djks");
            Entity rssLink = new Entity("RSS", rssLinkKey);
            rssLink.setProperty("title", rssTitle);
            rssLink.setProperty("link", rssLink);
            rssLink.setProperty("description", new Text(rssContent));
            
            DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
            datastore.put(rssLink);
            log.info("Ending Datastore");

Open in new window

0
Comment
Question by:iamkhova
  • 2
3 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39655532
but I am struggling to make it work
Can you be a little more specific? Are you getting error messages, or it's just not working? If you are getting error messages/exceptions what are they, post the full message?
0
 

Accepted Solution

by:
iamkhova earned 0 total points
ID: 39656524
The error that I was getting was
String properties must be 500 characters or less. Instead, use com.google.appengine.api.datastore.Text, which can store strings of any length

I looked at my code again this morning and realized that I was not declaring the Text variable correctly.

I have made the modifications and now it correctly stores fields over 500 characters to GAE Datastore.

            
            Text text = new Text(getElementValue(element, "description"));
            rssLink.setProperty("description", text);

Open in new window


Thanks.
0
 

Author Closing Comment

by:iamkhova
ID: 39671072
The error became obvious once I took some time to step away and revisit the code.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Verbose logging is used to diagnose garbage collector problems. By default, -verbose:gc output is written to either native_stderr.log or native_stdout.log.   It is also possible to redirect the logs to a user-specified file. This article will de…
Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now