I am currently working on S3 upload in Java, and I am to add a set of user metadata to the file. I am trying to understand, if I can add a list of string to one of the parameters in the user metadata.
My current code snippet is the below -
ObjectMetadata objectMetadata = new ObjectMetadata();
Map<String, Object> userMetadata = new HashMap<String, Object>();
userMetadata.put("a", "a1");
userMetadata.put("b", "b1");
List<String> c = new ArrayList<String>();
c.add("c1");
c.add("c2");
userMetadata.put("c", c);
objectMetadata.setUserMetadata(userMetadata);
However, I am getting a compile time error at the line "objectMetadata.setUserMetadata(userMetadata);"
setUserMetadata takes a Map<String, String>, not a Map<String, Object> like you have - so you can't use a list directly there. You could, for example, join it:
Map<String, String> userMetadata = new HashMap<>(); // Note the type!
userMetadata.put("a", "a1");
userMetadata.put("b", "b1");
List<String> c = new ArrayList<>();
c.add("c1");
c.add("c2");
userMetadata.put("c", String.join(",", c); // list is joined when added to the map
objectMetadata.setUserMetadata(userMetadata);
Related
I query data from Solr-8.3.1 but i got error "Sorting on a tokenized field that is not a SortableTextField is not supported in cloud mode." I don't know why.
Below is my query in web and java code.
select?fq=calling_number:8562099976999&q=*:*&wt=json&group=true&group.field=calling_number
StringBuilder queryString = new StringBuilder("calling_number:(").append(isdn).append(" OR ").append(countryCode + isdn).append(")");
StringBuilder filterString = new StringBuilder("start_time:[").append(startDate).append(" TO ").append(endDate).append("]");
final Map<String, String> queryParamMap = new HashMap<String, String>();
queryParamMap.put("q", queryString.toString());
queryParamMap.put("fq", filterString.toString());
queryParamMap.put("group","true");
queryParamMap.put("group.field", "calling_number");
MapSolrParams queryParams = new MapSolrParams(queryParamMap);
I have the below expression
Map<String, String> institutionList = new LinkedHashMap<String, String>();
institutionService.list().forEach(institution -> institutionList.put(institution.getCode(),institution.getName()));
I tried like below.
institutionService.list().stream().collect(Collectors.toMap(Institution::getCode, Institution::getName));
But still error. how to convert this into stream() & map() with lambda?
An example which is working just fine:
Map<String, String> p = new HashMap<String, String>();
List<String> values = new ArrayList<String>(Arrays.asList("2", "4", "1"));
p = values.stream().collect(Collectors.toMap(String::toString, String::toString));
System.out.println(p);
results in
{1=1, 2=2, 4=4}
If we transfer it to your problem, the code might look like this:
I have the below expression
Map<String, String> institutionList = new LinkedHashMap<String, String>();
institutionList = institutionService.list().stream().collect(Collectors.toMap(Institution::getCode, Institution::getName));
In this case, I assume that your service gives values of class Institution
I am trying to retrieve a list of objects from a Stored Procedure in Oracle SQL. May you know how can I get an Arraylist from the code below?
ArrayList<String> strings = new ArrayList<>();
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("P_ROUTES");
SqlParameterSource in = new MapSqlParameterSource()
.addValue("V_FIXED_INT", period)
.addValue("V_CARRIER", carrier)
.addValue("V_DATE_RANGE_START", dateRangeStart)
.addValue("V_DATE_RANGE_END", dateRangeEnd);
Map<String, Object> out = jdbcCall.execute(in);
ArrayList obj = (ArrayList) out.get("RET_CURSOR");
Map<String, Object> map = (Map<String, Object>) obj.get(0);
In map object I have the list of key - value pairs.
Check image below:
I am not sure that is the best solution but it works for me. I ll try to find something better. If you have any other suggestion feel free!
Map<String, Object> out = jdbcCall.execute(in);
ArrayList obj = (ArrayList) out.get("RET_CURSOR");
logger.info("Length of retrieved routes from database = " + obj.size());
for (Object o : obj) {
Map<String, Object> map = (Map<String, Object>) o;
for (Map.Entry<String, Object> entry : map.entrySet())
routes.add(entry.getValue().toString());
}
I am trying to use map with Bulk Insert Api of ElasticSearch Java Api
public void bulkInsert(List<Map<String,String>> listOfObjects ){
BulkRequestBuilder bulkRequest = client.prepareBulk();
Iterator<Map<String,String>> itr = listOfObjects.iterator();
if (itr.hasNext()){
Map<String,String> document = itr.next();
bulkRequest.add(client.prepareIndex(index, type)
.setSource(document));
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
System.out.println(bulkResponse.buildFailureMessage());
}
}
And I am calling this with
Map<String,String> jsonMap = new HashMap<String,String>();
jsonMap.put("name", fullName.toString());
jsonMap.put("file", file);
List<Map<String,String>> listOfObjects = new ArrayList<Map<String,String>>();
listOfObjects.add(jsonMap);
indexService.bulkInsert(listOfObjects);
I am getting following exception
The number of object passed must be even but was [1]
Ok I got the fix :
Use Map<String, Object> instead of Map <String,String>
Map<String,Object> jsonMap = new HashMap<String,Object>();
jsonMap.put("name", fullName.toString());
jsonMap.put("file", file);
List<Map<String,Object>> listOfObjects = new ArrayList<Map<String,Object>>();
listOfObjects.add(jsonMap);
indexService.bulkInsert(listOfObjects);
From ES java api;
Using Map
Map is a key:values pair collection. It represents a JSON structure:
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
how do i write this as a list structure in java
In this case i want the structure to be like this, Where options is also a key in another
hashmap called styles
options[{"value":"0","label":"zero"},{"value":"1","label":"one"},
{"value":"2","label":"two"}]
Here if i want to access options[1].value should give me 1 and options[2].label should give me two.
How can i achieve this with
LIst<Map<string><string[]>>?
Also Can i pass "options" array as one of the keys in my hash map
protected Map<String, String[]> getValueProperties(int view, Field field) {
Map<String, String> properties = new HashMap<String,String[]>();
properties.put("options", []);
return properties
}
I am new to handling data in this format, any pointers will be good
I think this can do:
List<Map<String,String>> options = new ArrayList<Map<String,String>>();
and populate as :
Map<String, String> option1 = new HashMap<String, String>();
option1.put("value", "0");
option1.put("level", "zero");
options.add(option1);
Map<String, String> option2 = new HashMap<String, String>();
option2.put("value", "1");
option2.put("level", "one");
options.add(option2);
Map<String, String> option3 = new HashMap<String, String>();
option3.put("value", "2");
option3.put("level", "two");
options.add(option3);
EDIT: You can populate the list in a loop as below:
List<Map<String,String>> options = new ArrayList<Map<String,String>>();
String[] levels = {"zero", "one", "two"};
for(int indx = 0; indx <levels.length; indx++){
Map<String, String> option = new HashMap<String, String>();
option.put("value", String.valueOf(indx));
option.put("level", levels[indx]);
options.add(option);
}
Use this data structure:
List< Map<String, String> >