未加星标

Using Couchbase to Store Non-JSON Data

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

I believe most of the power ofCouchbase Server comes when you use it as a document database to store JSON documents. You get to useN1QL, 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 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.

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:


Using Couchbase to Store Non-JSON Data

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


Using Couchbase to Store Non-JSON Data

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 theCouchbase Forums or Follow me on Twitter .

本文数据库(综合)相关术语:系统安全软件

主题: XMLGitTwitter
分页:12
转载请注明
本文标题:Using Couchbase to Store Non-JSON Data
本站链接:http://www.codesec.net/view/480594.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(32)