I have a DynamoDB table having field X as a GSI hash key, I what query this table to fetch all documents where the field X has value contained in the list xList = ["a", "b", "c"]. what is the best way to query this using DynamoDBMapper?
Right now I use the following code for this senario,
List<TableDoc> tableDocs = xList.stream().map( x -> new TableDocument().setX(x)).collect(Collectors.toList());
List<String, List<Object>> result = dynamoDBMapper.batchLoad(tableDocs);
I have documents in the table corresponding to the x values in xList, But I get null when I do
result.get(TABLE_NAME);
Is this a right way of batchloading items using GSIs?
Thanks for reading :)
Related
I have a query:
Query q = em.createNativeQuery("select DISTINCT id, rating, random() as ordering FROM table\n" +
" WHERE id not in (1,2) ORDER BY ordering LIMIT 10");
List data = q.getResultList();
Every element of this list is array like object:
I want to retrieve that "8" and "16" and compose a comma separated string (to use it in my query in "not in" section in future):
for (Object x : data) {
System.out.println(Arrays.asList(x).get(0));
}
But it produces strings:
[Ljava.lang.Object;#ee93cd3
[Ljava.lang.Object;#62f3c3e1
I don't know how to get that IDs ("8" and "16")
1.I think this is what you are looking for...
Convert JPA query.getResultList() to MY Objects.
or
List<Object[]> rows = q.getResultList(); for (Object[] row : rows) { System.out.println(row[0]); }
in this line
List<Object[]> data = q.getResultList();
data is list of Object of form
[ [1,233, 0.000333], [1,233, 0.000333] ]
for (Object[] x : data) {
// x is [1,233, 0.000333]
System.out.println(x[0]);
}
If I understood it correctly, you are looking for comma separated string of ID's.
If so, then follow these steps might help you to solve the issue.
Create a constructor in table which has only one parameter ID. (If you want you can add more parameters as well but make sure the value which you want it must be in constructor as well as in query.)
Write sql query and execute it.
It returns result and gather it in List which contains the object of the table.
Get the string
dataList.stream().map(obj -> obj.getId()).collect(Collectors.joining(", "))
This will give you the comma separated string.
I am trying to load use this query and the values into a map
LinkedHashMap<SCRegionPriority, String> hash = new LinkedHashMap<>();
pstmt=("SELECT a.Id,a.RegionName,ISNULL(b.Priority,0) as priority
FROM dbo.region a left join dbo.SCMap b on a.Id = b.RegionId
and a.CountryId = b.CountryId and b.SCId=1 where a.CountryId ='1'")
ResultSet rs = pstmt.executeQuery();
con.commit();
while (rs.next()) {
SCPri c = new SCPri();
c.setRegionid(rs.getInt(1));
c.setRegionname(rs.getString(2));
c.setPriority(rs.getInt(3));
hash.put(c, String.valueOf(rs.getInt(3)));
}
the query gives me the following values:
1,0|2,1|3,1|4,2|...
And then I am trying to load this into the vaadin grid like the following:
List<HashMap<SCPri, String>> rows = new ArrayList<>();
LinkedHashMap<SCPri, String> fakeBean1 = subdao1.getSCMap(subcontractor.getId(),subcontractor.getCountryId());
rows.add(fakeBean1);
Grid<HashMap<SCPri, String>> grid2 = new Grid<>();
grid2.setItems(rows);
for (Map.Entry<SCPri, String> entry : s.entrySet()) {
grid2.addColumn(h -> h.get(entry.getKey().getRegionname())).setCaption(entry.getKey().getRegionname());
}
addComponents(grid2);
I am not able to load the grid with the columns dynamically generated and one editable row of with values underneath those columns.
I am trying to make the grid look like the following:
r1|r2|r3|r4
0 |1 |1 |2
I tried to follow the following two links but failed to get it working:
How to fill Vaadin Grid with Map items type
https://vaadin.com/forum/thread/16038356/grid-8-without-bean-class
How can I achieve this?
A single row with empty cells implies that the value provider callback for each cell returns null (or "").
In your case, the value provider callback is h -> h.get(entry.getKey().getRegionname()). Here, h represents the row object, i.e. your single HashMap<SCPri, String> instance. I'm assuming getRegionname() returns something like a String, which would surely cause null results from h.get() since h uses SCPri instances as they key. Changing the callback to h -> h.get(entry.getKey()) instead might do the trick.
It is an unfortunate leftover from the pre-generics times that Map::get accepts any Object without compilation errors instead of requiring you to pass an instance of the actual key type of the map.
I have two tables A and B. Both of them have the fields session_id and cookie_id.
How do i create a Joined table output joining A with B on session_id, cookie_id with the help of a Dataflow pipeline? CoGroupByKey method allows you to join on a single key. Couldn't find anything helpful in the documentation as well.
To expand on user9720010's answer. You can create a composite key by mapping the fields to a combination of session_id and cookie_id. This pattern is explained in the common Dataflow use-case patterns blog. Assuming you are using BigQuery, you can do something similar to the following:
Pipeline pipeline = Pipeline.create(options);
// Create tuple tags for the value types in each collection.
final TupleTag<TableRow> table1Tag = new TupleTag<>();
final TupleTag<TableRow> table2Tag = new TupleTag<>();
// Transform for keying table rows by session_id and cookie_id
WithKeys<String, TableRow> sessionAndCookieKeys = WithKeys.of(
(TableRow row) ->
String.format("%s#%s",
row.get("session_id"),
row.get("cookie_id")))
.withKeyType(TypeDescriptors.strings());
/*
* Steps:
* 1) Read table 1's rows
* 2) Read table 2's rows
* 3) Map each row to a composite key
* 4) Join on the composite key
* 5) Process the results
*/
PCollection<KV<String, TableRow>> table1Rows = pipeline
.apply(
"ReadTable1",
BigQueryIO
.readTableRows()
.from(options.getTable1()))
.apply("WithKeys", sessionAndCookieKeys);
PCollection<KV<String, TableRow>> table2Rows = pipeline
.apply(
"ReadTable2",
BigQueryIO
.readTableRows()
.from(options.getTable2()))
.apply("WithKeys", sessionAndCookieKeys);
//Merge collection values into a CoGbkResult collection
PCollection<KV<String, CoGbkResult>> coGbkResult = KeyedPCollectionTuple
.of(table1Tag, table1Rows)
.and(table2Tag, table2Rows)
.apply("JoinOnSessionAndCookie", CoGroupByKey.create());
// Process the results
coGbkResult.apply(
"ProcessResults",
ParDo.of(new DoFn<KV<String, CoGbkResult>, Object>() {
#ProcessElement
public void processElement(ProcessContext context) {
// Do something here
}
}));
One approach that I follow in such situations is to create an ad hoc key which is the combination of two keys .
Post reading data , while converting to key value pair , I would output session_id$cookie_id as a single concatenated string . Here $ can be any delimiter which does not forms the charset of two keys . Delimiter can be ignored also .
How to compare list of records against database? I have more than 1000 records in list and need to validate against database. How to validate each record from list to database? Select all the data from database and stored in list, then have to compare the values? Please advise...
The below code lists values to validate against database.
private void validatepart(HttpServletRequest req, Vector<String> errors) {
Parts Bean = (Parts)req.getAttribute("partslist");
Vector<PartInfo> List = Bean.getPartList();
int sz = partList.size();
for (int i = 0; i < sz; i++) {
PartInfo part = (PartInfo)partList.elementAt(i);
System.out.println(part.getNumber());
System.out.println(part.getName());
}
}
This depends on what you mean by compare. If it's just one field then executing a query such as select * from parts_table where part_number = ?. It's not that much of a stretch to add more fields to that query. If nothing is returned you know it doesn't exist.
If you need to compare and know exactly which values are different then you can try something like this
List<String> compareObjects(PartInfo filePart, PartInfo dbPart) {
List<String> different = new LinkedList<String>();
if (!filePart.getNumber().equals(dbPart.getNumber())) {
different.add("number");
}
//repeat for all your fields
return different;
}
If your list of objects that you need to validate against the database includes a primary key, then you could just build a list of those primary key values and run a query like:
SELECT <PRIMARY KEY FIELD> FROM <TABLE> WHERE <PRIMARY_KEY_FIELD> IN <LIST OF PRIMARY KEYS> SORT BY <PRIMARY KEY FIELD> ASC;
Once you get that list back, you can compare the results. My instinct would be to put your data (and the query results too) into a Set object and then call removesAll() to get the items not in the database (reverse this for items in the database but not in your set):
yourDataSet.removeAll(queryResults);
This assumes that you have an equals() method implemented in your PartInfo object. You can see the Java API documentation for more details.
I have requirement to remove the duplicate values from result set based on some unique identifier.
I need to remove the duplicates from the result set.
while(resultSet.next())
{
int seqNo = resultSet.getInt("SEQUENCE_NO");
String tableName = resultSet.getString("TABLE_NAME");
String columnName = resultSet.getString("COLUMN_NAME");
String filter = resultSet.getString("FILTER");
}
from the above iteration, i m getting 2 rows from result set. There is same seq no,same table name, different columnname, same filter.
1 PRODUCTFEES CHARGETYPE PRODUCTID
1 PRODUCTFEES PRODUCTCODE PRODUCTID
My requirement is to remove the duplicate table name, duplicate seq no, duplicate filter.
I want to get output something below,
1 PRODUCTFEES CHARGETYPE PRODUCTCODE PRODUCTID
By the example you provide, it seems like you want to output all distinct values for each column indidivually (there are 4 columns in the table, but you output 5 values).
Being the question tagged java, an approach you could take would be using an implementation of Set for each of the columns, so that duplicates won't get through. Then output all the elements of each Set.
LinkedHashSet[] sets = new LinkedHashSet[]{
new LinkedHashSet(),
new LinkedHashSet(),
new LinkedHashSet(),
new LinkedHashSet() };
while(resultSet.next()) {
sets[0].add(resultSet.getInt("SEQUENCE_NO"));
sets[1].add(resultSet.getString("TABLE_NAME")););
sets[2].add(resultSet.getString("COLUMN_NAME"));
sets[3].add(resultSet.getString("FILTER"));
}
StringBuilder buf = new StringBuilder();
for (LinkedHashSet set : sets) {
// append to buf all elements of each set
}
But it might be simpler to address this from the very same SQL query and just make SELECT DISTINCT columnX for each of the columns and output the result without further manipulation. Or use an aggregation function that will concatenate all distinct values. The implementation will be highly dependent on the DBMS you're using (GROUP_CONCAT for MySQL, LISTAGG for Oracle, ...). This would be a similar question for Oracle: How to use Oracle's LISTAGG function with a unique filter?
Based on the different outputs I'd say, that you not just need to remove duplicates, but also reorder the data from the duplicates.
In that case you need to fill a new data-array (or similar structure) in the while(resultSet.next()), and after that loop over the newly arranged data-object and output accordingly.
In Meta-Lang this would be as follows:
while resultset.next()
if newdata-array has unique key
add column-name to found entry in newdata-array
else
create new entry in newdata-array with column-name
while newdata-array.next()
output seq, table-name
while entry.column-names.next()
output column-name
output product-id