我需要读取CSV文件,而不是需要将所有数据存储在一个表中。
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
DECLARE @start AS DateTime
DECLARE @end AS DateTime
DECLARE @actual AS DateTime
DECLARE @first AS DateTime
DECLARE @bus AS VARCHAR(50)
SET @start = '2015-07-01';
SET @end = '2016-06-01';
SET @actual = '2015-09-01';
SET @first = '2015-10-01';
SET @bus = 'EUR';
--Get distinct values of the PIVOT Column
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME(date1)
FROM (
SELECT m.date1, m.date2 FROM(
SELECT DISTINCT CONVERT(nvarchar(50), DATENAME(m, date)
+ ', '
+ DATENAME(yyyy,date)) as date1, date as date2
FROM bus_actual where date between @start and @actual
union all
SELECT DISTINCT
CONVERT(nvarchar(50), DATENAME(m, date)
+ ', '
+ DATENAME(yyyy,date)) as date1, date as date3
FROM bus_forecast where date between @first and @end
)m
)tab order by tab.date2
SET @DynamicPivotQuery =
'select * from (
select ct.category as [name],
CONVERT(nvarchar(50), DATENAME(m, bu.date)
+ '', ''
+ DATENAME(yyyy,bu.date))as date
,ISNULL(sum(bu.value),0) as value from bus bu
join bus_category buc on bu.id = buc.bus_id
join category ct on ct.id = buc.id
join bus_actual b on buc.id = b.bus_id
where b.date between '''+ cast (@start as VARCHAR(50))+''' and '''+ cast (@actual as VARCHAR(50))+'''
and bu.name = '''+ cast (@bus as VARCHAR(50))+'''
group by bu.date, ct.category
) as t
PIVOT( SUM(t.value)
FOR date IN (' + @ColumnName + ')) AS PVTTable'
EXEC sp_executesql @DynamicPivotQuery,
N'@start datetime, @actual_date datetime, @bus VARCHAR(50)',
@startFY = @start, @actual = @actual, @bus = @business_unit
您可以看到我使用过的代码
public static DataSource setupDataSource(String connectURI) {
String url = "jdbc:mysql://127.0.0.1:3306/ams";
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUsername("root");
ds.setPassword("root");
ds.setUrl(connectURI);
return ds;
}
public static void main(String[] args) throws Exception {
String url = "jdbc:mysql://127.0.0.1:3306/ams";
DataSource dataSource = setupDataSource(url);
SimpleRegistry reg = new SimpleRegistry();
reg.put("dataSource", dataSource);
CamelContext context = new DefaultCamelContext(reg);
String myString = " <route id=\"UserCSVToMYSQL\" xmlns=\"http://camel.apache.org/schema/spring\">"
+ " <from uri=\"file:/home/viral/Projects/camel/cxfJavaTest/src/csvfiles?noop=true\" />"
+ " <log loggingLevel=\"DEBUG\" message=\"${body}\"/>"
+ " <split streaming=\"true\">"
+ " <tokenize token=\"\n\" />"
+ " <unmarshal>"
+ " <csv/>"
+ " </unmarshal>"
+ " <transform> "
+ " <simple>${body[0]}</simple> "
+ " </transform> "
+ " <log loggingLevel=\"DEBUG\" message=\"${body}\"/>"
+ " <to uri=\"sql:INSERT INTO user(`external_user_id`,`first_name`,`last_name`,`email`,`active`) VALUES (#,#,#,#,#)?dataSourceRef=dataSource\"/> "
+ " </split> " + " </route> ";
InputStream is = new ByteArrayInputStream(myString.getBytes());
RoutesDefinition routes = context.loadRoutesDefinition(is);
context.addRouteDefinitions(routes.getRoutes());
context.setTracing(true);
context.start();
Thread.sleep(16000);
System.out.println("Done");
context.stop();
}
执行此代码后,它将仅存储来自csv的0索引行。我需要将所有原始数据从csv存储到sql表。
如果有人知道,请为我提供相同的解决方案。
答案 0 :(得分:1)
你需要添加一个额外的斜杠来正确地转义代码的tokenize部分中的新行字符,这是我看到的唯一问题