简体   繁体   中英

Import csv in h2 dynamically

I'm searching for a solution to Import any csv file with Java (or SQL computed with a Java application at runtime) in a h2 database. a possibility could be to write at first a dynamic file and run the "script" on h2 databse connection after creation. my main Problem is that i create the csv files dynamically and the column number and Label values can differ. the Environment for getting Connection, run a scribt on h2 and create the files during application is running are still exists.

for now i found many Solutions if i know the csv structure but i didn't can know it before. another Problem is that the typ is not ever sure(if it is easier to find a solution let as say all are double.

the reason why i Need this is, that i wanna Show the dataset in a graph (linechart). sometimes i Need the first two rows as x axis. sometimes i have to Show one line sometimes more(so in csv there are one ylabel or more)

the reason why I wanna have this data in a database is i ever wanna Show min max and avg of this set based on the x axis criteria. (smt. Show per month, day, week). so the idea is to set a date Format by creating the data and by reading for the Chart i Group by DATE(DATE,TIME) , the data.

examples: my base csv example

DATE,TIME,label1  ,  y2       ,line3  ,...  (labelNames have no equality)

20160101,0115, any int,any double ,any int,...

20160101,0130, ...    , ...       , ...   ,..   (if there is no messure 

20160101,0145, ...    ,  .....    , ---   ,..    the placefolder is '---')

20160101,0200, ....

20160102,...

so sometimes my resulting csv seems like:

DATE,TIME,label1,y2

or like this:

DATETIME,label1,y2

or this:

DATE,y2,another4

We can be sure that all lines have same length. labels are at first line. I hope you get the idea. if you have a better solution to solve it i also would listen to that!(sry for the style have no idea how to make a table)

thanks for any command!

What will work for sure is use this SQL command:

CREATE TABLE TEST AS SELECT * FROM CSVREAD('test.csv'); <<from h2 docu>>

With this, all columns will be created as varchar with a matching size.

But after that, so if you want to work with this data you have to know which column you need (position or column name) and to which type you want to parse the data.

Of course it is possible to try out all possibilities and catch every exception. If nothing holds you can throw this measure away and go on, but this is not the best practice.

By default all columns data types are set varchar, if do this own make choice then you make this query.

CREATE TABLE TEST
(
  ID INT PRIMARY KEY,
  Col1 VARCHAR(55),
  Col2 INT(55)
) 
AS 
SELECT * 
FROM CSVREAD('here file path complete\test.csv');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM