-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature][ConnectorV2]add file excel sink #2585
Changes from 1 commit
049652d
e525451
89e2b4f
0a5fd71
b64e547
1265c23
a595be9
182f009
4ad6160
c218193
c5470d4
4973b36
e6178bb
0fe3dfe
93cbbb0
a9e57f1
2513f4f
144bb27
a699681
2fb89b7
d3359f7
f025b89
9a77d7e
06edf3b
6eee00e
162ba2f
7605250
971bc78
00134e4
874af27
4f8d572
5754104
78f8b2d
9870a07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.seatunnel.connectors.seatunnel.file.sink.util; | ||
|
||
import org.apache.seatunnel.api.table.type.BasicType; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelDataType; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRowType; | ||
|
||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.CellStyle; | ||
import org.apache.poi.ss.usermodel.CreationHelper; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.List; | ||
|
||
public class ExcelGenerator { | ||
private Workbook wb; | ||
private CellStyle wholeNumberCellStyle; | ||
private CellStyle stringCellStyle; | ||
private int row = 0; | ||
private List<Integer> sinkColumnsIndexInRow; | ||
private SeaTunnelRowType seaTunnelRowType; | ||
|
||
public ExcelGenerator(List<Integer> sinkColumnsIndexInRow, SeaTunnelRowType seaTunnelRowType) { | ||
this.sinkColumnsIndexInRow = sinkColumnsIndexInRow; | ||
this.seaTunnelRowType = seaTunnelRowType; | ||
wb = new XSSFWorkbook(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@TyrantLucifer @Hisoka-X @CalvinKirs @EricJoy2048 reference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using |
||
Sheet st = wb.createSheet("Sheet1"); | ||
Row row = st.createRow(this.row); | ||
for (Integer i : sinkColumnsIndexInRow) { | ||
String fieldName = seaTunnelRowType.getFieldName(i); | ||
row.createCell(i).setCellValue(fieldName); | ||
} | ||
|
||
wholeNumberCellStyle = createStyle(wb, "General"); | ||
stringCellStyle = createStyle(wb, "@"); | ||
this.row += 1; | ||
} | ||
|
||
public void writeData(SeaTunnelRow seaTunnelRow) { | ||
Sheet st = wb.getSheet("Sheet1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about treated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh ,Let me modify that |
||
Row excelRow = st.createRow(this.row); | ||
SeaTunnelDataType<?>[] fieldTypes = seaTunnelRowType.getFieldTypes(); | ||
for (Integer i : sinkColumnsIndexInRow) { | ||
Cell cell = excelRow.createCell(i); | ||
Object value = seaTunnelRow.getField(i); | ||
makeConverter(fieldTypes[i], value, cell); | ||
} | ||
this.row += 1; | ||
} | ||
|
||
public void flushAndCloseExcel(OutputStream output) throws IOException { | ||
wb.write(output); | ||
wb.close(); | ||
} | ||
|
||
private void makeConverter(SeaTunnelDataType<?> type, Object value, Cell cell) { | ||
if (value == null) { | ||
cell.setBlank(); | ||
} else if (BasicType.STRING_TYPE.equals(type)) { | ||
cell.setCellValue((String) value); | ||
cell.setCellStyle(stringCellStyle); | ||
} else if (BasicType.BOOLEAN_TYPE.equals(type)) { | ||
cell.setCellValue((Boolean) value); | ||
cell.setCellStyle(wholeNumberCellStyle); | ||
} else if (BasicType.BYTE_TYPE.equals(type)) { | ||
cell.setCellValue((byte) value); | ||
cell.setCellStyle(wholeNumberCellStyle); | ||
} else if (BasicType.SHORT_TYPE.equals(type)) { | ||
cell.setCellValue((short) value); | ||
cell.setCellStyle(wholeNumberCellStyle); | ||
} else if (BasicType.INT_TYPE.equals(type)) { | ||
cell.setCellValue((int) value); | ||
cell.setCellStyle(wholeNumberCellStyle); | ||
} else if (BasicType.LONG_TYPE.equals(type)) { | ||
cell.setCellValue((long) value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cell.setCellStyle(wholeNumberCellStyle); | ||
} else if (BasicType.FLOAT_TYPE.equals(type)) { | ||
cell.setCellValue((float) value); | ||
cell.setCellStyle(wholeNumberCellStyle); | ||
} else if (BasicType.DOUBLE_TYPE.equals(type)) { | ||
cell.setCellValue((double) value); | ||
cell.setCellStyle(wholeNumberCellStyle); | ||
} else { | ||
String errorMsg = String.format("[%s] type not support ", type.getSqlType()); | ||
throw new RuntimeException(errorMsg); | ||
} | ||
} | ||
|
||
private CellStyle createStyle(Workbook wb, String format) { | ||
CreationHelper creationHelper = wb.getCreationHelper(); | ||
CellStyle cellStyle = wb.createCellStyle(); | ||
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(format)); | ||
return cellStyle; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.seatunnel.connectors.seatunnel.file.sink.writer; | ||
|
||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.connectors.seatunnel.file.sink.config.TextFileSinkConfig; | ||
import org.apache.seatunnel.connectors.seatunnel.file.sink.util.ExcelGenerator; | ||
import org.apache.seatunnel.connectors.seatunnel.file.sink.util.FileSystemUtils; | ||
|
||
import lombok.NonNull; | ||
import org.apache.hadoop.fs.FSDataOutputStream; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ExcelWriteStrategy extends AbstractWriteStrategy { | ||
private Map<String, ExcelGenerator> beingWrittenWriter; | ||
|
||
public ExcelWriteStrategy(TextFileSinkConfig textFileSinkConfig) { | ||
super(textFileSinkConfig); | ||
this.beingWrittenWriter = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public void write(SeaTunnelRow seaTunnelRow) throws Exception { | ||
String filePath = getOrCreateFilePathBeingWritten(seaTunnelRow); | ||
ExcelGenerator excelGenerator = getOrCreateExcelGenerator(filePath); | ||
excelGenerator.writeData(seaTunnelRow); | ||
} | ||
|
||
@Override | ||
public void finishAndCloseFile() { | ||
this.beingWrittenWriter.forEach((k, v) -> { | ||
try { | ||
FileSystemUtils.createFile(k); | ||
FSDataOutputStream fileOutputStream = FileSystemUtils.getOutputStream(k); | ||
v.flushAndCloseExcel(fileOutputStream); | ||
fileOutputStream.close(); | ||
} catch (IOException e) { | ||
log.error("can not get output file stream"); | ||
throw new RuntimeException(e); | ||
} | ||
needMoveFiles.put(k, getTargetLocation(k)); | ||
}); | ||
} | ||
|
||
private ExcelGenerator getOrCreateExcelGenerator(@NonNull String filePath) { | ||
ExcelGenerator excelGenerator = this.beingWrittenWriter.get(filePath); | ||
if (excelGenerator == null) { | ||
excelGenerator = new ExcelGenerator(sinkColumnsIndexInRow, seaTunnelRowType); | ||
this.beingWrittenWriter.put(filePath, excelGenerator); | ||
} | ||
return excelGenerator; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.seatunnel.connectors.seatunnel.file.source.reader; | ||
|
||
import org.apache.seatunnel.api.source.Collector; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRowType; | ||
import org.apache.seatunnel.connectors.seatunnel.file.config.HadoopConf; | ||
import org.apache.seatunnel.connectors.seatunnel.file.exception.FilePluginException; | ||
|
||
public class ExcelReadStrategy extends AbstractReadStrategy { | ||
@Override | ||
public void read(String path, Collector<SeaTunnelRow> output) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public SeaTunnelRowType getSeaTunnelRowTypeInfo(HadoopConf hadoopConf, String path) throws FilePluginException { | ||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, please move connector dependency to connector's pom.xml. Reference: #2630
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, they have been moved to connector's pom.xml