Sie sind auf Seite 1von 3

Accessing Actian PSQL Data from a

Java OData Application


Java applications run on a variety of mobile devices on which PSQL components are not
supported. But you can still use PSQL as the data repository for such applications through the
Open Data Protocol (OData).
This is the companion article to Creating an OData Java Producer for Actian PSQL. In that
article, I explained how to create the producer (server piece) for a Java OData solution. This
article outlines the steps that I used to create a Java client application. Or, in OData terms, how
to create the Java consumer.
This article contains the following sections:

Understanding Open Data Protocol and Odata4j

Setting Up the Environment

Building and Running the Example Consumer Application

Noting Some Limitations

Understanding Open Data Protocol and Odata4j


OData is a Web based protocol from Microsoft released under the Microsoft Open Specification
Promise. It is designed to access data from a variety of sources in a standardized manner.
OData is built on top of existing standards, such as HTTP, Atom Publishing Protocol (Atompub),
and JavaScript Object Notation (JSON). OData client libraries exist for devices that support the
Java virtual machine. The client libraries abstract the details of OData and allow you to create
applications using Java. (Visit the OData website (http://www.odata.org/) for more information
on the protocol, libraries, and so forth.)
An OData solution is similar to traditional client/server architectures. In OData terms, a server is
called a producer and a client is called a consumer.

Odata4j
Odata4j is an open source toolkit that provides both OData producer and consumer frameworks
for Java applications. Information on this toolkit can be found at their Web site
(http://code.google.com/p/odata4j/).
This toolkit is the basis for the Java OData consumer discussed in this article. I created a
sample console application that shows data retrieval.

2013 Actian Corporation. All rights reserved. Printed in the U.S.A. Actian is a trademark of Actian Corporation in the United States
and in other countries. All other trademarks, trade names, service marks, and logos referenced herein belong to their respective
companies.

Note that a complete OData solution for Java requires both a producer and a consumer. I have
also implemented a basic Java provider that you can configure to share your database. Refer
to Creating an OData Java Producer for Actian PSQL for details on the producer portion.

Setting Up the Environment


Here is the environment setup that I used:

One system installed with PSQL Vx Server 11 SP3. This system also needs to have the
Java Runtime Environment (JRE) installed. You can either use the JRE installed by
PSQL or install a different version. This is the system on which the OData producer
runs.

A development system with Eclipse and the Java Development Kit (JDK) installed. I
used JDK 1.6.0_31. I ran the consumer application on this system as well, but you may
run it from a third system that has the JRE installed.

Building and Running the Example Consumer Application


The consumer application is based on one in the odata4j examples. My application is a simple
one that prints the DEMODATA OData metadata followed by all of the entries in the Room
table. The example producer and consumers can be found as a source archive in the odata4j
v0.7 download from the odata4j Web site. See http://code.google.com/p/odata4j/downloads/list.
I used the auto build feature of Eclipse to build the .java files automatically. I then created a
batch file named createconsumerjar.bat to create the jar.
Follow these steps to build the consumer application.
1. Open in Eclipse the file
PSQLODataJavaExample\PSQLODataJavaConsumer\src\odata4j\consumer\
ODataPSQLDemoDataReadExample.java.
2. Find the Run function and change the IP address in the URL to the IP for the system on
which the producer is running.
3. Recreate the consumer jar file by running createconsumerjar.bat and deploy the jar on a
remote system. If your JDK is installed to a different directory than mine, you may need
to edit the createconsumerjar.bat file to modify the directory of the jar command.
4. Start the producer application. See Creating an OData Java Producer for Actian PSQL
for details.
5. Run the consumer application with the following command: java jar
PsqlOdataTestClient.jar.
The end of the output looks like the following data:
2013 Actian Corporation. All rights reserved. Printed in the U.S.A. Actian is a trademark of Actian Corporation in the United States
and in other countries. All other trademarks, trade names, service marks, and logos referenced herein belong to their respective
companies.

Room: Gambill Building


501
Building_Name: Gambill Building
Number: 501
Capacity: 50
Type: Classroom
Room: Gambill Building
613
Building_Name: Gambill Building
Number: 613
Capacity: 1
Type: Office
Room: Garrison Hall
110
Building_Name: Garrison Hall
Number: 110
Capacity: 2
Type: Office
Room: Garrison Hall
120
Building_Name: Garrison Hall
Number: 120
Capacity: 35
Type: Classroom

Noting Some Limitations


The example application has the following limitations.

This application only queries data. It does not insert, update, or delete data. The entity
data model in which the OData protocol is built upon requires primary keys to be defined
on tables to perform insert, update, and delete operations. DEMODATA does not have
any primary keys defined. To see examples of insert, update, and delete operations,
review the ODataTestServiceReadWriteExample.java file. That file is contained in the
source archive for the odata4j v0.7 download from the odata4j Web site. See
http://code.google.com/p/odata4j/downloads/list.

The companion Java producer is intended to show that an OData producer can expose
information from PSQL using JDBC. The producer is not very robust and may not be
completely compliant with the OData specification. However, the odata4j group is
currently working on a JDBC producer to be included natively in their package. Once
that producer is available, the companion producer that I created will be obsolete.

Jan D., Principal Software Development Engineer, Actian Corporation

2013 Actian Corporation. All rights reserved. Printed in the U.S.A. Actian is a trademark of Actian Corporation in the United States
and in other countries. All other trademarks, trade names, service marks, and logos referenced herein belong to their respective
companies.

Das könnte Ihnen auch gefallen