superset icon indicating copy to clipboard operation
superset copied to clipboard

Allow users to import CSV as datasource

Open gbrian opened this issue 10 years ago • 25 comments

Hi there, Is there any plan to add support for uploading CSV data as well as data source ?

Maybe using sqlite3: http://stackoverflow.com/questions/2580497/database-on-the-fly-with-scripting-languages

Thanks

gbrian avatar Apr 20 '16 08:04 gbrian

It should probably be done at the database level, maybe an upload icon in the database list view.

pandas has some utility functions that make that trivial, first load the csv in a dataframe, then upload it to the db.

mistercrunch avatar Apr 20 '16 20:04 mistercrunch

+1, would love this feature.

Miserlou avatar Apr 23 '16 17:04 Miserlou

This would be a very handy feature for a data mining system.

But would be curious how could the upload CSV file / data be saved,

Will the data always be read the CSV file and parsed / during dashboard / graph generation?

Or user has to select an existing database to save data on the CSV file?

This would require the database user to have insert or even create table permission on the data source, which is not necessary on current design.

Any thoughts here?

xqliu avatar Apr 25 '16 03:04 xqliu

I'd be willing to have a go at this. Something like:

  • Drag and drop a csv file and/or upload button on the page listing SQLA tables
  • Use pandas to parse file
  • Use pandas to write a single table sqlite database (might need an additional option in the config, USER_UPLOADED_DB_DIR or something)
  • Add metadata to caravel's db
  • Use table as any other

Additional bonus is this could make replicating/debugging others' problems easier.

@mistercrunch any thoughts?

andrewhn avatar Apr 25 '16 14:04 andrewhn

For all those wanting to use CSV in the interim, I had success using a csv2sqlite script, as detailed here: https://github.com/FOIA-data-hackathon/MuckRock-Caravel

Miserlou avatar Apr 25 '16 15:04 Miserlou

@mistercrunch, any updates on this issue? There is a workaround via miserlou but I was hoping to make a contribution.

@andrewhn, did you make any progress with this? If so, could I see your code?

SalehHindi avatar Jul 05 '16 03:07 SalehHindi

@andrewhn @SalehHindi Has anyone given this a go yet? We also think this would be a great feature, but would be keen to hear of any new approaches that did/didn't work.

samempson avatar Oct 11 '16 15:10 samempson

For the record, I would suggest to anyone who wants to tackle this to the following pandas method, and expose as much as is possible/reasonable from their api in the upload form: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html

Basically you'd have a form with sensible defaults and options based on the pandas api.

mistercrunch avatar Nov 22 '16 01:11 mistercrunch

@mistercrunch, thanks for laying that out. I think I'll make an attempt.

SalehHindi avatar Dec 08 '16 07:12 SalehHindi

@mistercrunch @SalehHindi

I've made a start on this on a csv-import branch on my fork. The basic functionality is in place but needs some testing and additional validation on the fields presented by the new form.

A button to import CSV has been added on the 'sources->database' page that brings up a new form exposing most of the pandas api. The CSV is added as a new table to an existing database. It can then be added like any other table on the 'sources->tables' page.

Ryan4815 avatar Dec 12 '16 10:12 Ryan4815

@axitkhurana I was swamped with finals week last week so I didn't get to it so go for it.

SalehHindi avatar Dec 22 '16 07:12 SalehHindi

Nice one @Ryan4815, is there any update on your solution?

simeonbabatunde avatar Jan 18 '17 13:01 simeonbabatunde

@axitkhurana @Simeon-ayo I believe that @SalehHindi is going to add some tests to the branch and get it prepped for a merge request.

Ryan4815 avatar Jan 30 '17 09:01 Ryan4815

Pandas is quite memory hungry. I can't load a sparse 1GB csv file on my 16GB system due to MemoryError.

Plot.ly offers a tutorial on how to convert a CSV to SQLite chunk by chunk to avoid eating all the memory. https://plot.ly/python/big-data-analytics-with-pandas-and-sqlite/.

It's probably useful for superset to use a similar conversion step so an arbitrarily sized csv can be converted.

If speed is an issue as pandas.read_csv is single-threaded, an alternative is paratext https://github.com/wiseio/paratext. the load_csv_to_pandas function is using all cores and is much faster than pandas. It doesn't solve the whole memory issue though: while it's quite efficient while reading and processing the CSV, the last conversion to pandas dataframe will use as much memory as pandas alone.

mratsim avatar Feb 27 '17 17:02 mratsim

Nice catch @mratsim and thanks for the link. I'm currently tidying up my code and preparing to do a pull request for this issue. @mistercrunch, do you think it's ok if I go ahead and do the pull request for the current issue and include @mratsim's suggestion in another pull request?

SalehHindi avatar Mar 08 '17 02:03 SalehHindi

http://stackoverflow.com/questions/25962114/how-to-read-a-6-gb-csv-file-with-pandas

mistercrunch avatar Mar 08 '17 04:03 mistercrunch

Any updates on this feature

eyadsibai avatar Apr 10 '17 22:04 eyadsibai

any updates of this feature

vinpatel avatar Apr 22 '17 20:04 vinpatel

import pandas as pd pdsites = pd.read_csv("site_data.csv") pdsites.columns

def df2sqlite(dataframe, db_name = "import.sqlite", tbl_name = "import"):

import sqlite3
conn=sqlite3.connect(db_name)
cur = conn.cursor()

wildcards = ','.join(['?'] * len(dataframe.columns))
data = [tuple(x) for x in dataframe.values]

cur.execute("drop table if exists %s" % tbl_name)

col_str = '"' + '","'.join(dataframe.columns) + '"'
cur.execute("create table %s (%s)" % (tbl_name, col_str))

cur.executemany("insert into %s values(%s)" % (tbl_name, wildcards), data)

conn.commit()
conn.close()

df2sqlite(pdsites, db_name="sites_4g.db", tbl_name = "sites_data_4g")

import sqlite3 import pandas as pd

Create your connection.

cnx = sqlite3.connect('sites_4g.db')

df = pd.read_sql_query("SELECT * FROM sites_data_4g", cnx) df.head(5)

####Then go to superset data sources-databases and input 'sqlite///sites_4g.db'

hillaryhitch avatar Aug 13 '17 03:08 hillaryhitch

Update: I'm now working on this issue, continuing from @SalehHindi 's last commit to the csv-import branch. When I run the code, I don't see any "Add CSV Table to Database" button. Can you tell what I might be doing wrong? You can look at the code I'm running on my fork (https://github.com/timifasubaa/incubator-superset) with branch name import_csv. Also, please post a snapshot of the new flow (e.g. the page with the new button e.t.c.) .

timifasubaa avatar Aug 24 '17 00:08 timifasubaa