Skip to content

Commit d9896cc

Browse files
committed
README: Add some sample code
1 parent 57c226e commit d9896cc

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,67 @@ To use this SDK to call Azure DocumentDB, you need to first [create an account](
4747

4848
You can follow this [tutorial](http://azure.microsoft.com/en-us/documentation/articles/documentdb-java-application/) to help you get started.
4949

50+
```java
51+
import java.util.Collection;
52+
53+
import com.google.gson.Gson;
54+
import com.microsoft.azure.documentdb.ConnectionPolicy;
55+
import com.microsoft.azure.documentdb.ConsistencyLevel;
56+
import com.microsoft.azure.documentdb.Database;
57+
import com.microsoft.azure.documentdb.Document;
58+
import com.microsoft.azure.documentdb.DocumentClient;
59+
import com.microsoft.azure.documentdb.DocumentClientException;
60+
import com.microsoft.azure.documentdb.DocumentCollection;
61+
62+
public class SampleApp {
63+
// Replace with your DocumentDB end point and master key.
64+
private static final String END_POINT = "[YOUR_ENDPOINT_HERE]";
65+
private static final String MASTER_KEY = "[YOUR_KEY_HERE]";
66+
67+
// Define an id for your database and collection
68+
private static final String DATABASE_ID = "TestDB";
69+
private static final String COLLECTION_ID = "TestCollection";
70+
71+
// We'll use Gson for POJO <=> JSON serialization for this sample.
72+
// Codehaus' Jackson is another great POJO <=> JSON serializer.
73+
private static Gson gson = new Gson();
74+
75+
public static void main(String[] args) throws DocumentClientException {
76+
// Instantiate a DocumentClient w/ your DocumetnDB Endpoint and AuthKey.
77+
DocumentClient documentClient = new DocumentClient(END_POINT,
78+
MASTER_KEY, ConnectionPolicy.GetDefault(),
79+
ConsistencyLevel.Session);
80+
81+
// Define a new database using the id above.
82+
Database myDatabase = new Database();
83+
myDatabase.setId(DATABASE_ID);
84+
85+
// Create a new database.
86+
myDatabase = documentClient.createDatabase(myDatabase, null)
87+
.getResource();
88+
89+
// Define a new collection using the id above.
90+
DocumentCollection myCollection = new DocumentCollection();
91+
myCollection.setId(COLLECTION_ID);
92+
93+
// Create a new collection.
94+
myCollection = documentClient.createCollection(
95+
myDatabase.getSelfLink(), myCollection, null).getResource();
96+
97+
// Create an object, serialize it in to JSON, and wrap it in to a
98+
// document.
99+
SomePojo somePojo = new SomePojo();
100+
String somePojoJson = gson.toJson(somePojo);
101+
Document myDocument = new Document(somePojoJson);
102+
103+
// Create a new document.
104+
myDocument = documentClient.createDocument(myCollection.getSelfLink(),
105+
myDocument, null, false).getResource();
106+
107+
}
108+
}
109+
```
110+
50111
Additional samples are provided in the unit tests.
51112

52113
##Need Help?

0 commit comments

Comments
 (0)