DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Using Couchbase to Store Non-JSON Data

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!

Matthew Groves user avatar by
Matthew Groves
·
Oct. 10, 16 · Tutorial
Like (3)
Save
Tweet
Share
5.64K Views

Join the DZone community and get the full member experience.

Join For Free

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 the binaryformatter 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 .

Document Data (computing) Couchbase Server

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Discovery With Eureka
  • A Brief Overview of the Spring Cloud Framework
  • API Design Patterns Review
  • How Observability Is Redefining Developer Roles

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: