Language: - Java 1.7
Environment: - Eclipse IDE Kepler
Requirement: - It is regarding fetch data from java application. I have created a web application in GAE which is working fine. Now I want to my application code should run from main and retrieve the date stored in Data Store. Need to fetch data from core java application.
import java.util.List;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.FetchOptions;
public static void main(String[] args)
{
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query = new Query("SMS").addSort("Date", Query.SortDirection.DESCENDING);
List<Entity> customers = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(50));
for(Entity e : customers){
System.out.println("Service: - " + e.getProperty("Service") );
System.out.println("UserName: - " + e.getProperty("UserName") );
System.out.println("QueryString: - " + e.getProperty("QueryString") );
System.out.println("EmailId: - " + e.getProperty("EmailId") );
System.out.println("MobileHashFormat: - " + e.getProperty("MobileHashFormat") );
System.out.println("Date: - " + e.getProperty("Date") );
System.out.println("Status: - " + e.getProperty("Status") );
}
}
Exception: -
Exception in thread "main" java.lang.NullPointerException: No API environment is registered for this thread. at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:86) at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppIdNamespace(DatastoreApiHelper.java:102) at com.google.appengine.api.datastore.Query.(Query.java:215) at com.google.appengine.api.datastore.Query.(Query.java:144) at serive.TestGAEFile.main(TestGAEFile.java:17)
When you try to run things from the command line, you need to understand that Google App Engine will need to do a lot of work to setup the local environment with just about enough services to even let the APIs work in the way that you want.
To facilitate testing in this local way, you will need to read up on the Local Unit Testing features. Check out https://developers.google.com/appengine/docs/java/tools/localunittesting?csw=1 and you will need additional JARs and use a LocalTesting Helper class that setups up the environment for you.