关于人工智能:ChatGPT应用篇助力Beauty代码的初体验-京东云技术团队

42次阅读

共计 11395 个字符,预计需要花费 29 分钟才能阅读完成。

思考过程:

案例 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 的工作原理及颠覆性影响,通俗易懂(愿学有所得)

职责有边界、思考无边界、担当无边界!!!

利用好 ChatGPT 所有皆有可能,用好新时代的生产力工具心愿 ChatGPT 能助力大家 更上一层楼!

正文完
 0