How to store raw JSON in Couchdb with java - java

I am currently parsing a web service that give me Json Documents as response. I want to store those in CouchDb using java, but I cannot find a way. With the couchdb library for java (Ektorp, couchdb4j etc...) I can only store java documents in the database, which would mean I have to transform my raw json to java Document in order to store them.
Do you have any idea how i could directly store raw json ?
Many thanks in advance

The Ektop documentation provides an example of updating a document using JSON stored in a file:
File file = someMethodToGetFile();
InputStream jsonInputStream = new FileInputStream(file);
db.update("document_id",
jsonInputStream,
file.length(),
null);
Instead of using the FileInputStream, if your JSON is in memory in your java program (e.g. as a String), you could wrap the string in a ByteArrayInputSteam:
String yourJsonString = ...
InputStream jsonInputStream = new ByteArrayInputStream(yourJsonString.getBytes());
db.update("document_id",
jsonInputStream,
file.length(),
null);
Alternatively, CouchDB is accessed through a restful API so you can interact with CouchDB using any java library that understands REST, for example using Apache HttpClient:
String yourJsonString = ...
StringRequestEntity requestEntity = new StringRequestEntity(
yourJsonString,
"application/json",
"UTF-8");
PostMethod postMethod = new PostMethod("http://couchdb.server/database");
postMethod.setRequestEntity(requestEntity);
int statusCode = httpClient.executeMethod(postMethod);

Related

Android - Accessing JSON children from a URL

I'm in the process of converting my website to an Android app and one of the pages' data currently is populated via JSON in my website. The way it works is that the URL generates a different JSON data with the same structure based on the passed ID. I already have the logic for passing the ID to the URL. Now I want to read the data through Java code and parse the JSON children and its values in it.
I have a URL that leads to the JSON file in textual form, but I'm not sure how to go about reading the data from it and accessing the child nodes based on the JSON key.
So I guess what I'm asking is what is the usual approach for this procedure? I see a lot of different examples, but none of which are applicable to my problem.
Anyone have any suggestions as to how I should approach this?
JSONObject = new JSONObject(yourjsonstring);
Now you have your Json Object...
If your Json start with array use this:
JSONArray = new JSONArray(yourjsonarray);
You can use existing libraries to parse JSON, gson or Moshi are two solutions.
The way you go about parsing the JSON is as followed
First you need to make pojo's with the same structure as the JSON file.
then you can parse it to java code via the fromJSON() method, this will make new objects and fill it with the data from the JSON.
gson example for clarification:
Gson gson = new Gson();
Response response = gson.fromJson(jsonLine, Response.class);
where jsonLine = your json file and the Response.Class the pojo in which you want to json to load.
Now you have the JSON values as Java classes in response.
If you're using Retrofit and OkHTTP to perform the network calls i suggest you use Moshi as it's also from Square and claimed to work faster and better than gson. (if you want to know why you can leave a comment).
I think what you're trying to do is this
on post execute method do the following
#Override
protected void onPostExecute(String result) {
String status = "";
String message = "";
String tag = "";
String mail = "";
try {
JSONObject jsonResult = new JSONObject(result);
status = jsonResult.optString("status");
message = jsonResult.optString("message");
tag = jsonResult.optString("tag");
mail = jsonResult.optString("mail");
} catch (JSONException e) {
e.printStackTrace();
}
of course your json array contains different keys
Just reolace them with yours

How do I upload a pdf to elasticsearch when using the elastic search java client?

This link explains how to use the REST API to upload an attachment.
But I want to upload an attachment with the java client...
I assume the following classes are relevant (though I may be wrong)...
org.elasticsearch.ingest.IngestService
org.elasticsearch.ingest.PipelineStore
I realize that I can just fall back to the REST interface but I'd rather try and use the native client first...
Just send a BASE64 encoded PDF in a field like:
String base64;
try (InputStream is = YourClass.class.getResourceAsStream(pathToYourFile)) {
byte bytes[] = IOUtils.toByteArray(is);
base64 = Base64.getEncoder().encodeToString(bytes);
}
IndexRequest indexRequest = new IndexRequest("index", "type", "id")
.setPipeline("foo")
.source(
jsonBuilder().startObject()
.field("field", base64)
.endObject()
);
In case you are not aware of it, I'm also linking to FSCrawler project in case it solves something you want to do already.
Here is four options that you can use to index PDFs to ElasticSearch
Ingest Attachment Plugin
Apache Tika
FsCrawler
Ambar
Pros/cons described in this post

Parsing Request as json with stackexchange API

I am struggling with some issue related with http, java and the stackexchange API
consider the following url as string:
private static final String URLSTRING_2 = "http://freegeoip.net/json/";
if I write this url in my browser I get this answer as json:
now im trying to do that with java and only native libs, for that am using the snippet below wich is working so far so good...
If I parse the json and i try to get the value for the key "country_name" then the snippet prints as spected "Singapore"
public static void main(String[] args) throws Exception {
// Connect to the URL using java's native library
final URL url = new URL(URLSTRING_2);
final HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
// Convert to a JSON object to print data
final JsonParser jp = new JsonParser(); // from gson
final JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); // Convert the input stream to a json
// element
final JsonObject rootobj = root.getAsJsonObject(); // May be an array, may be an object.
final String country = rootobj.get("country_name").getAsString(); // just grab the zipcode
System.out.println("country_name: " + country);
}
Now my question:
if I do the same with this link
https://api.stackexchange.com//2.2/users/22656?order=desc&sort=reputation&site=stackoverflow&filter=!T6oHjO_RIWkfWpoL5g
my browser outputs the following json:
but if I try to parse the json I get an exception because am getting from the request this:
ý•#‡ž¼ÚRìØ1ôX`»v?±h[‹-¹’/+ò........
for something that is not even human readable...
do you know why?
Thanks in advance
The StackOverflow API GZIP-compresses its response. The reason you see that string of non-human-readable characters is that you are trying to read GZIP-compressed data without first decompressing it.
Your browser is capable of reading this header and doing the decompression itself. Your code isn't yet.
You can confirm that GZIP compression is used by displaying the value of the Content-Encoding header in the response. Adding the line
System.out.println(request.getContentEncoding());
will print out
gzip
Fortunately, fixing the problem is fairly straightforward. You need to wrap the InputStream you get from the request in a GZIPInputStream:
final JsonElement root = jp.parse(new InputStreamReader(new GZIPInputStream((InputStream) request.getContent()))); // Convert the input stream to a json
However, instead of the built-in Java classes, I'd recommend using a library such as Apache HTTPComponents Client for making your HTTP requests. In particular, a library such as this will automatically detect the content-encoding and do the decompression for you.

How to add xml file data into ArangoDb?

<InputParameters>
<Textbox>
<Text>ABCD</Text>
<Text>EFGH</Text>
<Text>HIJK</Text>
</Textbox>
</InputParameters>
Suppose i have to add this xml file data into arangodb. How would one able to do so ?
Since version 2.7.1 the aragodb-java-driver supports writing (createDocumentRaw(...)) and reading (getDocumentRaw(...)) of
raw strings.
Example:
arangoDriver.createDocumentRaw("testCollection", "{\"test\":123}",
true, false);
With JsonML you can convert a XML string into a JSON string and store it into ArangoDB:
// writing
String xml = "<recipe name=\"bread\" prep_time=\"5 mins\"</recipe> ";
JSONObject jsonObject = JSONML.toJSONObject(string);
DocumentEntity<String> entity = arangoDriver.createDocumentRaw(
"testCollection", jsonObject.toString(), true, false);
String documentHandle = entity.getDocumentHandle();
// reading
String json = arangoDriver.getDocumentRaw(documentHandle, null, null);
JSONObject jsonObject2 = new JSONObject(str);
String xml2 = JSONML.toString(jsonObject2));
You can find more examples in the arangodb-java-driver git repository.
There are two proper solutions.
One is to put the whole XML into an attribute of a document. This will then in term probably be not good for doing AQL queries on the payload of the xml.
Another possible approach could be to use jsonml to translate your xml into structured json documents, and store them using their java library. I don't know how well this scales on complex XML like SOAP though.
You could then create AQL queries to work on that collection and FILTER for attributes of the source XML.

Simple JSON-String Query

I am developing a web-app using AJAX requests on the client-side and Servlets on the server-side.
My aim is to send objects of Javascript to server, then do some manipulations there and send it back to show here.
Let's say my js object is
var obj={hero:"Spiderman",name:"Peter Parker"};
My Approach
1.Convert obj to JSON string and send
var str= JSON.stringify(obj);
xmlhttp.open("POST",myurl,true);
xmlhttp.setRequestHeader("Content-Type","application/json",true);
xmlhttp.send("data="+str);
2. Recieve string,convert this back to JSON, manipulate "name" to "Bruce Wayne" and send it back as string
3.Recieve and convert back to Json
var data= JSON.parse(xmlhttp.responseText);
I am struggling at second point.I am using org.json for it .I searched and read docs but could not find satisfied answer for converting string to json and vica-versa in JAVA in my context.
It would be really helpful one could provide simple working code or point to some links where I can study.
P.S :
I cannot use Jquery as I am using AngularJS. See Why?
I will always send valid JSON string.
I can use other JSON lib. if its good than org.json and satisfy my needs.
Please provide its jar download link.
Assuming you are able to pull out data in your server code
This is how you can do it using org.json:
JSONParser parser = new JSONParser();
JSONObject requestObj = (JSONObject) parser.parse(data);
String name = (string)requestObj.get("name");
name = "Bruce Wayne";
Code to create the response can look something like this:
JSONObject response = new JSONObject();
response.put("name",name);
return response.toJSONString();
This assumes your server method returns a String type
And in case if you are using Servlet you can use HttpServletResponse object res to create response like:
res.setContentType("application/json");
OutputStream os = res.getOutputStream();
os.write(response.toString().getBytes());
os.close();

Categories

Resources