思考过程:
可以去这个网站进行体验:https://k.zank.cc/#/chat/1002 (如进不去看下截图)
(原文链接: 登录JoySpace )
案例1:项目里面有Excel文件的解析场景,试着与ChatGPT进行了交互,现将问题整理如下:
1.给我写一个Java版本的excel导入解析代码 (毫不客气的分配任务)
2.需要支持100000000数据量 (业务需求变更)
3.优化代码需要支持10000000数据量 (降低数量级,减轻难度)
4.请采用面向对象的思想给做一下封装 (初级工程师 -> 中级工程师)
5.进一步面向接口编程,抽离业务 (中级晋升应该加一点泛型诉求,代码更Beauty)
6.替换掉 poi 采用EasyExcel (替换原始的默认技术选型,替换三方包)
7.进一步优化,能支持EasyExcel、POI自由切换 (问题没问好!本意是想让他进一步抽象,能基于SPI支持扩展点)
8.采用控制反转方式优化 (与问题5有重复)
9.提升解析性能,采用多线程并行解析优化 (中级工程->高级工程师)
10.提升程序效率,把解析性能在提升10倍 (架构师的成本意识,这个问题的回答有点意思)
11.采用Spring IOC 控制反转方式进一步封装 (与问题8有重复)
12.添加异常 logger 打印 (优化细节)
13.添加更多的中文代码注释 (增强易读性,优化细节)
14.将中文注释替换成英文注释 (^_^ 开始走向国际化)
15.日志错误信息支持 国际化 资源文件 (国际化的开始……)
16.把上面Excel解析的代码在给来个单元测试吧
17.…………………………………………
案例2:项目里面一个ES深度分页优化的场景,问题整理如下:
1.Elasticsearch 深度分页问题
2.Elasticsearch 深度分页Java代码
3.以上代码采用面向IOC优化
4.进一步面向对象优化
5.基于泛型进行抽象
6.将 scroll API 替换为 searchafter
7.使用 spring data elasticsearch 优化并实现 search After
8.以上代码请采用jmeter 生成一个压测脚本
9.上面Spring Data Elasticsearch 实现 search after 的示例代码请采用 groovy 语言生成测试用例
案例2的代码就不贴出来了,大家感兴趣可以自行输入以上问题进行体验!
(个人体会:程序领域问问题要采用专业术语,专业术语越强越能精准给予反馈!)
说明:对于ChatGPT输出的代码没有做仔细的Review及调试,但是看代码风格基本上是改改就能用于生产的,对于代码规范、命名还是很认可的!
案例过程:
Happy Coding!Beauty Code!
以下是一个与ChatGPT 结对编程的一个Excel数据解析案例,大家可以根据我的思考过程进行体验,以下放了一些截图,ChatGPT的反应速度惊人,代码版本一步步进行迭代优化,感触较深的是真正的高质量低代码平台要应运而生了!
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.listener.ReadListener;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet as PoiSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelParser {
private ParserType parserType;
public ExcelParser(ParserType parserType) {
this.parserType = parserType;
}
public List<Map<String, String>> parse(File file, int sheetIndex) throws IOException {
if (parserType == ParserType.EASY_EXCEL) {
return parseWithEasyExcel(file, sheetIndex);
} else if (parserType == ParserType.POI) {
return parseWithPoi(file, sheetIndex);
} else {
throw new IllegalArgumentException("Invalid parser type");
}
}
private List<Map<String, String>> parseWithEasyExcel(File file, int sheetIndex) {
List<Map<String, String>> data = new ArrayList<>();
DataListener listener = new DataListener(data);
ExcelReaderBuilder readerBuilder = EasyExcel.read(file, listener);
Sheet sheet = new Sheet(sheetIndex + 1, 0);
readerBuilder.sheet(sheet).doRead();
return data;
}
private List<Map<String, String>> parseWithPoi(File file, int sheetIndex) throws IOException {
List<Map<String, String>> data = new ArrayList<>();
FileInputStream inputStream = new FileInputStream(file);
HSSFWorkbook workbook = file.getName().endsWith(".xls") ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);
PoiSheet sheet = workbook.getSheetAt(sheetIndex);
List<String> headers = new ArrayList<>();
for (Cell cell : sheet.getRow(0)) {
headers.add(cell.getStringCellValue());
}
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
Map<String, String> record = new HashMap<>();
for (int j = 0; j < headers.size(); j++) {
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String value = "";
switch (cell.getCellType()) {
case STRING:
value = cell.getStringCellValue();
break;
case NUMERIC:
value = Double.toString(cell.getNumericCellValue());
break;
default:
value = cell.getStringCellValue();
break;
}
record.put(headers.get(j), value);
}
data.add(record);
}
workbook.close();
inputStream.close();
return data;
}
public static void main(String[] args) throws IOException {
File file = new File("excel_file.xlsx");
ExcelParser parser = new ExcelParser(ParserType.EASY_EXCEL);
List<Map<String, String>> data = parser.parse(file, 0);
System.out.println(data);
}
}
enum ParserType {
EASY_EXCEL,
POI
}
class DataListener extends ReadListener<Data> {
private List<Map<String, String>> data;
private List<String> headers;
public DataListener(List<Map<String, String>> data) {
this.data = data;
this.headers = new ArrayList<>();
}
@Override
public void onException(Exception exception) {
// 处理异常
}
@Override
public void onReadError(Exception exception) {
// 处理读取错误
}
@Override
public void onSheet(int sheetIndex, Sheet sheet) {
// 处理工作表信息
}
@Override
public void onReadEnd() {
// 读取结束
}
@Override
public void onHeadRow(Row row) {
for (Cell cell : row) {
headers.add(cell.getStringCellValue());
}
}
@Override
public void onRecord(Data data) {
Map<String, String> record = new HashMap<>();
for (int i = 0; i < headers.size(); i++) {
record.put(headers.get(i), data.getField(i));
}
this.data.add(record);
}
}
class Data {
@ExcelProperty(index = 0)
private String name;
@ExcelProperty(index = 1)
private int age;
import com.alibaba.excel.annotation.ExcelProperty;
import java.util.List;
import java.util.Map;
public class ExcelParser {
private SheetParser sheetParser;
public ExcelParser(SheetParser sheetParser) {
this.sheetParser = sheetParser;
}
public List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception {
return sheetParser.parse(fileName, sheetIndex);
}
public static void main(String[] args) throws Exception {
String fileName = "excel_file.xlsx";
SheetParser sheetParser = new EasyExcelSheetParser();
ExcelParser parser = new ExcelParser(sheetParser);
List<Map<String, String>> data = parser.parse(fileName, 0);
System.out.println(data);
}
}
interface SheetParser {
List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception;
}
class EasyExcelSheetParser implements SheetParser {
@Override
public List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception {
List<Map<String, String>> data = new ArrayList<>();
DataListener listener = new DataListener(data);
ExcelReaderBuilder readerBuilder = EasyExcel.read(fileName, listener);
Sheet sheet = new Sheet(sheetIndex + 1, 0);
readerBuilder.sheet(sheet).doRead();
return data;
}
}
class PoiSheetParser implements SheetParser {
@Override
public List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception {
List<Map<String, String>> data = new ArrayList<>();
FileInputStream inputStream = new FileInputStream(fileName);
HSSFWorkbook workbook = fileName.endsWith(".xls") ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);
PoiSheet sheet = workbook.getSheetAt(sheetIndex);
List<String> headers = new ArrayList<>();
for (Cell cell : sheet.getRow(0)) {
headers.add(cell.getStringCellValue());
}
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
Map<String, String> record = new HashMap<>();
for (int j = 0; j < headers.size(); j++) {
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String value = "";
switch (cell.getCellType()) {
case STRING:
value = cell.getStringCellValue();
break;
case NUMERIC:
value = Double.toString(cell.getNumericCellValue());
break;
default:
value = cell.getStringCellValue();
break;
}
record.put(headers.get(j), value);
}
data.add(record);
}
workbook.close();
inputStream.close();
return data;
}
}
class DataListener extends ReadListener<Data> {
private List<Map<String, String>> data;
private List<String> headers;
public DataListener(List<Map<String, String>> data) {
this.data = data;
this.headers = new ArrayList<>();
}
@Override
public void onException(Exception exception) {
// 处理异常
}
@Override
public void onReadError(Exception exception) {
// 处理读取错误
}
@Override
public void onSheet(int sheetIndex, Sheet sheet) {
// 处理工作表信息
}
@Override
public void onReadEnd() {
// 读取结束
}
@Override
public void onHeadRow(Row row) {
for (Cell cell : row) {
headers.add(cell.getStringCellValue());
}
}
@Override
public void onRecord(Data data) {
Map<String, String> record = new HashMap<>();
for (int i = 0; i < headers.size(); i++) {
record.put(headers.get(i), data.getField(i));
}
this.data.add(record);
}
}
class Data {
@ExcelProperty(index = 0)
private String name;
@ExcelProperty(index = 1)
private int age;
// Getters and setters
}
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.listener.ReadListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
public class ExcelParser {
private SheetParser sheetParser;
public ExcelParser(SheetParser sheetParser) {
this.sheetParser = sheetParser;
}
public List<Map<String, String>> parse(String fileName, int sheetIndex) throws ExecutionException, InterruptedException {
return sheetParser.parse(fileName, sheetIndex);
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
String fileName = "excel_file.xlsx";
ExecutorService executor = Executors.newFixedThreadPool(2);
List<Future<List<Map<String, String>>>> futures = new ArrayList<>();
for (int i = 0; i < 2; i++) {
SheetParser sheetParser = new EasyExcelSheetParser();
Future<List<Map<String, String>>> future = executor.submit(() -> sheetParser.parse(fileName, i));
futures.add(future);
}
List<Map<String, String>> data = new ArrayList<>();
for (Future<List<Map<String, String>>> future : futures) {
data.addAll(future.get());
}
System.out.println(data);
executor.shutdown();
}
}
interface SheetParser {
List<Map<String, String>> parse(String fileName, int sheetIndex);
}
class EasyExcelSheetParser implements SheetParser {
@Override
public List<Map<String, String>> parse(String fileName, int sheetIndex) {
List<Map<String, String>> data = new ArrayList<>();
DataListener listener = new DataListener(data);
ExcelReaderBuilder readerBuilder = EasyExcel.read(fileName, listener);
Sheet sheet = new Sheet(sheetIndex + 1, 0);
readerBuilder.sheet(sheet).doRead();
return data;
}
}
class PoiSheetParser implements SheetParser {
@Override
public List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception {
// 实现 POI 的解析,类似于前面的示例
}
}
class DataListener extends ReadListener<Data> {
private List<Map<String, String>> data;
private List<String> headers;
public DataListener(List<Map<String, String>> data) {
this.data = data;
this.headers = new ArrayList<>();
}
@Override
public void onHeadRow(Row row) {
for (Cell cell : row) {
headers.add(cell.getStringCellValue());
}
}
@Override
public void onRecord(Data data) {
Map<String, String> record = new HashMap<>();
for (int i = 0; i < headers.size(); i++) {
record.put(headers.get(i), data.getField(i));
}
this.data.add(record);
}
}
class Data {
@ExcelProperty(index = 0)
private String name;
@ExcelProperty(index = 1)
private int age;
// Getters and setters
}
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ExcelParser {
private SheetParser sheetParser;
@Autowired
public ExcelParser(SheetParser sheetParser) {
this.sheetParser = sheetParser;
}
public List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception {
return sheetParser.parse(fileName, sheetIndex);
}
}
interface SheetParser {
List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception;
}
@Component
class EasyExcelSheetParser implements SheetParser {
@Override
public List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception {
List<Map<String, String>> data = new ArrayList<>();
DataListener listener = new DataListener(data);
ExcelReaderBuilder readerBuilder = EasyExcel.read(fileName, listener);
Sheet sheet = new Sheet(sheetIndex + 1, 0);
readerBuilder.sheet(sheet).doRead();
return data;
}
}
@Component
class PoiSheetParser implements SheetParser {
@Override
public List<Map<String, String>> parse(String fileName, int sheetIndex) throws Exception {
// 实现 POI 的解析,类似于前面的示例
}
}
class DataListener extends ReadListener<Data> {
private List<Map<String, String>> data;
private List<String> headers;
public DataListener(List<Map<String, String>> data) {
this.data = data;
this.headers = new ArrayList<>();
}
@Override
public void onHeadRow(Row row) {
for (Cell cell : row) {
headers.add(cell.getStringCellValue());
}
}
@Override
public void onRecord(Data data) {
Map<String, String> record = new HashMap<>();
for (int i = 0; i < headers.size(); i++) {
record.put(headers.get(i), data.getField(i));
}
this.data.add(record);
}
}
class Data {
@ExcelProperty(index = 0)
private String name;
@ExcelProperty(index = 1)
private int age;
// Getters and setters
}
原理学习:
知其然,并未知其所以然!直到看到下面的解惑视频……
视频号搜索:于建国博士讲解ChatGPT的工作原理及颠覆性影响,通俗易懂 (愿学有所得)
特别感谢:云交易团队Leader(龙柯帆)分享ChatGPT原理,学而解惑,为Leader技术敏感度点赞。
职责有边界、思考无边界、担当无边界!!!
利用好ChatGPT一切皆有可能,用好新时代的生产力工具希望ChatGPT能助力大家更上一层楼!