Query Json Objects using Hazelcast SqlPredicate - java

I'm using hazelcast in memory in my application.
Can anyone please explain how to query JSON objects using hazelcast..
map(String, new(HazelcastJsonValue());
In the value i'm storing entire JSON.
Storing JSON one by one in value:-
{"id":"01","name":"abc"}
{"id":"02","name":" data"}
{"id":"03","name":"abc"}
query:- name='abc'
Selecting based on the name
query:- name='abc'
Expecting output:-
{"id":"01","name":"abc"}
{"id":"03","name":"abc"}
how to do this using hazelcast?
Thank you.

This link (sent by #Neil) is good. In your case, it will look like this:
HazelcastInstance instance = Hazelcast.newHazelcastInstance();
String item1 = "{\"id\":\"01\",\"name\":\"abc\"}";
String item2 = "{\"id\":\"02\",\"name\":\" data\"}";
String item3 = "{\"id\":\"03\",\"name\":\"abc\"}";
IMap<String, HazelcastJsonValue> map = instance.getMap("jsonValues");
map.put("1", new HazelcastJsonValue(item1));
map.put("2", new HazelcastJsonValue(item2));
map.put("3", new HazelcastJsonValue(item3));
Collection<HazelcastJsonValue> selected = map.values(Predicates.equal("name", "abc"));
System.out.println(selected);

Related

How to map Json (from procedure) to Java object

I have the following SP (SQL server) that return a Json output.
BEGIN
SET #jsonOutput = (
SELECT
Program.Name AS ProgramName,
ProgramOwner.FirstName AS OwnerFirstName,
FROM ProgramOwner, Program
WHERE Program.Id = ProgramOwner.ProgramOwner2Program
FOR JSON PATH,WITHOUT_ARRAY_WRAPPER)
I would like to map the return Json output to a List of ProgramDto via modelMapper. Not sure hot to do that since the return values from call.execute is an Object.
Something like this:
SimpleJdbcCall call = new
SimpleJdbcCall(jdbcTemplate).withProcedureName(programProc).declareParameters(
new SqlOutParameter("jsonOutput", Types.VARCHAR));
Map<String,Object>out = call.execute(new MapSqlParameterSource());
if(out.size()>0) {
// Only to show what I am trying to do
Type rootType = new TypeToken<List<ProgramDto>>() {}.getType();
modelMapper.map(out.get("jsonOutput"),rootType );
}
Thank you
As I understood you are trying to get a list of object from
You can use Jackson api
Like this
say for example your json is in variable named jsonData, then you can get the object you need like below.
ObjectMapper mapper = new ObjectMapper();
List<Type> myList = Arrays.asList(mapper.readValue(jsonData, Type[].class));
You can also find more examples here

What is the best way to pass multiply parameters as one http query parameter?

I have java web application (servlet) that does user authentication using SalesForce Server OAuth Authentication Flow. This OAuth Authentication provides "state" query parameter to pass any data on callback. I have a bunch of parameters that I want to pass through this "state" query param. What is the best way to do it? In java in particularly?
Or in other words, what is the best way to pass an array or map as a single http query parameter?
You can put all in json or xml format or any other format and then encode in base64 as a one large string. Take care that params can impose some hard limit on some browser/web server.
So, I have done it this way. Thank you guys! Here are some code snippets to illustrate how it works for me:
// forming state query parameter
Map<String, String> stateMap = new HashMap<String, String>();
stateMap.put("1", "111");
stateMap.put("2", "222");
stateMap.put("3", "333");
JSONObject jsonObject = new JSONObject(stateMap);
String stateJSON = jsonObject.toString();
System.out.println("stateJSON: " + stateJSON);
String stateQueryParam = Base64.encodeBase64String(stateJSON.getBytes());
System.out.println("stateQueryParam: " + stateQueryParam);
// getting map from state query param
ObjectMapper objectMapper = new ObjectMapper();
stateMap = objectMapper.readValue(Base64.decodeBase64(stateQueryParam.getBytes()), LinkedHashMap.class);
System.out.println("stateMap: " + stateMap);
Here is output:
stateJSON: {"1":"111","2":"222","3":"333"}
stateQueryParam: eyIxIjoiMTExIiwiMiI6IjIyMiIsIjMiOiIzMzMifQ==
stateMap: {1=111, 2=222, 3=333}

Podio API - updating contact reference

I have an an Podio app that handles customer accounts where each customer has a manager (podio contact). All of this data resides in another system, and we are in the progress off writing software to synch the two. The software is written in java and using the podio api
I'm currently able to read and set all types of fields, except for the manager field (contact).
This is what is received from the API when a customer is loaded:
But how do you go about updating the manager reference to something else?
I've tried something like:
List<Map<String, Object>> list = new ArrayList<>();
HashMap<String, Object> values = new HashMap<>();
list.add(values);
HashMap<String, Object> value = new HashMap<>();
values.put("value", value);
value.put("mail", "employee#email.com");
//also tried user_id and profile_id
and then using the
ItemAPI.updateItemFieldValues(int itemId, int fieldId,
List<Map<String, Object>> values, boolean silent, boolean hook)
to update the field. Where am I going wrong?
Setup:
Podio app with 'Contact' field, configured to 'Workspace members / share when new address added' (like this screnshot)
Working example in Ruby
item_id = <some_item_id>
field_id = <contact_field_numeric_id>
field_external_id = <contact_field_external_id>
# set to empty value by field_id
empty_value = []
Podio::ItemField.update(item_id, field_id, single_value)
# set to single value by field_id
single_value = [{'value' => {'type' => 'user', 'id' => <Podio user id>}}]
Podio::ItemField.update(item_id, field_id, single_value)
# set to multiple values by external field id
multi_values = [{'value' => {'type' => 'user', 'id' => <Podio user id>}},
{'value' => {'type' => 'user', 'id' => <another Podio user id>}}]
Podio::ItemField.update(item_id, field_external_id, multi_values)
Will set this contact field to new value single_value or to list of new values multiple_value. Also, I'd strongly recommend to think again about your data architecture. It might be much more scalable to work with Podio Contact app type and use Reference field instead of using Contact field.
Java example:
HashMap<String, Object> pair = new HashMap<>();
HashMap<String, Object> value = new HashMap<>();
List<Map<String, Object>> list = new ArrayList<>();
// set to empty value
list = new ArrayList<>();
ItemAPI.updateItemFieldValues(<itemId>, <fieldId>, list, <silent>, <hook>)
// set to real value
value = new HashMap<>();
list = new ArrayList<>();
pair.put("id", <user_id>);
pair.put("type", "user");
value.put("value", pair);
list.add(value);
ItemAPI.updateItemFieldValues(<itemId>, <fieldId>, list, <silent>, <hook>)

About inserting dynamic data into the static content at run time

I have a template like this in properties file: Dear xxxxxx, you are payment is succesfull.
After loading this template from properties file, I want to replace that "xxxxxx" with dynamic data in Java class.
Please help me on this.
I used Message.format("template",object array which is having dynamic data);
Try this way
placeholderReplacementMap is map that contain your static value and dynamic value key pair
Map<String, Object> placeholderReplacementMap = new HashMap<>();
StrSubstitutor substitutor = new StrSubstitutor(placeholderReplacementMap);
placeholderReplacementMap.put("xxxxxx", dynamicValue);
String newString = substitutor.replace("Dear xxxxxx","you are payment is succesful");

dynamodb: how to filer all items which do not have a certain attribute?

I have a table of users with a primary hash key of userId. each user may/may not have a string attribute called "environment".
I would like to get all the users which have "environment"="xyz" or which do not have the "environment" attribute.
The following code will filter those users with environment=xyz, but how do I filter those items with no environment at all? Dynamo API will not allow to filter on an empty String.
AmazonDynamoDBClient client = DbClientManager.getDynamoDbClient();
ArrayList<AttributeValue> avList = new ArrayList<AttributeValue>();
avList.add(new AttributeValue().withS("xyz"));
Condition scanFilterCondition = new Condition()
.withComparisonOperator(ComparisonOperator.EQ.toString())
.withAttributeValueList(avList);
Map<String, Condition> conditions = new HashMap<>();
conditions.put("environment", scanFilterCondition);
ScanRequest scanRequest = new ScanRequest()
.withTableName("users")
.withAttributesToGet(
"userId",
"environment");
.withScanFilter(conditions);
ScanResult result = client.scan(scanRequest);
For now I just dropped the scan filter, and I do the filtering client-side. Bit is there any way to do it server side?
Thanks,
Aliza
Hope I'm not too late.
I've found useful function which you could use in the query. I did not check with ScanRequest but with QueryRequest works as charm.
QueryRequest queryRequest = new QueryRequest()
.withTableName("YouTableName")
queryRequest.setFilterExpression(" attribute_not_exists(yourAttributeName) ")
queryRequest.setExpressionAttributeValues(expressionAttributeValues)
queryRequest.setExclusiveStartKey(ifYouHave)
queryRequest.setSelect('ALL_ATTRIBUTES')
queryRequest.setExpressionAttributeNames(youNames)
attribute_not_exists(yourAttributeName) works with ":aws-sdk:1.11.11"
also you could use attribute_exists(yourAttributeName)
You need to use the NULL ComparisonOperator.
Check out this link: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html
NOT_NULL : The attribute exists.
NULL : The attribute does not exist.
Does this work for you?
I my case also, attribute_not_exists(attribute) worked. You can refer to this question:- How to check whether an attribute is not present in dynamoDB filter expression
for more details.

Categories