Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gic-cloud
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
data-hook
gic-cloud
Commits
96a65cb9
Commit
96a65cb9
authored
Sep 17, 2020
by
陶光胜
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'developer' into 'master'
取数平台下载优化 See merge request
!11
parents
6a45c0b0
7bba938c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
310 additions
and
300 deletions
+310
-300
FlatQueryResultServiceImpl.java
...ud/data/hook/service/impl/FlatQueryResultServiceImpl.java
+163
-158
FreeQueryServiceImpl.java
...ic/cloud/data/hook/service/impl/FreeQueryServiceImpl.java
+147
-142
No files found.
gic-cloud-data-hook-service/src/main/java/com/gic/cloud/data/hook/service/impl/FlatQueryResultServiceImpl.java
View file @
96a65cb9
...
...
@@ -508,174 +508,179 @@ public class FlatQueryResultServiceImpl implements IFlatQueryResultService {
this
.
downloadTaskTimer
.
schedule
(
new
TimerTask
()
{
@Override
public
void
run
()
{
if
(
taskConditions
!=
null
&&
taskConditions
.
size
()
>
0
)
{
FlatQueryTaskCondition
condition
=
null
;
//FlatQueryTaskCondition condition = taskConditions.remove(0); // 移除并获取第一个任务条件
for
(
int
i
=
0
;
i
<
taskConditions
.
size
();
i
++
)
{
log
.
debug
(
"自助指标当前正在执行的任务为:"
,
JSON
.
toJSONString
(
taskConditions
.
get
(
i
)));
if
(
taskConditions
.
get
(
i
).
getBuildPermitted
().
equals
(
Global
.
YES
))
{
condition
=
taskConditions
.
remove
(
i
);
// 移除并获取第一个任务条件
break
;
}
// IF OVER
}
// FOR OVER
if
(
condition
!=
null
)
{
// 更新任务状态
DownloadTask
task
=
DownloadTaskServiceImpl
.
getInstance
().
getDownloadTaskById
(
condition
.
getTaskId
());
task
.
setStatus
(
DownloadTaskStatus
.
BUILDING
);
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
log
.
debug
(
"runDownloadTask.run"
,
"自助指标下载任务执行:"
+
task
.
getId
());
String
fullQuery
=
buildFlatQuerySQL
(
false
,
// 下载用途
condition
.
getTableId
(),
condition
.
getEnterpriseIds
(),
condition
.
getConditions
(),
condition
.
getOrderField
(),
condition
.
getOrderDir
(),
condition
.
getExecDistinct
(),
0
,
condition
.
getAuthStoreIdList
());
Connection
conn
=
HiveHelper
.
getDownloadHiveConnection
();
if
(
conn
!=
null
)
{
try
{
Statement
stat
=
conn
.
createStatement
();
try
{
if
(
taskConditions
!=
null
&&
taskConditions
.
size
()
>
0
)
{
FlatQueryTaskCondition
condition
=
null
;
//FlatQueryTaskCondition condition = taskConditions.remove(0); // 移除并获取第一个任务条件
for
(
int
i
=
0
;
i
<
taskConditions
.
size
();
i
++
)
{
log
.
debug
(
"自助指标当前正在执行的任务为:"
,
JSON
.
toJSONString
(
taskConditions
.
get
(
i
)));
if
(
taskConditions
.
get
(
i
).
getBuildPermitted
().
equals
(
Global
.
YES
))
{
condition
=
taskConditions
.
remove
(
i
);
// 移除并获取第一个任务条件
break
;
}
// IF OVER
}
// FOR OVER
if
(
condition
!=
null
)
{
// 更新任务状态
DownloadTask
task
=
DownloadTaskServiceImpl
.
getInstance
().
getDownloadTaskById
(
condition
.
getTaskId
());
task
.
setStatus
(
DownloadTaskStatus
.
BUILDING
);
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
log
.
debug
(
"runDownloadTask.run"
,
"自助指标下载任务执行:"
+
task
.
getId
());
String
fullQuery
=
buildFlatQuerySQL
(
false
,
// 下载用途
condition
.
getTableId
(),
condition
.
getEnterpriseIds
(),
condition
.
getConditions
(),
condition
.
getOrderField
(),
condition
.
getOrderDir
(),
condition
.
getExecDistinct
(),
0
,
condition
.
getAuthStoreIdList
());
Connection
conn
=
HiveHelper
.
getDownloadHiveConnection
();
if
(
conn
!=
null
)
{
try
{
Statement
stat
=
conn
.
createStatement
();
// stat.setQueryTimeout(60 * 1000);
stat
.
execute
(
"REFRESH TABLE "
+
condition
.
getTableId
());
// 强制刷新表结构
ResultSet
rs
=
stat
.
executeQuery
(
fullQuery
);
// 生成指定格式下载元文件
String
originalFilePath
=
""
;
if
(
task
.
getFormat
().
equals
(
DownloadFileFormat
.
CSV
))
{
// 如果指定为 CSV 格式
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自助指标下载文件 "
+
condition
.
getTaskId
()
+
".csv"
);
originalFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".csv"
;
File
tmp
=
new
File
(
originalFilePath
);
if
(
tmp
.
exists
())
{
// 删除可能存在的文件
tmp
.
delete
();
}
//CSVWriter csvWriter = new CSVWriter(new FileWriter(csvPath), '\t');
OutputStreamWriter
out
=
new
OutputStreamWriter
(
new
FileOutputStream
(
originalFilePath
),
Charset
.
forName
(
"GBK"
));
ResultSetHelper
helper
=
new
CsvResultSetHelper
(
task
.
getQueryDataType
()
==
QueryDataType
.
FULL
?
CsvDataFilterMode
.
DECRYPT
:
CsvDataFilterMode
.
DESENSI
,
condition
.
getDecryptFilters
());
CSVWriter
writer
=
new
CSVWriter
(
out
,
','
);
writer
.
setResultService
(
helper
);
writer
.
writeAll
(
rs
,
true
);
writer
.
close
();
out
.
close
();
//记得关闭资源
log
.
debug
(
"runDownloadTask.run"
,
"已生成自助指标下载文件 "
+
condition
.
getTaskId
()
+
".csv"
);
}
else
{
// 如果指定为 XLS 格式
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自助指标下载文件 "
+
condition
.
getTaskId
()
+
".xlsx"
);
originalFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".xlsx"
;
SXSSFWorkbook
wb
=
new
SXSSFWorkbook
(
100
);
// 内存中保留 100 行
Sheet
sheet
=
wb
.
createSheet
();
Row
row
=
sheet
.
createRow
(
0
);
Cell
cell
;
for
(
int
j
=
0
;
j
<
rs
.
getMetaData
().
getColumnCount
();
++
j
)
{
// 遍历创建表头
String
colName
=
rs
.
getMetaData
().
getColumnLabel
(
j
+
1
);
cell
=
row
.
createCell
(
j
);
cell
.
setCellValue
(
colName
);
}
// 遍历输出行
int
rowCount
=
0
;
while
(
rs
.
next
())
{
rowCount
++;
row
=
sheet
.
createRow
(
rowCount
);
for
(
int
j
=
0
;
j
<
rs
.
getMetaData
().
getColumnCount
();
++
j
)
{
//String c = rs.getString(j + 1);
//row.createCell(j).setCellValue(c);
String
cName
=
rs
.
getMetaData
().
getColumnName
(
j
+
1
);
List
<
String
>
cFilters
=
condition
.
getDecryptFilters
();
if
(
task
.
getQueryDataType
()
==
QueryDataType
.
FULL
&&
cFilters
.
contains
(
cName
))
{
String
tmpResult
=
rs
.
getString
(
j
+
1
);
if
(
StringUtils
.
isNotBlank
(
tmpResult
))
tmpResult
=
DecryptUtils
.
getInstance
().
decrypt
(
tmpResult
);
row
.
createCell
(
j
).
setCellValue
(
tmpResult
);
}
else
{
int
cType
=
rs
.
getMetaData
().
getColumnType
(
j
+
1
);
switch
(
cType
)
{
case
Types
.
TIMESTAMP
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getTimestamp
(
j
+
1
)
!=
null
?
datetimeFormatter
.
format
(
rs
.
getTimestamp
(
j
+
1
))
:
""
);
break
;
case
Types
.
DATE
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getDate
(
j
+
1
)
!=
null
?
dateFormatter
.
format
(
rs
.
getDate
(
j
+
1
))
:
""
);
break
;
case
Types
.
TIME
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getTimestamp
(
j
+
1
)
!=
null
?
timeFormatter
.
format
(
rs
.
getTimestamp
(
j
+
1
))
:
""
);
break
;
default
:
if
(
cFilters
.
contains
(
cName
)){
row
.
createCell
(
j
).
setCellValue
(
"******"
);
}
else
{
row
.
createCell
(
j
).
setCellValue
(
rs
.
getString
(
j
+
1
));
}
break
;
}
}
// IF ELSE OVER
}
// FOR OVER
}
// WHILE OVER
FileOutputStream
fileOut
=
new
FileOutputStream
(
originalFilePath
);
wb
.
write
(
fileOut
);
//fileOut.flush(); // SXSSFWorkbook 使用 auto-flush 模式
fileOut
.
close
();
//wb.close();
wb
.
dispose
();
// SXSSFWorkbook 没有 close 方法
log
.
debug
(
"runDownloadTask.run"
,
"已生成自助指标下载文件 "
+
condition
.
getTaskId
()
+
".xlsx"
);
}
// IF ELSE OVER
// 如果指定压缩,则使用之
//if (task.getFormat().equals("zip")) {
if
(
task
.
getUseCompress
().
equals
(
Global
.
YES
))
{
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自助指标压缩文件 "
+
condition
.
getTaskId
()
+
".zip"
);
String
zipFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".zip"
;
File
zipFile
=
new
File
(
zipFilePath
);
ZipOutputStream
zos
=
null
;
byte
[]
buf
=
new
byte
[
1024
];
int
length
=
0
;
try
{
OutputStream
os
=
new
FileOutputStream
(
zipFilePath
);
BufferedOutputStream
bos
=
new
BufferedOutputStream
(
os
);
zos
=
new
ZipOutputStream
(
bos
);
zos
.
setLevel
(
6
);
// 压缩率选择 0-9
InputStream
is
=
new
FileInputStream
(
originalFilePath
);
BufferedInputStream
bis
=
new
BufferedInputStream
(
is
);
zos
.
putNextEntry
(
new
ZipEntry
(
originalFilePath
.
substring
(
originalFilePath
.
lastIndexOf
(
"/"
)
+
1
)));
while
((
length
=
bis
.
read
(
buf
))
>
0
)
{
zos
.
write
(
buf
,
0
,
length
);
stat
.
execute
(
"REFRESH TABLE "
+
condition
.
getTableId
());
// 强制刷新表结构
ResultSet
rs
=
stat
.
executeQuery
(
fullQuery
);
// 生成指定格式下载元文件
String
originalFilePath
=
""
;
if
(
task
.
getFormat
().
equals
(
DownloadFileFormat
.
CSV
))
{
// 如果指定为 CSV 格式
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自助指标下载文件 "
+
condition
.
getTaskId
()
+
".csv"
);
originalFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".csv"
;
File
tmp
=
new
File
(
originalFilePath
);
if
(
tmp
.
exists
())
{
// 删除可能存在的文件
tmp
.
delete
();
}
//CSVWriter csvWriter = new CSVWriter(new FileWriter(csvPath), '\t');
OutputStreamWriter
out
=
new
OutputStreamWriter
(
new
FileOutputStream
(
originalFilePath
),
Charset
.
forName
(
"GBK"
));
ResultSetHelper
helper
=
new
CsvResultSetHelper
(
task
.
getQueryDataType
()
==
QueryDataType
.
FULL
?
CsvDataFilterMode
.
DECRYPT
:
CsvDataFilterMode
.
DESENSI
,
condition
.
getDecryptFilters
());
CSVWriter
writer
=
new
CSVWriter
(
out
,
','
);
writer
.
setResultService
(
helper
);
writer
.
writeAll
(
rs
,
true
);
writer
.
close
();
out
.
close
();
//记得关闭资源
log
.
debug
(
"runDownloadTask.run"
,
"已生成自助指标下载文件 "
+
condition
.
getTaskId
()
+
".csv"
);
}
else
{
// 如果指定为 XLS 格式
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自助指标下载文件 "
+
condition
.
getTaskId
()
+
".xlsx"
);
originalFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".xlsx"
;
SXSSFWorkbook
wb
=
new
SXSSFWorkbook
(
100
);
// 内存中保留 100 行
Sheet
sheet
=
wb
.
createSheet
();
Row
row
=
sheet
.
createRow
(
0
);
Cell
cell
;
for
(
int
j
=
0
;
j
<
rs
.
getMetaData
().
getColumnCount
();
++
j
)
{
// 遍历创建表头
String
colName
=
rs
.
getMetaData
().
getColumnLabel
(
j
+
1
);
cell
=
row
.
createCell
(
j
);
cell
.
setCellValue
(
colName
);
}
// 遍历输出行
int
rowCount
=
0
;
while
(
rs
.
next
())
{
rowCount
++;
row
=
sheet
.
createRow
(
rowCount
);
for
(
int
j
=
0
;
j
<
rs
.
getMetaData
().
getColumnCount
();
++
j
)
{
//String c = rs.getString(j + 1);
//row.createCell(j).setCellValue(c);
String
cName
=
rs
.
getMetaData
().
getColumnName
(
j
+
1
);
List
<
String
>
cFilters
=
condition
.
getDecryptFilters
();
if
(
task
.
getQueryDataType
()
==
QueryDataType
.
FULL
&&
cFilters
.
contains
(
cName
))
{
String
tmpResult
=
rs
.
getString
(
j
+
1
);
if
(
StringUtils
.
isNotBlank
(
tmpResult
))
tmpResult
=
DecryptUtils
.
getInstance
().
decrypt
(
tmpResult
);
row
.
createCell
(
j
).
setCellValue
(
tmpResult
);
}
else
{
int
cType
=
rs
.
getMetaData
().
getColumnType
(
j
+
1
);
switch
(
cType
)
{
case
Types
.
TIMESTAMP
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getTimestamp
(
j
+
1
)
!=
null
?
datetimeFormatter
.
format
(
rs
.
getTimestamp
(
j
+
1
))
:
""
);
break
;
case
Types
.
DATE
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getDate
(
j
+
1
)
!=
null
?
dateFormatter
.
format
(
rs
.
getDate
(
j
+
1
))
:
""
);
break
;
case
Types
.
TIME
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getTimestamp
(
j
+
1
)
!=
null
?
timeFormatter
.
format
(
rs
.
getTimestamp
(
j
+
1
))
:
""
);
break
;
default
:
if
(
cFilters
.
contains
(
cName
)){
row
.
createCell
(
j
).
setCellValue
(
"******"
);
}
else
{
row
.
createCell
(
j
).
setCellValue
(
rs
.
getString
(
j
+
1
));
}
break
;
}
}
// IF ELSE OVER
}
// FOR OVER
}
// WHILE OVER
FileOutputStream
fileOut
=
new
FileOutputStream
(
originalFilePath
);
wb
.
write
(
fileOut
);
//fileOut.flush(); // SXSSFWorkbook 使用 auto-flush 模式
fileOut
.
close
();
//wb.close();
wb
.
dispose
();
// SXSSFWorkbook 没有 close 方法
log
.
debug
(
"runDownloadTask.run"
,
"已生成自助指标下载文件 "
+
condition
.
getTaskId
()
+
".xlsx"
);
}
// IF ELSE OVER
// 如果指定压缩,则使用之
//if (task.getFormat().equals("zip")) {
if
(
task
.
getUseCompress
().
equals
(
Global
.
YES
))
{
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自助指标压缩文件 "
+
condition
.
getTaskId
()
+
".zip"
);
String
zipFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".zip"
;
File
zipFile
=
new
File
(
zipFilePath
);
ZipOutputStream
zos
=
null
;
byte
[]
buf
=
new
byte
[
1024
];
int
length
=
0
;
try
{
OutputStream
os
=
new
FileOutputStream
(
zipFilePath
);
BufferedOutputStream
bos
=
new
BufferedOutputStream
(
os
);
zos
=
new
ZipOutputStream
(
bos
);
zos
.
setLevel
(
6
);
// 压缩率选择 0-9
InputStream
is
=
new
FileInputStream
(
originalFilePath
);
BufferedInputStream
bis
=
new
BufferedInputStream
(
is
);
zos
.
putNextEntry
(
new
ZipEntry
(
originalFilePath
.
substring
(
originalFilePath
.
lastIndexOf
(
"/"
)
+
1
)));
while
((
length
=
bis
.
read
(
buf
))
>
0
)
{
zos
.
write
(
buf
,
0
,
length
);
}
bis
.
close
();
is
.
close
();
//bos.close();
//os.close();
log
.
debug
(
"runDownloadTask.run"
,
"已生成自助指标压缩文件 "
+
condition
.
getTaskId
()
+
".zip"
);
}
catch
(
Exception
ex2
)
{
throw
ex2
;
}
finally
{
zos
.
closeEntry
();
zos
.
close
();
}
bis
.
close
();
is
.
close
();
//bos.close();
//os.close();
log
.
debug
(
"runDownloadTask.run"
,
"已生成自助指标压缩文件 "
+
condition
.
getTaskId
()
+
".zip"
);
}
catch
(
Exception
ex2
)
{
throw
ex2
;
}
finally
{
zos
.
closeEntry
();
zos
.
close
();
}
}
task
.
setStatus
(
DownloadTaskStatus
.
COMPLISHED
);
task
.
setOverTime
(
new
Date
());
String
taskFileExt
=
task
.
getUseCompress
().
equals
(
Global
.
YES
)
?
".zip"
:
task
.
getFormat
().
equals
(
DownloadFileFormat
.
CSV
)
?
".csv"
:
".xlsx"
;
task
.
setFilePath
(
task
.
getId
()
+
taskFileExt
);
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
task
.
setStatus
(
DownloadTaskStatus
.
COMPLISHED
);
task
.
setOverTime
(
new
Date
());
String
taskFileExt
=
task
.
getUseCompress
().
equals
(
Global
.
YES
)
?
".zip"
:
task
.
getFormat
().
equals
(
DownloadFileFormat
.
CSV
)
?
".csv"
:
".xlsx"
;
task
.
setFilePath
(
task
.
getId
()
+
taskFileExt
);
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
// 标记任务异常
task
.
setStatus
(
DownloadTaskStatus
.
ERROR
);
task
.
setOverTime
(
new
Date
());
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
}
finally
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
// 标记任务异常
task
.
setStatus
(
DownloadTaskStatus
.
ERROR
);
task
.
setOverTime
(
new
Date
());
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
}
finally
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
}
// IF OVER
}
// IF OVER
}
// IF OVER
}
// 没有任务则忽略
}
// 没有任务则忽略
}
catch
(
Exception
e
){
log
.
debug
(
"自助指标下载异常"
,
e
.
getMessage
());
e
.
printStackTrace
();
}
}
// run Define Over
},
interval
*
1000
,
interval
*
1000
);
// 配置中的值为毫秒
}
...
...
gic-cloud-data-hook-service/src/main/java/com/gic/cloud/data/hook/service/impl/FreeQueryServiceImpl.java
View file @
96a65cb9
...
...
@@ -358,158 +358,163 @@ public class FreeQueryServiceImpl implements IFreeQueryService {
this
.
downloadTaskTimer
.
schedule
(
new
TimerTask
()
{
@Override
public
void
run
()
{
if
(
taskConditions
!=
null
&&
taskConditions
.
size
()
>
0
)
{
//FreeQueryTaskCondition condition = taskConditions.remove(0); // 移除并获取第一个任务条件
FreeQueryTaskCondition
condition
=
null
;
for
(
int
i
=
0
;
i
<
taskConditions
.
size
();
i
++)
{
log
.
debug
(
"自定义查询下载当前任务:"
,
JSON
.
toJSONString
(
taskConditions
.
get
(
i
)));
if
(
taskConditions
.
get
(
i
).
getBuildPermitted
().
equals
(
Global
.
YES
))
{
condition
=
taskConditions
.
remove
(
i
);
// 移除并获取第一个任务条件
break
;
}
// IF OVER
}
// FOR OVER
if
(
condition
!=
null
)
{
// 更新任务状态
DownloadTask
task
=
DownloadTaskServiceImpl
.
getInstance
().
getDownloadTaskById
(
condition
.
getTaskId
());
task
.
setStatus
(
DownloadTaskStatus
.
BUILDING
);
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
String
fullQuery
=
condition
.
getSql
();
Connection
conn
=
MysqlHelper
.
getFreeQueryConnection
(
condition
.
getEnterpriseId
());
if
(
conn
!=
null
)
{
try
{
// 设定为流式读取
Statement
stat
=
conn
.
createStatement
(
ResultSet
.
TYPE_FORWARD_ONLY
,
ResultSet
.
CONCUR_READ_ONLY
);
stat
.
setFetchSize
(
Integer
.
MIN_VALUE
);
ResultSet
rs
=
stat
.
executeQuery
(
fullQuery
);
// 生成指定格式下载元文件
String
originalFilePath
=
""
;
if
(
task
.
getFormat
().
equals
(
DownloadFileFormat
.
CSV
))
{
// 如果指定为 CSV 格式
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自定义查询下载文件 "
+
condition
.
getTaskId
()
+
".csv"
);
//String csvPath = SAVE_FOLDER + "/" + condition.getTaskId() + ".csv";
originalFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".csv"
;
File
tmp
=
new
File
(
originalFilePath
);
if
(
tmp
.
exists
())
{
// 删除可能存在的文件
tmp
.
delete
();
}
try
{
if
(
taskConditions
!=
null
&&
taskConditions
.
size
()
>
0
)
{
//FreeQueryTaskCondition condition = taskConditions.remove(0); // 移除并获取第一个任务条件
FreeQueryTaskCondition
condition
=
null
;
for
(
int
i
=
0
;
i
<
taskConditions
.
size
();
i
++)
{
log
.
debug
(
"自定义查询下载当前任务:"
,
JSON
.
toJSONString
(
taskConditions
.
get
(
i
)));
if
(
taskConditions
.
get
(
i
).
getBuildPermitted
().
equals
(
Global
.
YES
))
{
condition
=
taskConditions
.
remove
(
i
);
// 移除并获取第一个任务条件
break
;
}
// IF OVER
}
// FOR OVER
if
(
condition
!=
null
)
{
// 更新任务状态
DownloadTask
task
=
DownloadTaskServiceImpl
.
getInstance
().
getDownloadTaskById
(
condition
.
getTaskId
());
task
.
setStatus
(
DownloadTaskStatus
.
BUILDING
);
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
//CSVWriter csvWriter = new CSVWriter(new FileWriter(csvPath), '\t');
OutputStreamWriter
out
=
new
OutputStreamWriter
(
new
FileOutputStream
(
originalFilePath
),
Charset
.
forName
(
"GBK"
));
ResultSetHelper
helper
=
new
CsvResultSetHelper
(
task
.
getQueryDataType
()
==
QueryDataType
.
FULL
?
CsvDataFilterMode
.
NONE
:
CsvDataFilterMode
.
DESENSI
,
FreeQueryServiceImpl
.
getFieldsFilters
(
condition
.
getDesensiType
()));
CSVWriter
writer
=
new
CSVWriter
(
out
,
','
);
writer
.
setResultService
(
helper
);
writer
.
writeAll
(
rs
,
true
);
writer
.
close
();
out
.
close
();
log
.
debug
(
"runDownloadTask.run"
,
"已生成自定义查询下载文件 "
+
condition
.
getTaskId
()
+
".csv"
);
}
else
{
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自定义查询下载文件 "
+
condition
.
getTaskId
()
+
".xlsx"
);
originalFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".xlsx"
;
SXSSFWorkbook
wb
=
new
SXSSFWorkbook
(
100
);
// 内存中保留 100 行
Sheet
sheet
=
wb
.
createSheet
();
Row
row
=
sheet
.
createRow
(
0
);
Cell
cell
;
for
(
int
j
=
0
;
j
<
rs
.
getMetaData
().
getColumnCount
();
++
j
)
{
// 遍历创建表头
String
colName
=
rs
.
getMetaData
().
getColumnLabel
(
j
+
1
);
cell
=
row
.
createCell
(
j
);
cell
.
setCellValue
(
colName
);
}
// 遍历输出行
int
rowCount
=
0
;
while
(
rs
.
next
())
{
rowCount
++;
row
=
sheet
.
createRow
(
rowCount
);
for
(
int
j
=
0
;
j
<
rs
.
getMetaData
().
getColumnCount
();
++
j
)
{
//String c = rs.getString(j + 1);
//row.createCell(j).setCellValue(c);
String
cName
=
rs
.
getMetaData
().
getColumnName
(
j
+
1
);
if
(
task
.
getQueryDataType
()
==
QueryDataType
.
SAFE
&&
FreeQueryServiceImpl
.
isFilterFields
(
condition
.
getDesensiType
(),
cName
))
{
row
.
createCell
(
j
).
setCellValue
(
"******"
);
}
else
{
int
cType
=
rs
.
getMetaData
().
getColumnType
(
j
+
1
);
switch
(
cType
)
{
case
Types
.
TIMESTAMP
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getTimestamp
(
j
+
1
)
!=
null
?
datetimeFormatter
.
format
(
rs
.
getTimestamp
(
j
+
1
))
:
""
);
break
;
case
Types
.
DATE
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getDate
(
j
+
1
)
!=
null
?
dateFormatter
.
format
(
rs
.
getDate
(
j
+
1
))
:
""
);
break
;
case
Types
.
TIME
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getTimestamp
(
j
+
1
)
!=
null
?
timeFormatter
.
format
(
rs
.
getTimestamp
(
j
+
1
))
:
""
);
break
;
default
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getString
(
j
+
1
));
break
;
String
fullQuery
=
condition
.
getSql
();
Connection
conn
=
MysqlHelper
.
getFreeQueryConnection
(
condition
.
getEnterpriseId
());
if
(
conn
!=
null
)
{
try
{
// 设定为流式读取
Statement
stat
=
conn
.
createStatement
(
ResultSet
.
TYPE_FORWARD_ONLY
,
ResultSet
.
CONCUR_READ_ONLY
);
stat
.
setFetchSize
(
Integer
.
MIN_VALUE
);
ResultSet
rs
=
stat
.
executeQuery
(
fullQuery
);
// 生成指定格式下载元文件
String
originalFilePath
=
""
;
if
(
task
.
getFormat
().
equals
(
DownloadFileFormat
.
CSV
))
{
// 如果指定为 CSV 格式
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自定义查询下载文件 "
+
condition
.
getTaskId
()
+
".csv"
);
//String csvPath = SAVE_FOLDER + "/" + condition.getTaskId() + ".csv";
originalFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".csv"
;
File
tmp
=
new
File
(
originalFilePath
);
if
(
tmp
.
exists
())
{
// 删除可能存在的文件
tmp
.
delete
();
}
//CSVWriter csvWriter = new CSVWriter(new FileWriter(csvPath), '\t');
OutputStreamWriter
out
=
new
OutputStreamWriter
(
new
FileOutputStream
(
originalFilePath
),
Charset
.
forName
(
"GBK"
));
ResultSetHelper
helper
=
new
CsvResultSetHelper
(
task
.
getQueryDataType
()
==
QueryDataType
.
FULL
?
CsvDataFilterMode
.
NONE
:
CsvDataFilterMode
.
DESENSI
,
FreeQueryServiceImpl
.
getFieldsFilters
(
condition
.
getDesensiType
()));
CSVWriter
writer
=
new
CSVWriter
(
out
,
','
);
writer
.
setResultService
(
helper
);
writer
.
writeAll
(
rs
,
true
);
writer
.
close
();
out
.
close
();
log
.
debug
(
"runDownloadTask.run"
,
"已生成自定义查询下载文件 "
+
condition
.
getTaskId
()
+
".csv"
);
}
else
{
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自定义查询下载文件 "
+
condition
.
getTaskId
()
+
".xlsx"
);
originalFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".xlsx"
;
SXSSFWorkbook
wb
=
new
SXSSFWorkbook
(
100
);
// 内存中保留 100 行
Sheet
sheet
=
wb
.
createSheet
();
Row
row
=
sheet
.
createRow
(
0
);
Cell
cell
;
for
(
int
j
=
0
;
j
<
rs
.
getMetaData
().
getColumnCount
();
++
j
)
{
// 遍历创建表头
String
colName
=
rs
.
getMetaData
().
getColumnLabel
(
j
+
1
);
cell
=
row
.
createCell
(
j
);
cell
.
setCellValue
(
colName
);
}
// 遍历输出行
int
rowCount
=
0
;
while
(
rs
.
next
())
{
rowCount
++;
row
=
sheet
.
createRow
(
rowCount
);
for
(
int
j
=
0
;
j
<
rs
.
getMetaData
().
getColumnCount
();
++
j
)
{
//String c = rs.getString(j + 1);
//row.createCell(j).setCellValue(c);
String
cName
=
rs
.
getMetaData
().
getColumnName
(
j
+
1
);
if
(
task
.
getQueryDataType
()
==
QueryDataType
.
SAFE
&&
FreeQueryServiceImpl
.
isFilterFields
(
condition
.
getDesensiType
(),
cName
))
{
row
.
createCell
(
j
).
setCellValue
(
"******"
);
}
else
{
int
cType
=
rs
.
getMetaData
().
getColumnType
(
j
+
1
);
switch
(
cType
)
{
case
Types
.
TIMESTAMP
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getTimestamp
(
j
+
1
)
!=
null
?
datetimeFormatter
.
format
(
rs
.
getTimestamp
(
j
+
1
))
:
""
);
break
;
case
Types
.
DATE
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getDate
(
j
+
1
)
!=
null
?
dateFormatter
.
format
(
rs
.
getDate
(
j
+
1
))
:
""
);
break
;
case
Types
.
TIME
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getTimestamp
(
j
+
1
)
!=
null
?
timeFormatter
.
format
(
rs
.
getTimestamp
(
j
+
1
))
:
""
);
break
;
default
:
row
.
createCell
(
j
).
setCellValue
(
rs
.
getString
(
j
+
1
));
break
;
}
}
}
}
// FOR OVER
}
// WHILE OVER
FileOutputStream
fileOut
=
new
FileOutputStream
(
originalFilePath
);
wb
.
write
(
fileOut
);
//fileOut.flush(); // SXSSFWorkbook 使用 auto-flush 模式
fileOut
.
close
();
//wb.close();
wb
.
dispose
();
// SXSSFWorkbook 没有 close 方法
log
.
debug
(
"runDownloadTask.run"
,
"已生成自定义查询下载文件 "
+
condition
.
getTaskId
()
+
".xlsx"
);
}
}
// FOR OVER
}
// WHILE OVER
FileOutputStream
fileOut
=
new
FileOutputStream
(
originalFilePath
);
wb
.
write
(
fileOut
);
//fileOut.flush(); // SXSSFWorkbook 使用 auto-flush 模式
fileOut
.
close
();
//wb.close();
wb
.
dispose
();
// SXSSFWorkbook 没有 close 方法
log
.
debug
(
"runDownloadTask.run"
,
"已生成自定义查询下载文件 "
+
condition
.
getTaskId
()
+
".xlsx"
);
}
if
(
task
.
getUseCompress
().
equals
(
Global
.
YES
))
{
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自定义查询压缩文件 "
+
condition
.
getTaskId
()
+
".zip"
);
String
zipFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".zip"
;
File
zipFile
=
new
File
(
zipFilePath
);
ZipOutputStream
zos
=
null
;
byte
[]
buf
=
new
byte
[
1024
];
int
length
=
0
;
try
{
OutputStream
os
=
new
FileOutputStream
(
zipFilePath
);
BufferedOutputStream
bos
=
new
BufferedOutputStream
(
os
);
zos
=
new
ZipOutputStream
(
bos
);
zos
.
setLevel
(
6
);
// 压缩率选择 0-9
InputStream
is
=
new
FileInputStream
(
originalFilePath
);
BufferedInputStream
bis
=
new
BufferedInputStream
(
is
);
zos
.
putNextEntry
(
new
ZipEntry
(
originalFilePath
.
substring
(
originalFilePath
.
lastIndexOf
(
"/"
)
+
1
)));
while
((
length
=
bis
.
read
(
buf
))
>
0
)
{
zos
.
write
(
buf
,
0
,
length
);
if
(
task
.
getUseCompress
().
equals
(
Global
.
YES
))
{
log
.
debug
(
"runDownloadTask.run"
,
"准备生成自定义查询压缩文件 "
+
condition
.
getTaskId
()
+
".zip"
);
String
zipFilePath
=
SAVE_FOLDER
+
"/"
+
condition
.
getTaskId
()
+
".zip"
;
File
zipFile
=
new
File
(
zipFilePath
);
ZipOutputStream
zos
=
null
;
byte
[]
buf
=
new
byte
[
1024
];
int
length
=
0
;
try
{
OutputStream
os
=
new
FileOutputStream
(
zipFilePath
);
BufferedOutputStream
bos
=
new
BufferedOutputStream
(
os
);
zos
=
new
ZipOutputStream
(
bos
);
zos
.
setLevel
(
6
);
// 压缩率选择 0-9
InputStream
is
=
new
FileInputStream
(
originalFilePath
);
BufferedInputStream
bis
=
new
BufferedInputStream
(
is
);
zos
.
putNextEntry
(
new
ZipEntry
(
originalFilePath
.
substring
(
originalFilePath
.
lastIndexOf
(
"/"
)
+
1
)));
while
((
length
=
bis
.
read
(
buf
))
>
0
)
{
zos
.
write
(
buf
,
0
,
length
);
}
bis
.
close
();
is
.
close
();
//bos.close();
//os.close();
log
.
debug
(
"runDownloadTask.run"
,
"已生成自定义查询压缩文件 "
+
condition
.
getTaskId
()
+
".zip"
);
}
catch
(
Exception
ex2
)
{
throw
ex2
;
}
finally
{
zos
.
closeEntry
();
zos
.
close
();
}
bis
.
close
();
is
.
close
();
//bos.close();
//os.close();
log
.
debug
(
"runDownloadTask.run"
,
"已生成自定义查询压缩文件 "
+
condition
.
getTaskId
()
+
".zip"
);
}
catch
(
Exception
ex2
)
{
throw
ex2
;
}
finally
{
zos
.
closeEntry
();
zos
.
close
();
}
}
task
.
setStatus
(
DownloadTaskStatus
.
COMPLISHED
);
task
.
setOverTime
(
new
java
.
util
.
Date
());
task
.
setStatus
(
DownloadTaskStatus
.
COMPLISHED
);
task
.
setOverTime
(
new
java
.
util
.
Date
());
String
taskFileExt
=
task
.
getUseCompress
().
equals
(
Global
.
YES
)
?
".zip"
:
task
.
getFormat
().
equals
(
DownloadFileFormat
.
CSV
)
?
".csv"
:
".xlsx"
;
task
.
setFilePath
(
task
.
getId
()
+
taskFileExt
);
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
String
taskFileExt
=
task
.
getUseCompress
().
equals
(
Global
.
YES
)
?
".zip"
:
task
.
getFormat
().
equals
(
DownloadFileFormat
.
CSV
)
?
".csv"
:
".xlsx"
;
task
.
setFilePath
(
task
.
getId
()
+
taskFileExt
);
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
// 标记任务异常
task
.
setStatus
(
DownloadTaskStatus
.
ERROR
);
task
.
setOverTime
(
new
Date
());
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
}
finally
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
// 标记任务异常
task
.
setStatus
(
DownloadTaskStatus
.
ERROR
);
task
.
setOverTime
(
new
Date
());
DownloadTaskServiceImpl
.
getInstance
().
updateDownloadTask
(
task
);
}
finally
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
}
// IF OVER
}
// IF OVER
}
// IF OVER
}
// 没有任务则忽略
}
// 没有任务则忽略
}
catch
(
Exception
e
){
log
.
debug
(
"自定义查询下载异常"
,
e
.
getMessage
());
e
.
printStackTrace
();
}
}
// run Define Over
},
interval
*
1000
,
interval
*
1000
);
// 配置中的值为毫秒
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment