I believe most of the power of Couchbase Server comes when you use it as a document database to store JSON documents. You get to use N1QL, for instance! However, there are some times when you need Couchbase to store something else. In this blog post, I’m going to show you how to store XML and binary data into Couchbase. I’ll be using the .NET SDK, but I believe the other SDKs also support these operations.
Review storing JSON documents
A quick review of storing JSON documents in Couchbase. Once you have a bucket, you can just use Insert
/Upsert
to create/update a document, and then use Get
to get the document back out. With the .NET SDK, the serialization is handled automatically to the type you specify.
1 2 3 4 5 |
// insert document bucket.Insert("JSON_" + guid, new MyType { Foo = "BarJSON"}); // get the document back out and display it var jsonBackOut = bucket.Get("JSON_" + guid).Value; Console.WriteLine($"JSON document: {jsonBackOut.Foo}"); |
Storing XML
Storing XML takes a little more work. First, I seralize an object into an XML string, using XmlSerializer
. Then, I Insert
that value as a string. To get it back out, I use the XmlSeralizer
again to go from string to a type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var xmlo = new MyType {Foo = "BarXML"}; var xml = new XmlSerializer(xmlo.GetType()); using (var textWriter = new StringWriter()) { xml.Serialize(textWriter, xmlo); bucket.Insert("XML_" + guid, textWriter.ToString()); } // get the XML back out, deserialize it, display object var xmlBackOut = bucket.Get("XML_" + guid).Value; using (var reader = new StringReader(xmlBackOut)) { var xmlObject = (MyType)xml.Deserialize(reader); Console.WriteLine($"XML: {xmlObject.Foo}"); } |
Storing a byte array serialization
Next, I’m going to serialize an object into a byte
array. Unlike JSON and XML, storing in a byte array means that the object can only be serialized back into a .NET object.
The process is similar to XML, except Insert
and Get
will specify byte[]
instead of string
, and I’m using the BinaryFormatter
instead of XmlSerializer
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var formatter = new BinaryFormatter(); using (var ms = new MemoryStream()) { formatter.Serialize(ms, new MyType { Foo = "BarDotNET"}); bucket.Insert("byte_" + guid, ms.ToArray()); } // get the bytes back out, deserialize them, display object var bytesBackOut = bucket.Get("byte_" + guid).Value; using (var stream = new MemoryStream(bytesBackOut)) { var bytesObject = (MyType)formatter.Deserialize(stream); Console.WriteLine($".NET: {bytesObject.Foo}"); } |
Summary
Running the sample console program (source code is available on Github) produces:
After you run the above code examples, this is what you’ll see in Couchbase Console:
The non-JSON documents exist in the same bucket as the JSON documents. But as you can see, Couchbase Server doesn’t know how to interpret them. So, you won’t be able to perform most N1QL operations on these documents. You can’t index their values like in a JSON document. And, in the case of the .NET byte array, a non-.NET program won’t be able to interpret them at all.
If you can store your values in JSON, I’d recommend it. But Couchbase Server gives you the flexibility to store other types of values.
Got questions? Need help with Couchbase Server? Check out the Couchbase Forums or Follow me on Twitter.