简体   繁体   English

如何在Java中从Couchbase获取json文档?

[英]How can I get json documents from couchbase in java?

I have a couchbase database that is shared between multiple applications, storing documents as json. 我有一个在多个应用程序之间共享的couchbase数据库,将文档存储为json。 I cannot seem to get data into my java app, since it appears to be dependent on native java binary serialization. 我似乎无法将数据输入我的Java应用程序,因为它似乎依赖于本机Java二进制序列化。

This code: 这段代码:

CouchbaseClient client = new CouchbaseClient(hosts,"bucket","");
System.out.println((String)client.get("someKey"));

results in 结果是

net.spy.memcached.transcoders.SerializingTranscoder:  Failed to decompress data
java.util.zip.ZipException: Not in GZIP format

since it is trying to deserialize by default. 因为它默认会尝试反序列化。 I notice that I can provide my own transcoder, but I really only want the raw string data so I can json parse it myself using gson or whatever. 我注意到我可以提供自己的转码器,但实际上我只想要原始字符串数据,因此我可以使用gson或其他内容对自己进行json解析。 None of the available transcoders seem to give me this. 可用的转码器似乎都没有给我这个。

The couchbase docs have an example for setting json, but none for reading it. 沙发文档有一个设置json的示例,但没有一个用于读取json的示例。 How are people reading json into java? 人们如何将json读入Java?

First off, this problem will go away soon in that the Couchbase "2.0 SDKs" implement common flags between each other so this kind of problem doesn't come up. 首先,这个问题很快就会消失,因为Couchbase“ 2.0 SDK”在彼此之间实现了通用标志,因此不会出现这种问题。 Michael's blogs are a good read if you want to see what's happening here. 如果您想看看这里发生了什么,可以阅读Michael的博客 The reason for the problem in the first place is that in the 1.x series, Couchbase was trying to stay compatible with existing application code and memcached. 首先,出现问题的原因是在1.x系列中,Couchbase试图与现有应用程序代码和内存缓存保持兼容。 In the memcached world, the clients were all written by different people at different times. 在内存缓存的世界中,客户端都是由不同的人在不同的时间编写的。

Based on the exception, I believe you're probably trying to read an item stored by .NET. 根据例外情况,我认为您可能正在尝试读取.NET存储的项目。 I have a sample transcoder you can use for this from a few weeks ago. 我有一个示例转码器,您可以在几周前使用它。

Make sure you are using latest CB java client: 确保您使用的是最新的CB Java客户端:

<dependencies>
  <dependency>
    <groupId>com.couchbase.client</groupId>
    <artifactId>couchbase-client</artifactId>
    <version>1.4.4</version>
  </dependency>
</dependencies>

see: Couchbase Java Client Library 1.4 请参阅: Couchbase Java客户端库1.4

I have my service that uses CB client running just fine. 我有使用CB客户端运行的服务。 Here is how I create client: 这是我创建客户端的方法:

CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
cfb.setOpTimeout(10000);
cfb.setOpQueueMaxBlockTime(5000);
CouchbaseClient client = new CouchbaseClient(cfb.buildCouchbaseConnection(baseURIs, bucketName, ""));

And here is an example how I get a raw string and convert it to POJOs: 这是一个示例,我如何获取原始字符串并将其转换为POJO:

MyPOJO get(CouchbaseClient client, String key)
{
  com.google.gson.Gson gson = new com.google.gson.Gson();
  String jsonValue = (String) client.get(key);
  return gson.fromJson(jsonValue, MyPOJO.class);
}

Also, update your question with the sample JSON doc that causing this issue. 另外,请使用导致此问题的示例JSON文档更新您的问题。 Perhaps it has something to do with the format of the document itself. 也许与文档本身的格式有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM