1、新增输出到文件和直接入库

This commit is contained in:
td_zhangyu 2020-03-05 17:48:23 +08:00
parent b47195550b
commit cb271391de
2 changed files with 136 additions and 3 deletions

View File

@ -1,21 +1,25 @@
package org.spiderflow.core.executor.shape; package org.spiderflow.core.executor.shape;
import java.util.List; import java.io.IOException;
import java.util.Map; import java.util.*;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.spiderflow.ExpressionEngine; import org.spiderflow.ExpressionEngine;
import org.spiderflow.context.RunnableTreeNode; import org.spiderflow.context.RunnableTreeNode;
import org.spiderflow.context.SpiderContext; import org.spiderflow.context.SpiderContext;
import org.spiderflow.core.executor.function.FileFunctionExecutor;
import org.spiderflow.core.serializer.FastJsonSerializer; import org.spiderflow.core.serializer.FastJsonSerializer;
import org.spiderflow.core.utils.DataSourceUtils;
import org.spiderflow.executor.ShapeExecutor; import org.spiderflow.executor.ShapeExecutor;
import org.spiderflow.io.SpiderResponse; import org.spiderflow.io.SpiderResponse;
import org.spiderflow.model.SpiderNode; import org.spiderflow.model.SpiderNode;
import org.spiderflow.model.SpiderOutput; import org.spiderflow.model.SpiderOutput;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -32,6 +36,16 @@ public class OutputExecutor implements ShapeExecutor{
public static final String OUTPUT_VALUE = "output-value"; public static final String OUTPUT_VALUE = "output-value";
public static final String DATASOURCE_ID = "datasourceId";
public static final String OUTPUT_DATABASE = "output-database";
public static final String OUTPUT_CSV = "output-csv";
public static final String TABLE_NAME = "tableName";
public static final String CSV_NAME = "csvName";
private static Logger logger = LoggerFactory.getLogger(OutputExecutor.class); private static Logger logger = LoggerFactory.getLogger(OutputExecutor.class);
@Autowired @Autowired
@ -43,10 +57,16 @@ public class OutputExecutor implements ShapeExecutor{
output.setNodeName(node.getNodeName()); output.setNodeName(node.getNodeName());
output.setNodeId(node.getNodeId()); output.setNodeId(node.getNodeId());
boolean outputAll = "1".equals(node.getStringJsonValue(OUTPUT_ALL)); boolean outputAll = "1".equals(node.getStringJsonValue(OUTPUT_ALL));
boolean databaseFlag = "1".equals(node.getStringJsonValue(OUTPUT_DATABASE));
boolean csvFlag = "1".equals(node.getStringJsonValue(OUTPUT_CSV));
if (outputAll) { if (outputAll) {
outputAll(output, variables); outputAll(output, variables);
} }
List<Map<String, String>> outputs = node.getListJsonValue(OUTPUT_NAME, OUTPUT_VALUE); List<Map<String, String>> outputs = node.getListJsonValue(OUTPUT_NAME, OUTPUT_VALUE);
Map<String, Object> outputData = null;
if (databaseFlag || csvFlag) {
outputData = new HashMap<>(outputs.size());
}
for (Map<String, String> item : outputs) { for (Map<String, String> item : outputs) {
Object value = null; Object value = null;
String outputValue = item.get(OUTPUT_VALUE); String outputValue = item.get(OUTPUT_VALUE);
@ -58,6 +78,18 @@ public class OutputExecutor implements ShapeExecutor{
logger.error("输出{}出错,异常信息:{}", outputName,e); logger.error("输出{}出错,异常信息:{}", outputName,e);
} }
output.addOutput(outputName, value); output.addOutput(outputName, value);
if ((databaseFlag || csvFlag) && value != null) {
outputData.put(outputName, value.toString());
}
}
if(databaseFlag){
String dsId = node.getStringJsonValue(DATASOURCE_ID);
String tableName = node.getStringJsonValue(TABLE_NAME);
outputDB(dsId, tableName, outputData);
}
if (csvFlag) {
String csvName = node.getStringJsonValue(CSV_NAME);
outputCSV(csvName, outputData);
} }
context.addOutput(output); context.addOutput(output);
} }
@ -90,6 +122,58 @@ public class OutputExecutor implements ShapeExecutor{
} }
} }
private void outputDB(String databaseId, String tableName, Map<String, Object> data) {
if (data == null || data.isEmpty()) {
return;
}
JdbcTemplate template = new JdbcTemplate(DataSourceUtils.getDataSource(databaseId));
Set<String> keySet = data.keySet();
Object[] params = new Object[data.size()];
SQL sql = new SQL();
//设置表名
sql.INSERT_INTO(tableName);
int index = 0;
//设置字段名
for (String key : keySet) {
sql.VALUES(key, "?");
params[index] = data.get(key);
index++;
}
try {
//执行sql
int update = template.update(sql.toString(), params);
} catch (Exception e) {
logger.error("执行sql出错,异常信息:{}", e.getMessage(), e);
ExceptionUtils.wrapAndThrow(e);
}
}
private void outputCSV(String csvName, Map<String, Object> data) {
if (data == null || data.isEmpty()) {
return;
}
Set<Map.Entry<String, Object>> entries = data.entrySet();
StringBuffer sb = new StringBuffer();
int i = 1 ;
for (Map.Entry<String,Object> item : entries) {
if (item.getValue() != null) {
sb.append(item.getValue().toString());
}
if (i < data.size()) {
sb.append(",");
}
i++;
}
sb.append("\r\n");
System.out.println(sb.toString());
try {
FileFunctionExecutor.write(csvName, sb.toString().getBytes(),true);
} catch (IOException e) {
logger.error("文件输出错误,异常信息:{}", e.getMessage(), e);
ExceptionUtils.wrapAndThrow(e);
}
}
@Override @Override
public String supportShape() { public String supportShape() {
return "output"; return "output";

View File

@ -37,6 +37,39 @@
</div> </div>
</div> </div>
</div> </div>
<div class="layui-row layui-col-space20">
<div class="layui-col-md3">
<div class="layui-form-item">
<label class="layui-form-label">数据输出</label>
<div class="layui-input-block">
<input type="checkbox" target-div="databaseDiv" title="输出到数据库" value="output-database" lay-skin="primary" {{d.data.object['output-database'] == '1' ? 'checked' : ''}} lay-filter="targetCheck"/>
<input type="checkbox" class="oCheckbox" target-div="csvDiv" title="输出到CSV文件" value="output-csv" lay-skin="primary" {{d.data.object['output-csv'] == '1' ? 'checked' : ''}} lay-filter="targetCheck"/>
</div>
</div>
</div>
<div class="layui-col-md3 databaseDiv" {{d.data.object['output-database'] == '1' ? '' : 'style="display: none;"'}}>
<div class="layui-form-item">
<label class="layui-form-label">数据源</label>
<div class="layui-input-block">
<select name="datasourceId">
{{# layui.each(d.datasources,function(index,datasource){ }}
<option value="{{=datasource.id}}" {{datasource.id == d.data.object.datasourceId ? 'selected': ''}}>{{datasource.name}}</option>
{{# }) }}
</select>
</div>
</div>
</div>
<div class="layui-col-md2 databaseDiv" {{d.data.object['output-database'] == '1' ? '' : 'style="display: none;"'}}>
<div class="layui-form-item">
<input type="text" name="tableName" placeholder="请输入表名" autocomplete="off" class="layui-input input-default" value="{{=d.data.object.tableName}}">
</div>
</div>
<div class="layui-col-md2 csvDiv" {{d.data.object['output-csv'] == '1' ? '' : 'style="display: none;"'}}>
<div class="layui-form-item">
<input type="text" name="csvName" placeholder="请输入文件名" autocomplete="off" class="layui-input input-default" value="{{=d.data.object.csvName}}">
</div>
</div>
</div>
<table class="layui-table" id="spider-output" data-cell="{{=d.cell.id}}" data-keys="output-name,output-value"></table> <table class="layui-table" id="spider-output" data-cell="{{=d.cell.id}}" data-keys="output-name,output-value"></table>
<hr> <hr>
<div class="layui-form-item"> <div class="layui-form-item">
@ -52,6 +85,7 @@
function renderSpiderOutput(data){ function renderSpiderOutput(data){
layui.table.render({ layui.table.render({
elem : '#spider-output', elem : '#spider-output',
limit: 50,
cols : [[{ cols : [[{
title : '输出项', title : '输出项',
width : 150, width : 150,
@ -72,4 +106,19 @@
}) })
} }
renderSpiderOutput(getCellData({{d.cell.id}},$("#spider-output").data('keys').split(","))); renderSpiderOutput(getCellData({{d.cell.id}},$("#spider-output").data('keys').split(",")));
$.ajax({
url : 'datasource/all',
success : function(datasources){
for(var i =0;i<datasources.length;i++){
var ds = datasources[i];
$('select[name=datasourceId]').append('<option value="'+ds.id+'">'+ds.name+'</option>');
}
layui.form.render();
var selectDataSourceId = '{{=d.data.object.datasourceId}}';
if(selectDataSourceId != ''){
$('.layui-form-select dd[lay-value='+selectDataSourceId+']').trigger('click');
}
}
});
</script> </script>