Today we're announcing immediate availability of JAzure, a Java based interoperability library for the Windows Azure Services Storage API.
Windows Azure is often thought of as a Microsoft-only platform, but actually it has wide applicability. It has been possible to host Java applications in Windows Azure Compute for a while, but with this JAR library, you can now work with Azure Tables, Queues and Blobs from Java code.
JAzure is written on top of the Restlet framework and provides a simple Java API which is not dissimilar to the StorageClient sample shipped with the Windows Azure SDK.
To give you a feel for how JAzure works, the following Java sample code uses JAzure API to interacting with blobs. It shows you how to perform the main operations such as listing containers, listing blobs in a container, uploading blobs in the containers and downloading blobs.
// creating the Blob Storage entry point
// provide your credentials in the jazure.properties file
BlobStorage blobStorage = new BlobStorage();
// listing containers
blobStorage.listContainers();
BlobContainer container = blobStorage.getBlobContainer("test");
// creating a new blob and uploading the cloud.
BlobProperties blob = new BlobProperties("Jazure");
BlobContents blobContents = new BlobContents(new FileInputStream(new File("blobs/jazure")));
boolean result = container.putBlobImpl(blob, blobContents.asInputStream(), true);
Logger.getRootLogger().info(result);
List blobs =container.listBlobs("", false);
for(Object o:blobs){
if(o instanceof BlobProperties){
Logger.getRootLogger().info(((BlobProperties)o).getName());
}
if(o instanceof String){
Logger.getRootLogger().info(o);
}
}
// downloading a blob
container.getBlobImpl("Jazure", new FileOutputStream(new File("blobs/downloaded/Jazure")));
Using the Queue Storage is also straightforward, the following sample Java code shows how to interact with queue storage in JAzure.
// creating the Queue Storage entry point
// In this example we are using the development Queue Storage.
String endPoint = "http://127.0.0.1:10001/devstoreaccount1";
String accountName = "devstoreaccount1";
String accountSharedKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
QueueStorage queueStorage = new QueueStorage(endPoint,accountName, accountSharedKey);
// listing queues
List<Queue> queues = queueStorage.listQueues();
Logger.getRootLogger().info(queues.size());
for (Queue queue : queues) {
Logger.getRootLogger().info(queue.getQueueName());
}
// creating a new queue
Queue q = queueStorage.getQueue("foo");
q.create();
// inserting a String message in the queue
q.putMessage(new QueueMessage("Hello World"));
// listing messages in the queue
List<QueueMessage> messages = q.getMessages(5);
for(QueueMessage qmsg:messages){
Logger.getRootLogger().info(qmsg);
}
// deleting the queue.
q.delete();
The table storage support in JAzure is different from that in the StorageClient sample shipped with the .Net Azure SDK. The latter exposes the table storage as services defined by the .NET Client Library for ADO.NET Data Services, however, we provide a convenient straightforward way to create table entities and query the table store.
// initialising the table storage
TableStorage tableStorage = new TableStorage();
Table myTable = tableStorage.getTable("myTable");
myTable.clear();
// Create a new entity using a PK and a row key.
TableStorageEntity entity = new TableStorageEntity("11","xxx");
entity.getProperties().put("employeeName",new TableStorageProperty(TableStorageProperty.EdmString,"Martin"));
entity.getProperties().put("employeeSalary",new TableStorageProperty(TableStorageProperty.EdmInt32,new Integer(35000)));
// Inserting a new entity
Logger.getRootLogger().info(myTable.insertEntity(entity));
// Get an entity (pk-rk)
TableStorageEntity ent = myTable.getEntity("11", "xxx");
Logger.getRootLogger().info(ent);
// Updating an existing entity
entity.getProperties().put("employeeSalary",new TableStorageProperty(TableStorageProperty.EdmInt32,new Integer(37000)));
Logger.getRootLogger().info(myTable.updateEntity(entity));
ent = myTable.getEntity("11", "xxx");
Logger.getRootLogger().info(ent);
// Query entities as per http://msdn.microsoft.com/en-us/library/dd894031.aspx
List<TableStorageEntity> entites = myTable.queryEntities("PartitionKey%20eq%20'11'");
Logger.getRootLogger().info("The query returned "+entites.size()+ " entities");
for(TableStorageEntity e: entites){
Logger.getRootLogger().info(e);
}
Source code and binary distributions are available from Codeplex. For more information, see http://jazure.codeplex.com. The source code distribution includes sample code and JavaDoc based documentation.