DEV Community

Evgeny Shvarov for InterSystems

Posted on

Importing CSV data into InterSystems IRIS programmatically

Sometimes we need to import CSV data to InterSystems IRIS either from CSV or from URL. And we expect the class with proper datatypes to be created and the data to be imported. You can do it manually via the IRIS management portal, but often we need to do this programmatically.

I published a module csvgen on InterSystems Open Exchange which does exactly that.

If you just need the CSV file to be imported into IRIS you can do the following:

USER>do ##class(community.csvgen).Generate("/usr/data/titanic.csv",,"Data.Titanic")

Class name: Data.Titanic
Header: PassengerId INTEGER,Survived INTEGER,Pclass INTEGER,Name VARCHAR(250),Sex VARCHAR(250),Age INTEGER,SibSp INTEGER,Parch INTEGER,Ticket VARCHAR(250),Fare MONEY,Cabin VARCHAR(250),Embarked VARCHAR(250)
Records imported: 891
USER>
Enter fullscreen mode Exit fullscreen mode

Or if you have the CSV on the internet, e.g. COVID-19 Data on Github you can get the data in the following way:

USER>d ##class(community.csvgen).GenerateFromURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/05-29-2020.csv",",","Data.Covid19")

Class name: Data.Covid19
Header: FIPS INTEGER,Admin2 VARCHAR(250),Province_State VARCHAR(250),Country_Region VARCHAR(250),Last_Update DATE,Lat MONEY,Long_ DOUBLE,Confirmed INTEGER,Deaths INTEGER,Recovered INTEGER,Active INTEGER,Combined_Key VARCHAR(250),Incidence_Rate DOUBLE,Case-Fatality_Ratio DOUBLE
Records imported: 3522
USER>
Enter fullscreen mode Exit fullscreen mode

Installation

You can install the package with ObjectScript Package Manager ZPM:

USER>zpm

zpm:USER>install csvgen
Enter fullscreen mode Exit fullscreen mode

The csvgen module is a wrapper of CSV2CLASS method .

The ObjectScript Quality profile.

It's not ideal, so collaboration is very welcome!

Top comments (0)