So I have a table
Node_Mapping(location_id:UUID, node_ids: jsonb)
The corresponding POJO for this is
class NodeMapping{
UUID locationId;
Set<String> nodeIds;
}
Example data in table is
UUID1 : ['uuid100', 'uuid101']
UUID2 : ['uuid103', 'uuid101']
So I want to make a query like, find out all the locationIds which contains 'uuid101'.
Please help me to form the query.
You can use the contains operator ?
select *
from node_mapping
where node_ids ? 'uuid100';
This assumes that in reality the column stores a valid JSON array, e.g. ["uuid100", "uuid101"] but not an invalid JSON like UUID1 : ['uuid100', 'uuid101']
Related
I'm trying migrate from postgres db to mongodb. I've a field JSON in postgres table, which has key and value pairs. I need to fetch the value based on JSON key and convert those values(values are basically postgres id) to Mongo objectID and then map those to mongo document.
JSON field looks like this
"B":["956c5b0a5341d14c23ceed071bf136f8"]}
I've written a function in java to convert postgres ID column to mongoID.
public String convertIDToMongo(String colName){
---encoding logic
}
This is working when the field is explicitly available in the table and also if the field datatype is not an array. In case of JSON and array, is there a way to fetch set of values from JSON field and convert them into Mongo Object ID?
"Select json::json->'A' from table" ```
gives me the value ["06992a6fef0bcfc1ede998ac7da8f08b","7d1d55e1078191e53244c41d6b55b3a3","31571835c6600517f4e4c15b5144959b"] But I need some help to convert this list of IDs to Mongo Object IDs. Below is how I convert for non JSON field with one postgres ID value to Mongo ID.
"Select " + convertIDToMongo('colName') + " as "_id", \n" +
Any help is greatly appreciated. Thanks.
You need to parse the JSON array, then call your converter for each item in the array. There are many JSON parsing options available.
Good day. I am deploying a streaming job to insert data from Spark (Scala) to Postgres.
df.select("col1","col2").write.mode(SaveMode.Append).jdbc(url, "tableName", connectionProperties)
Here col2 is having uuid values in the dataframe df, but it is a string datatype. When it tries to insert in to the table that has col2 column defined as type uuid its failing with the Column is of type uuid but expression is of type character varying . Can someone help me with a workaround for this. ?
Thank you.
When you have column column in dataframe that has string data type but had values of type UUID then you can set the specific property in your connection properties and that will allow you to save your dataframe. If you don't set that property it will not allow you to save the data in the sql/postgres table, if the column has datatype of UUID.
You can set this property in the Connection Properties
connectionProps.setProperty("stringtype", "unspecified")
Setting the above property will allow you to save the string type column having UUID values in the dataframe to a column of type UUID in the postgres database.
In Oracle, Datatype is vArchar and we have some ID's stored in format 0000123. From source we are getting ID as 123 or 1234 with preceding 0. In some cases data is stored simply as 123.
In SQL Query i can simply use
Select * from Table where ID = 123 (It wil fetch even if 000123 id is present)
Is there way to achieve it using Named Query through hibernate as currently in oracle, it is String against Varchar and when searching 123 does not return correct results.?
The query translates automatically to the following:
Select * from Table where TO_NUMBER(ID) = 123
so you can use that instead.
You can also use:
SELECT * from Table where LTRIM(ID,'0') = LTRIM(123,'0')
if there are non numerical values in the ID column.
If you are getting an error while applying Radagast81's answer then try to convert the number to varchar using to_char() as following:
SELECT
*
FROM
EMPS
WHERE
LTRIM(EMP_NAME, '0') = TO_CHAR(LTRIM(123, '0'));
You can also use LPAD to achieve the same:
SELECT
*
FROM
EMPS
WHERE
EMP_NAME = LPAD(123, LENGTH(EMP_NAME), '0'));
Cheers!!
If you can assign a default number which does not exists in table, you can use it. see the example below:
Select * from Table where TO_NUMBER(ID DEFAULT -999999999999 ON CONVERSION ERROR ) = 123
Reference: https://www.postgresql.org/docs/current/functions-json.html
I'm looking through this and I'm not seeing what I am looking for, but what I want to do is this. I have a Java application that generates an object:
{"first":"Joe","last":"Doe"}
I want to query the database to find json objects with these fields. These fields are NOT static (I could have n fields and they can all be different).
select op.*
from bw.people p
where p.object_as_json = '{"first":"Joe","last":"Doe"}' -- this clearly doesn't work
Update
object_as_json is now a jsonb data type
One entry in person table, has
object_as_json ='{"first":"Joe","last":"Doe","middle":"S","DOB":"1940-01-01"}'
My queries were (neither of these returned anything):
select *
from bw.people
where object_in_json = '{"last":"Doe","first":"Joe"}'
select *
from bw.people
where object_in_json = '{"first":"Joe","last":"Doe","middle":"S","DOB":"1940-01-01"}'
If you used jsonb, you could write
WHERE object_in_json #> '{"last":"Doe","first":"Joe"}`
With json you'll have to write
WHERE object_in_json ->> 'last' = 'Doe'
AND object_in_json ->> 'first' = 'John'
I have a List of ids:
List<Object> ids;
I need to use this in a criteria query to get all rows with an id that ids contains.
What I have now and works:
if (ids.size() > 0) {
for (Object id : ids) {
preparedResults.add((T)sessionMngr.getSession().
createCriteria(rowType).add(Restrictions.idEq(id))
.uniqueResult());
}
}
So I fetch them one by one, which isn't optimal, but I first tried something like following to get them in one query, but don't know what to put at the ???:
preparedResults.addAll(sessionMngr.getSession().
createCriteria(rowType).
add(Restrictions.in(???, ids)).list());
The first argument of Restrictions.in() is of type String. I can't put a hard coded "id" there as I don't know what the propertyname is.
So I don't know how to get the id property as a String there.
I saw Projections.id(), but I am not sure if I can use this to get it as a String.
With this solution you can retrieve the name of the id field of your entity. If you use annotations you can have it even shorter as described here. If you donĀ“t use composite primary keys you could also use
ClassMetadata classMetadata = getSessionFactory().getClassMetadata(myClass);
string identifierPropertyName = classMetadata.getIdentifierPropertyName();
Source of this snippet: here