My favorites | English | Sign in

Google Base Data API (Labs)

C# Client Library - QueryExample

gbase_querytool.exe is a simple C# 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 C# Client Library in the file cs/samples/gbase/queryexample.cs. The executable is available in the file /cs/lib/Release/gbase_querytool.exe.

Contents

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

Using gbase_querytool.exe

QueryExample uses a command line structured like this:

gbase_querytool.exe --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. If you use a multi-word string, you must enclose it in quotes, as in "digital camera" but in the example below, the query uses a simple string, so there is no need for quotes.

gbase_querytool.exe  --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.cs to provide additional values as needed.

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:

gbase_querytool.exe  "[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 C# 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 programatically, you need to create a GBaseService object and then run a query on it. This is done as follows:

    GBaseService service = new GBaseService("Google-Tutorial-1.0", developerKey);
    GBaseQuery query = new GBaseQuery(GBaseUriFactory.Default.SnippetsFeedUri);
    query.GoogleBaseQuery = "digital camera";

    GBaseFeed feed = service.Query(query);

GBaseQuery takes the URL of the feed you want to access and adds parameters or categories as explained in the Google data documentation. Each parameter supported by the Snippets feed corresponds to a property in the GBaseQuery object. In the sample code above, the GoogleBaseQuery property corresponds to the bq parameter.

service.Query() connects to the URL generated by GBaseQuery, gets the result, parses it and returns a GBaseFeed. The GBaseFeed contains a list of GBaseEntry objects, one for each Google Base item returned by the server.

    foreach (GBaseEntry entry in feed.Entries)
    {
        System.Console.WriteLine(entry.GBaseAttributes.ItemType +
                                 ": " + entry.Title.Text +
                                 " - " + entry.Id.Uri);
    }
    

The code snippet above displays the following information for each entry:

  • its item type (<g:item_type type="text">)
  • its title (<atom:title>)
  • its id (<atom:id>)

Since <g:item_type> is in the g: namespace, you first need to get the GBaseAttributes object of the entry using entry.GBaseAttributes. Then you get the attribute value using the property ItemType.

ItemType is a shortcut that corresponds to GetTextAttribute("item type"). GetTextAttribute() gets the first attribute in the entry that is called 'item type' and whose type is 'text'. The second line in the example above could be rewritten as follows using GetTextAttribute():

      System.Console.WriteLine(entry.GBaseAttributes.GetTextAttribute("item type") +

Go to CustomerTool

Updated on