Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API design for file CSV import, best practice approach?

I need to design a REST API for importing an employee CSV file with 30 columns. Number of records in the file may vary based on the size of business,could be 10, could be 5000.

Here's my approach to design

  • POST /Employees - will add one employee record (will have 30 attributes)
  • POST /Employees?bulk - will accept JSon with multiple employee records. In this case the user may add one record as by passing json object.
  • Post /Employees?file - The API will accept a CSV file (under certain size) and the parsing and processing will be done one on the server.

In case of the first two options, the user is expected to read CSV and convert to JSON before sending.

Questions

  • Is this a best practice design?
  • Should I provide javascript library for reading CSV and converting to acceptable json format? When does one provide a JavaScript library?
    • Are any examples of such APIs that I can use to model the design?
like image 643
user9445 Avatar asked Oct 28 '16 12:10

user9445


1 Answers

Because I am not familiar with javascript, so my answer will focus on question 1, about how to design an api for importing large amounts of data. There are generally two ways, synchronous and asynchronous.

Synchronous Way

In order to avoid a long wait for importing data into the database, we should limit the number of data rows per request. If the data imported by user exceeds the limitation, the frontend needs to split the data into multiple requests. For a better user experience, we can display the current import progress.

Asynchronous Way

Compared to synchronous way, it's a little more complicated to implement asynchronous.
1.We can upload csv or json file to Amazon S3, then send file address to the server via api.
2.The asynchronous worker start importing data into the database after downing file from s3. In order to avoid database blocking, we also have to import in batches. 3.In order to get import progress, the frontend polls via api or server notify the frontend after the import is complete.

Both ways have pros and cons, which way you choose depends on the trad-off between the amount of data and the complexity of the implementation.

like image 126
spike 王建 Avatar answered Sep 27 '22 23:09

spike 王建