骆驼 Csv 读取存储数据到 sql
Camel Csv read store data to sql
我需要读取 CSV 文件,而且我需要将所有数据存储在一个文件中 table。
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();
}
你可以看到我使用的代码
+ " <transform> "
+ " <simple>${body[0]}</simple> "
+ " </transform> "
执行此代码后,它将仅存储 csv 中的 0 个索引行。我需要将所有原始数据从 csv 存储到 sql table.
如果有人知道,请提供相同的解决方案。
您需要添加一个额外的斜杠以正确转义代码的标记化部分中的换行符,这是我看到的唯一问题
我需要读取 CSV 文件,而且我需要将所有数据存储在一个文件中 table。
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();
}
你可以看到我使用的代码
+ " <transform> "
+ " <simple>${body[0]}</simple> "
+ " </transform> "
执行此代码后,它将仅存储 csv 中的 0 个索引行。我需要将所有原始数据从 csv 存储到 sql table.
如果有人知道,请提供相同的解决方案。
您需要添加一个额外的斜杠以正确转义代码的标记化部分中的换行符,这是我看到的唯一问题