English | Site Directory

Google Base Data API

Java Client Library - QueryExample

QueryExample is a simple Java application that runs from the command line. It is designed to perform only unauthenticated queries on the public snippets feed ( /feeds/snippets ). It doesn't allow for authentication, so you cannot use it to query the customer-specific data items feed. For more information on constructing queries, see Attributes and Queries. For more information about the public feed, refer to the Snippets Feed reference.

The source code for QueryExample is available in the Client Library in the file java/src/com/google/api/gbase/examples/cmdline/QueryExample.java

Contents

  1. Using QueryExample
    1. Sample queries
  2. Stepping through the QueryExample code

Using QueryExample

QueryExample uses a command line structured like this:

java ...QueryExample --key dev_key [--url base_url] your_query
Parameter Meaning Notes
key Developer Key You can obtain a developer key here.
base_url URL [Optional] By default, the query connects to http://www.google.com/base/, but you can enter a specific server instead.
your_query the query string Construct a normal Google Base query.

If the query string contains one or more spaces or you have more than one parameter, you must enclose the entire query in quotes.

Sample queries

The most basic query unit is a single word that is used as a basis for a full text search.but in the example below, the query uses a simple string, so there is no need for quotes.

java -cp gdata-base-cmdline.jar com.google.api.gbase.examples.cmdline.QueryExample --key 837738399 delectable

The response returns as an XML feed:

Sending request to: http://www.google.com/base/feeds/snippets/?q=delectable&max-results=10 
Recipes: Delectable Date Muffins - http://www.google.com/base/feeds/snippets/17157731210069334326 
Recipes: Delectable Carob Fudge - http://www.google.com/base/feeds/snippets/10804997642391806178 
Recipes: Delectable Meat Loaf - http://www.google.com/base/feeds/snippets/1725244762499121612 
Recipes: Delectable Cheese Crepes - http://www.google.com/base/feeds/snippets/15581187920521744475 
Recipes: Delectable Chocolate Cheesecake Bites
- http://www.google.com/base/feeds/snippets/12550115891789923473 Recipes: Delectable Pork in a Mustard Spice Mix
- http://www.google.com/base/feeds/snippets/12265106833065462005 Recipes: Delectable Carob Fudge - http://www.google.com/base/feeds/snippets/11766929948556732284 Recipes: Delectable Meat Loaf - http://www.google.com/base/feeds/snippets/8426529378607769341 Recipes: Pork Spareribs with Delectable Barbecue Sauce
- http://www.google.com/base/feeds/snippets/6246454571705952076 Recipes: Delectable Pork in a Mustard Spice Mix
- http://www.google.com/base/feeds/snippets/407380460513728684

The results only provide three values: item type, title, and the complete item URL. You can change QueryExample to provide additional values as needed.

Here is another example that searches for a multi-word string:

java -cp lib/gdata-base-cmdline.jar com.google.api.gbase.examples.cmdline.QueryExample "fastest laptop"

You must enclose the search string in quotation marks because it is has embedded spaces. A successful query returns your data as a public feed ( feeds/snippets ):

Sending request to: http://www.google.com/base/feeds/snippets/?q=fastest+laptop&max-results=10
products: Acme 2000 series laptop - http://www.google.com/base/feeds/snippets/183661021328332213 products: Acme 2000 series laptop - http://www.google.com/base/feeds/snippets/362381480143117290
Jobs: Sales executive - http://www.google.com/base/feeds/snippets/18372588426313973918
Jobs: Sales Executive - http://www.google.com/base/feeds/snippets/11236958285592445033
Jobs: Business Development Manager - http://www.google.com/base/feeds/snippets/8800160957116053291

Note that Google Base treats your input as two non-contiguous ANDed values, so it gets any item that has "fastest" AND "laptop" in their description.

You can also set up complex queries that use one or more attributes along with a text string, but you must enclose the entire query in quotes. For example, in order to obtain a list of all Chinese chicken recipes that are described as delectable, you could use a query like these:

java -cp lib/gdata-base-cmdline.jar com.google.api.gbase.examples.cmdline.QueryExample  
"[item type:recipes]([cuisine:chinese]|[label:chinese]) [main ingredient:chicken] delectable"

The response returns as an XML feed. All queries are URL encoded, but it is more obvious with complex queries. Here's how the request looks with encoding:

Sending request to:  http://www.google.com/base/feeds/snippets/?bq=%5Bitem+type%3Arecipes%5D+%28
%5Bcuisine%3A+chinese%5D%7C%5Blabel%3Achinese%5D%29+%5Bmain+ingredient%3A+chicken%5D+dice&max-results=10

In addition to searching for the "delectable" text string, this query searches for several attribute values, the basic syntax for which is:

[attrib_name: attrib_value]

In the command-line above, the | indicates OR, so ( [cuisine:value] | (label:value)] indicates that the value of either the cuisine or a label must be "chinese."

"[item type:recipes] ([cuisine:chinese]|[label:chinese]) [main ingredient:chicken] dice"

For a discussion of how to create queries, go to Attributes and Queries. For full details of the Google Base structured query language, go to the Query Language Specification.

Back to top

Stepping through the QueryExample code

QueryExample is a simple Java application that runs from the command line to query the public snippets feed. It uses Google data API client libraries to set up the query, to perform an HTTP GET, and to parse the XML response.

This section presents a simplified version of the actual code. It does not include code that parses command-line arguments or error handling code. Therefore, the code presented here may not exactly match the actual code that you see in the application.

To do this programmatically, you need to create a GoogleBaseService object and then run a query on it:

GoogleBaseService service =
    new GoogleBaseService("Google-Tutorial-1.0", developerKey);

GoogleBaseQuery query = 
    new GoogleBaseQuery(FeedURLFactory.getDefault().getSnippetsFeedURL());
query.setGoogleBaseQuery("digital camera");

GoogleBaseFeed feed = service.query(query);    

GoogleBaseQuery takes the URL of the feed you want to access (the default base URI is http:/www.google.com/base/ plus the hard-coded feeds/snippets/ URI ) and adds parameters as explained in Attributes and Queries. Each parameter supported by the Snippets feed has a corresponding setter in the GoogleBaseQuery object. In the sample code above, setGoogleBaseQuery corresponds to the parameter bq.

service.query() connects to the URL generated by GoogleBaseQuery, gets the result, parses it and returns a GoogleBaseFeed. The GoogleBaseFeed contains a list of GoogleBaseEntry objects, one for each Google Base item returned by the server.

for(GoogleBaseEntry entry: feed.getEntries()) {
  System.out.println(entry.getGoogleBaseAttributes().getItemType() +
                     ": " + entry.getTitle().getPlainText() +
                     " - " +entry.getId());
}  

The code snippet above displays, for each entry, its item type (<g:item_type type="text">) its title (<atom:title>) and its ID (<atom:id>).

Since <g:item_type> is in the g: namespace, you first need to get the GoogleBaseAttributesExtension object of the entry using entry.getGoogleBaseAttribute() and then get the attribute value using getItemType().

getItemType() is a shortcut, which corresponds to getTextAttribute("item type"), which means "get the first attribute in the entry that is called 'item type' and whose type is 'text'". The second line in the example above could then be rewritten:

System.out.println(entry.getGoogleBaseAttributes().getTextAttribute("item type") +

Go to CustomerTool

Updated on