Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Couchbase to Store Non-JSON Data

DZone's Guide to

Using Couchbase to Store Non-JSON Data

Wondering if and/or how you can store non-JSON documents in Couchbase? Can XML and binary data, too, have a home in Couchbase? Read on to find out the answer!

· Database Zone
Free Resource

Learn how to move from MongoDB to Couchbase Server for consistent high performance in distributed environments at any scale.

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.

// insert document
bucket.Insert<MyType>("JSON_" + guid, new MyType { Foo = "BarJSON"});
// get the document back out and display it
var jsonBackOut = bucket.Get<MyType>("JSON_" + guid).Value;
Console.WriteLine($"JSON document: {jsonBackOut.Foo}");

Storing XML

Storing XML takes a little more work. First, I serialize 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.

var xmlo = new MyType {Foo = "BarXML"};
var xml = new XmlSerializer(xmlo.GetType());
using (var textWriter = new StringWriter())
{
    xml.Serialize(textWriter, xmlo);
    bucket.Insert<string>("XML_" + guid, textWriter.ToString());
}
// get the XML back out, deserialize it, display object
var xmlBackOut = bucket.Get<string>("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 theBinaryFormatter instead of XmlSerializer.

var formatter = new BinaryFormatter();
using (var ms = new MemoryStream())
{
    formatter.Serialize(ms, new MyType { Foo = "BarDotNET"});
    bucket.Insert<byte[]>("byte_" + guid, ms.ToArray());
}
// get the bytes back out, deserialize them, display object
var bytesBackOut = bucket.Get<byte[]>("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:

Console output of sample program creating non-JSON values in Couchbase Server

After you run the above code examples, this is what you’ll see in Couchbase Console:

View of JSON and byte array values 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.

Want to deliver a whole new level of customer experience? Learn how to make your move from MongoDB to Couchbase Server.

Topics:
couchbase server ,json ,database ,n1ql

Published at DZone with permission of Matthew Groves, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}