I have a couchbase database that is shared between multiple applications, storing documents as json. I cannot seem to get data into my java app, since it appears to be dependent on native java binary serialization.
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. None of the available transcoders seem to give me this.
The couchbase docs have an example for setting json, but none for reading it. How are people reading json into 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. Michael's blogs are a good read if you want to see what's happening here. 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. 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. I have a sample transcoder you can use for this from a few weeks ago.
Make sure you are using latest CB java client:
<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
I have my service that uses CB client running just fine. 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:
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. Perhaps it has something to do with the format of the document itself.
Related
Hello I have a problem to parse any JSON in IIB Toolkit. The exception thrown by java compute node is: java.lang.NoClassDefFoundError: org.json.JSONObject
I am parsing incoming JSON messages in UTF-8. I already tried to get them in JSON, but accepting them as BLOB and converting to JSON UTF-8 works for me.
String messageText = new String(outMessage.getRootElement().getLastChild().getLastChild().getValueAsString());
messageText = new String(DatatypeConverter.parseHexBinary(messageText),"UTF-8");
JSONObject json = new JSONObject("{}");
I would love to create JSON object from JSON string in UTF-8
Many thanks in advance!
So what you are trying to do is a bit of a no-no. You're trying to use the Java class JSONObject rather than using the builtin IIB Java Parser.
Have a look at MbElement in particular the methods createElementAsLastChild(java.lang.String parserName) and createElementAsLastChildFromBitstream.
As per my earlier answer never forget your are trying to build a tree of elements.
One other trick I sometimes use is to build a sample output message and send it to an Input node connected to a Trace node. I then use the Trace node output to write code to build my actual output tree, you can even put a Trace node after your JavaCompute node to see what the Element tree you've currently built looks like and correct your mistakes. I mostly use this method for SOAP messages which can be quite complex.
If you really want to use external Java classes then search for Using JAXB with a JavaCompute node and follow the links from that article.
I've been trying to convert this statement from C# to JAVA, without much luck. Yes I've search Azure, stackoverflow, and such, but can't find a good example of READDOCUMENT in java.
UserInfo response = _DocumentClient.ReadDocument<UserInfo>(UriFactory.CreateDocumentUri(_DBConfiguration.DatabaseID, _DBConfiguration.DocumentCollectionId, pPhoneNumber));
This is as far as I've gotten:
UserInfo returnUserInfo = null;
try {
//todo: document link is incorrect, but reference pPhoneNumber as key
ResourceResponse<Document> response = _DocumentClient.readDocument(<<NEED To generate URI>>,null);
if (response != null) {
Document returnUserInfoDocument = response.getResource();
returnUserInfo = <<I have a document, but can't cast it to USERINFO>>;
}
}
catch (DocumentClientException ex) {
if (!ex.getError().getCode().equals("NotFound")) {
throw ex;
}
}
return returnUserInfo;
I don't personally have experience with the Java SDKs but a little bit of googling turned up this. There are plenty of usage examples inside this repo.
The parts you're struggling with are generating the Document URI. It appears that in C# this is abstracted away a bit with some factory methods that build the string for you but you can see in the example linked that in Java the equivalent would be:
String documentLink = String.format("/dbs/%s/colls/%s/docs/%s", databaseName, collectionName, family.getId());
this.client.readDocument(documentLink, new RequestOptions());
So in your case you would just use the phone number field (or whatever your document Id in this format with your document and collection Ids.
As far as converting the response to a UserInfo, you can just treat the responses as a JSON string and use your JSON deserialization library of choice to do the work for you. In C# we use Newtonsoft but there must be plenty of options in Java. Just try googling java json deserialize and I'm sure you'll be able to find lots of resources.
Just for the record I didn't downvote your question, but I can understand why people did and maybe you can take this as an opportunity to grow. I literally typed documentdb java into google and the first two links are GitHub repos full of code samples. You should attempt to search a little bit harder before expecting others to do your research for you.
If you just want to how to use Azure DocumentDB SDK for Java to access the DocumentDB, you can refer to the tutorial Azure Cosmos DB: Build a DocumentDB API app with Java and the Azure portal and the README content of azure-documentdb-java on GitHub.
Hope it helps. Any concern, please feel free to let me know.
I am new to REST , JSON and Jersey usage. Now we got a requirement where we need to implement a Client which need need to query the server with JSON request, GET and going to receive a JSON String/Object, which need to be parsed and converted into local datastructure.
Could someone help me for the below points?
What I need to download for implementing REST Client application for supporting JSON ( we have restrictions to use Jersey apis, and no other third-party apis)
Sample java client code for requesting and parsing the JSON data.
"What do I need ?", It really depends of the technology you want to use with it .. eg, if you use glassfish (netbeans server), you can use their jersey "org.glassfish.jersey.jackson.JacksonFeature" as json provider and the other ressources glassfish includes. You won't need anything else for a simple implementation.
As you are a beginner, you better follow a tutorial like this one : http://java.dzone.com/news/simple-restful-web-services
If you want to convert String to a JSONObject -
Use this library - http://www.json.org/java/
JAR file is available at http://code.google.com/p/org-json-java/downloads/list
Use below code to convert a string to JSONObject -
JSONObject final_result = new JSONObject(result);
Here is an example OpenSearch description file:
http://webcat.hud.ac.uk/OpenSearch.xml
When I send a query as like that:
http://webcat.hud.ac.uk/perl/opensearch.pl?keyword=new&startpage=1&itemsperpage=20
I get a response which is compatible to OpenSearch. How can I implement OpenSearch specification at Java or is there any library for it or is there any xsd that I can generate a Java code from it?
According to the OpenSearch website's section on "Reading OpenSearch", there is a Java library which can do this, called Apache Abdera. I have not used it myself, so I cannot comment on its quality, but it should be worth looking into - apparently it can both interpret AND create OpenSearch responses, so this may be exactly what you're looking for.
Alternatively, there are quite a few very good XML parsers for Java (see this question for some suggestions), so writing your own parser for a simple OpenSearch XML file shouldn't be too difficult, since the full specification is available online.
As for an XSD, I can't find an "official" one, however there are XSD's for OpenSearch in various open source projects which have been tested and you can use, such as this one, which is part of a project called "OpenSearch Validator."
Another potential choice for writing OpenSearch results is the very mature and widely-used Apache Lucene library, which is in the list of software "writing OpenSearch results" in the previously linked OpenSearch website.
ROME also supports OpenSearch with its ROME Module A9 OpenSearch.
Sample usage:
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType(feedType);
// Add the opensearch module, you would get information like totalResults from the
// return results of your search
List mods = feed.getModules();
OpenSearchModule osm = new OpenSearchModuleImpl();
osm.setItemsPerPage(1);
osm.setStartIndex(1);
osm.setTotalResults(1024);
osm.setItemsPerPage(50);
OSQuery query = new OSQuery();
query.setRole("superset");
query.setSearchTerms("Java Syndication");
query.setStartPage(1);
osm.addQuery(query);
Link link = new Link();
link.setHref("http://www.bargainstriker.com/opensearch-description.xml");
link.setType("application/opensearchdescription+xml");
osm.setLink(link);
mods.add(osm);
feed.setModules(mods);
// end add module
I am trying to port this bit of Python code to Java http://www.bemasher.net/archives/1002 (scroll down to the bottom for the code)
But because everything is dynamically typed, I'm having difficulty porting this. I need to be able to use a Java implementation of Pythons json. Right now I'm using gson ( http://code.google.com/p/google-gson/ ), but I'm open to anything.
Neither of gson's fromJson's signatures
public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException
match Pythons
data = json.loads(response)
This is the JSON that I am trying to read: http://www.ows.newegg.com/Stores.egg/Categories/1
How would I go about getting the class or type of the JSON above and read through it? All the other examples I've seen you have to know what it is.
Thanks!
It basically fetches http://www.ows.newegg.com/Stores.egg/Menus and dumps its contents to a string (response), then it parses the JSON using json.loads() and puts that into data.
Then it will iterate through the contents of data and build a HTML list using lxml. The list itself should look like:
<ul>
<li>{StoreTitle}</li>
...
</ul>
Where {StoreID} and {StoreTitle} are the IDs and titles fetched and parsed from that URL.
References: urllib2, json, lxml
I forgot to mention that I was using Google Web Toolkit - sorry about that. I went ahead and accepted #NullUserException 's answer though I'm writing this for anyone else who may come across this.
When converting this from Python to the Java (GWT) equivalant, I found that there was already support in GWT for JSON, and even better it works on the client side saving trips to the server. (source: JSON GWT API)
To keep thoe code clean I am using a wrapper for it that can be found on Google Code here.