Using JOOQ formatJSON to get column:value pairs - java

I'd like my formatJSON() result to be column:value pairs.
[{"ID":1,"AUTHOR_ID":1,"TITLE":"1984"},{"ID":2,"AUTHOR_ID":1,"TITLE":"Animal Farm"}]
This blog post (https://blog.jooq.org/2018/01/) suggests the result is possible by setting a formatting option flag somewhere, but I am unable to find how to specify that option. I am just getting the default (?) output:
{"fields":[{"schema":"sss","table":"ttt","name":"ccc1","type":"zzz"},{"schema":"sss","table":"ttt","name":"ccc2","type":"zzz"}],"records":[[1,"x"]]}
I am using jOOQ 3.7.0, but can upgrade if needed.

I am using jOOQ 3.7.0, but can upgrade if needed.
There's your answer. Upgrade to 3.9 or more to profit from #5372. You can then call Formattable.formatJSON(JSONFormat) like this:
String json = result.formatJSON(new JSONFormat()
.header(false)
.recordFormat(RecordFormat.OBJECT));

Related

Couchbase Java SDK N1QL UPDATE issue

I'm experimenting with Java and Couchbase 6.0 Community edition using Java 2.7 SDK.
I'm trying to execute a simple update query from my java application the Couchbase Java 2.7 SDK:
String query ="UPDATE admin SET FIELDNAME='TEST'"
N1qlParams params = N1qlParams.build().adhoc(false);
N1qlQuery nquery = N1qlQuery.simple(query, params);
N1qlQueryResult nqr= this.rbucket.query(nquery);
And I am getting the following exception (the most meaningful part):
com.couchbase.client.core.CouchbaseException: N1qlQuery Error - {"msg":"syntax error - at UPDATE","code":3000}
The actual exception starts like this:
Exception in thread "main" com.couchbase.client.core.CouchbaseException: Error while preparing plan
Of course - this query works fine through the Couchbase web UI and I can update without problem.
Just for info: I tried escaping the single quotes, even tried setting the column to be equal to itself - same error.
Select queries are executed in a similar manner without any problem.
"admin' is not a good name for a bucket, as it is usually related to some reserved keywords, if you still want to use this name, you have to use backticks around it:
update `admin` set FIELDNAME = 'TEST'
It also might ask you to create a primary index if you don't have one yet.

Aggregation with MongoDB 3.6 and Morphia 1.3.2

I am trying to aggregate a MongoDB (3.6) using Morphia (1.3.2).
Currently it is a simple match and unwind to understand Morphia's API.
The problem that I am facing however is related to MongoDB 3.6:
Changed in version 3.4: MongoDB 3.6 removes the use of aggregate command without the cursor option unless the command includes the explain option. Unless you include the explain option, you must specify the cursor option.
This paragraph comes directly from the MongoDB documentation. MongoDB Aggregate.
This means that a cursor is mandatory for the aggregate to work. However, I can't find a way to do this using Morphia. Therefore my aggregate does not work.
AggregationPipeline data = aggregation.match(query).unwind("data");
Iterator<LoraHourData> out = data.aggregate(Data.class);
The error that is produced using above code is as follows :
Command failed with error 9: The cursor option is required, except for aggregate with the explain argument on server localhost:27017. The full response is { ok : 0.0, errmsg: The cursor option is required, except for aggregate with the explain argument", code : 9, codeName : FailedToParse }

Java OData Service does not accept plus (+) signs in URI

I am trying to issue the following query using Apache Olingo for OData using Java:
URI customersUri = client.newURIBuilder(serviceRoot)
.appendEntitySetSegment("Customers")
.filter("CustomerID eq 'Joe'")
.build();
The expected query string that I want is: $filter=CustomerID eq 'Joe'
However, when the library builds the above URI, the actual query string becomes like this:
%24filter%3DCustomerID+eq+%27Joe%27
Now, the problem is that when I use this query string for my OData Service, it seems that it does not accept the plus (+) signs. However, when removing the plus signs and use space instead, it works
Any help about this or recommendations please? Thanks
This is a bug that was fixed in the V4 4.0.0-beta-03 release. Maybe your client library is out of date.

Ignore an Ebean Version in certain situations

I'm using a Version field with Ebean, in Play Framework 2.2, but in certain situations I would actually rather have the version of an object not be updated. Is this at all possible?
So someone has an account on my website and they're looking at a post of another user. If the user updates that post, it's not automatically reloaded in the frontend. Please don't suggest I do this to solve the problem, I can't do it that way.
The problem is when a user gives the post a rating, the PUT call is refused if the user updated the post recently.
Is there a way to force Ebean to ignore the version field in specific situations like this?
Please don't suggest I do this to solve the problem, I can't do it that way.
LOL, nobody's gonna to suggest it to you :)
Custom statement should avoid updating the version:
SqlUpdate update = Ebean.createSqlUpdate("UPDATE post set likes = likes+1 where id = :id");
update.setParameter("id", post.id).execute();
(tested it, works as required)
As an alternative approach to using SqlUpdate in Ebean 4.x you can use 'stateless updates'. Note that for the counter = counter + 1 use case SqlUpdate is still a better fit.
Customer customer = new Customer();
customer.setId(42);
customer.setName("Please update the name")
// customer.setVersion(2) ... not setting the version property
// perform stateless update (and the version property was not set)
Ebean.update(customer);
// effectively results in:
update customer set name = 'Please update the name' where id = 42

How can I configure my index to use BM25 in ElasticSearch using the JAVA API?

I'm trying to migrate from MySQL database to ElasticSearch so I can use the full-text search technique using BM25 similarity over each fields. I'm using JAVA to fetch entries from MySQL and add them into the ElasticSearch index.
I'm building my index using the JAVA index API, but I can't figure out a way to set the BM25 similarity over my fields.
I consider a table products table from MySQL and dev as an index with products as it's index type.
The original table products contains the following fields :
id
title
description
You can find the code on my Github, If you'd like to take a look.
It's forked project that I've configured with Maven integration.
Any suggestion and any help is welcome, Thanks!
I found the answer for my question.
Here is the code :
Settings settings = ImmutableSettings
.settingsBuilder()
.put("cluster.name", "es_cluster_name"))
// Define similarity module settings
.put("similarity.custom.type", "BM25")
.put("similarity.custom.k1", 2.0f)
.put("similarity.custom.b", 1.5f)
.build();
Client client = new TransportClient(settings);
It seems that you can define the similarity modules you wish to use in the Settings before you instantiate your Client.
Here is the list of similarity modules that are supported by elasticsearch for the moment :
default, BM25, DFR, IB, LMDirichlet and LMJelinekMercer. You can specify which one you want to use in the Settings like below :
.put("similarity.custom.type", "..." )
Each similarity has its own parameters which you would want to configure as well in order to use it properly.
Note: Code tested on elasticsearch 1.1.0.

Categories