Looking for technical architecture suggestions. I ...
# suitescript
c
Looking for technical architecture suggestions. I need to run a SQL query that will return around 36000 rows of data; I need to write the results into a single CSV file. I first thought an M/R script would work but I'd need to pass the entire result set as a single key (from the Map() function) which makes M/R pointless. Maybe I could save each row to a custom record then use another M/R to create the CSV and write as many rows until governance is exceeded then it can reschedule itself and append the subsequent records data to the CSV until all records are processed. Or maybe forget the custom record and segregate each result row from the SQL query into it's own key; the first key will create the CSV file and subsequent keys can append to the CSV. Now sure how to know when the CSV has finished being created though. Could always store the number of rows in N/cache and decrement it for each row saved maybe. All above solutions sucked - thanks for the help!
a
You absolutely need a query? Can’t be done with a search?
c
I'm trying to get the quantity available for a list of locations
But I think I need to look into the inventory detail so I'm not including inventory a store cannot sell.
a
If you are able to create a search that gives you you the data you need, then you can use the task module to do a searchTask and export that to CSV, everything is native and asynchronous, meaning it does not matter how many results and you don’t need to worry about handling CSV creation or parsing or anything like that… Basically the same as exporting to CSV via the UI click but only better because it is a background asynchronous process.
Tasks created this way also allows you to chain a second task… For example I use this to generate large CSV files and after it is done it trigger another script to upload the file to a sFTP server.
c
I'm familar with task.create and passing in the fieldId
That would be the best easiest solution - I just don't think I can use a search here.
a
This is not exactly task.create(), give it a try, you can do a lot with searches…
c
I've got a functional consultant telling me there's no way to do this with one search,
I'll spend a couple hours trying before looking at the SuiteQL option again.
a
In my experience if you don’t need 3 levels joins it most likely can be done with a search… But I’m not familiar enough with your case and I’m not fighting your consultant lol 😂 . That would be the cleanest possible design to handle a large dataset… Going back to use a query, you don’t need to pass the entire data set as a key… In you map stage you can use a fixed key and pass each map result into values, then group them in reduce and create your CSV file. That dataset is not large enough to break that…
The
key
is not going to contain the entire resultset, the key and the values are passed to reduce as many times as you get them in the map stage. Then you group then by that key in the reduce stage.
The resultset does not transition from map to reduce at once but instead one result at a time and then you group them by the key in the reduce stage.
c
I thought reduce could fully execute before receiving the next key from the map?
a
No, the reduce stage purpose is to aggregate data it does not run until map is completed.
Technically speaking reduce can start before map finish but not before the shuffling/sorting stage runs(intermediate stage we don’t see)…
c
If reduce aggregates all data with the same key, regardless of when it's received, this becomes easy.
a
Correct, the key you write in map to reduce could be ‘CSV_DATA’ and your value is each organized data row. You group in reduce, build your CSV header from the first grouped result and add your data to the CSV. My recommendation here is to use papaparse, that way you don’t need to worry about any parsing it can create a valid CSV content with one line of code.
a
you can use the task module with SQL?
a
@Craig If you can use that which is basically the same as searchTask, you should go with that, what @Anthony OConnor mentioned ^^^…
a
yeah you guys are massively overcomplicating this 😛
c
Well that makes it even easier!
Now I have zero motivation to try and get this into a search 🙂
🙌 1
a
I did not know the query module had a similar method, maybe new addition?
a
yeah idk they seem to quietly add sql support in a lot of places so always worth double checking
a
No very new, it looks like it was added in 2021
c
Everyday's a school day Thanks for the help
👍 1
😂 1
j
wait. There’s a built in “run sql and export it to csv” thing? Why did I write my own, lol.
😂 2
c
I was on the same path!