I'm having trouble lately adding the json structure i want into firebase database. I want to add an extra attribute to my database like in the image
I tried orderstReference.child(pid).child("quantity").setValue(orderId);
but the value is overwriting each time its execute where i want them to add like in a list.
How can i add this ? and is there any useful link to learn these stuff i can't find what i want?
If you already have a node with data, and you want to add an extra child then using setValue() will override the whole node. In this case, you need to use updateChildren():
private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}
Check this link:
https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields
Instead of set value which will override everything try update Children.
Related
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);
I have a Query for getting lastSeenTime only for one user
but what I need is to get a map of ids by their last seen for a list of users in elastic search
can somebody help me with converting this query to find last seen of a list of users ssoIds?
static Map<String, Object> getLastSeen(String ssoId) {
SearchResponse response = transportClient.prepareSearch(ChatSettings.ELASTIC_LAST_SEEN_INDEX_NAME)
.setTypes(ChatSettings.ELASTIC_DB_NAME)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.idsQuery().addIds(ssoId))
.setFrom(0).setSize(1).setExplain(true)
.get();
checkResponse(response);
Map<String, Object> result = null;
if (response.getHits().getTotalHits() > 0) {
result = response.getHits().getAt(0).getSource();
}
return result;
}
actually I want something like this
static Map<String, Object> getLastSeens(List<String> ssoIdList)
{
//elsticQuery
}
You can use fetch in your elastic query to return only selected fields:
.setFetchSource(new String[]{"field1","field2}, null)
And for passing multiple IDs, you can pass the Array of ids to the idsQuery()
So, in your case it will become:
SearchResponse response = transportClient
//.prepareSearch(ChatSettings.ELASTIC_LAST_SEEN_INDEX_NAME) // you might need to pass the columns here
.setTypes(ChatSettings.ELASTIC_DB_NAME)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setFetchSource(new String[]{"id","lastSeenTime"}, null) // or you can pass the columns here
.setQuery(QueryBuilders.idsQuery().addIds(ssoIds)) // where ssoIds is a array of Ids
.setFrom(0).setSize(1).setExplain(true)
.get();
post this, rest of the code will work as it is.
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}
Cannot figure out for the life of me how to do this. I've tested the following which isn't working;
String stripeCustomerID = "123";
Customer cu = Customer.retrieve(stripeCustomerID);
cu.setDefaultSource(token);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("default_source", token);
enter code here`cu.update(updateParams);
This is the only place the Stripe API documentation hasn't had the answer.
Has anyone implemented this previously?
Regards,
Michael
default_source is expecting a card id not a token id. Thus you either need to:
1) Add the card to the customer and then update the default_source property
or
2) You can set the source property of the customer to the token. By setting source you will add the new card, delete the old default_source, and then set the new one as the default, all in the same API call.
Answer thanks to Matthew;
Customer cu = Customer.retrieve(stripeCustomerID);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("source", token);
cu.update(updateParams);
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");