2

I have a csv file which I want to import to table in postgres

The table contains 3 fields (id text, name text, geo geometry). The csv file is in the same format (3 values separate with comma).

I want to use java code in order to import the file (input.csv) into the table (tbl)

How can I do it ? Is there a query which I pass the file path to the DB ?

2
  • "Is there a query which I pass the file path to the DB" - will the file be accessible from the database server (by the PostgreSQL user; the user ID the server runs as); If so you could use COPY. Commented Aug 27, 2020 at 5:23
  • 1
    You could use the CopyManager API. See here or here Commented Aug 27, 2020 at 5:30

2 Answers 2

1

You can use OpenCSV to read csv file into java object (here is an example - https://www.geeksforgeeks.org/mapping-csv-to-javabeans-using-opencsv/), and then use Java JPA Repository to insert data into database (here is an example - https://www.baeldung.com/spring-data-crud-repository-save)

Sign up to request clarification or add additional context in comments.

Comments

0

You can create a simple Spring boot app to import the CSV data into PostgreSQL. There are two choices

  1. Simple Implementation: Use OpenCSV reader to read the CSV content and JPA/JDBC for storing the data in the DB. This will be fine as long as the CSV files are small.

  2. Using Spring Batch: Spring batch offers robust architecture to efficiently import content from files to DB. It comes in handy with different Reader and Writer implementations to work with different input/output sources.

It is very efficient for processing large data files and offers chunk-based processing, transaction management, etc.

I have written a detailed blog post about this here

Also, there is a video tutorial available on this topic here https://youtu.be/XBW3D1f6CAA

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.