PERSONAL NOTES & ENGINEERING JOURNAL EST. 2021

记录技术,也记录生活。

喜欢在晨跑与灯下阅读中沉淀慢思考。这里没有喧嚣的信息流,只有经得起时间检验的笔记、随笔与复盘。

独立 · 缓慢 · 长期主义 2021 — 2031 持续书写中 友链 留言
FROM THE JOURNAL

最近写作 · Latest Notes

近期 10 篇 · 共 144 篇
2024 年 1 篇记录
Spring AI 来了 NEW

Spring AI 项目旨在简化包含人工智能功能的应用程序的开发,避免不必要的复杂性。 该项目从著名的 Python 项目(例如 LangChain 和 LlamaIndex)中汲取灵感,但 Spring AI 并不是这些项目的直接移植。 该项目的成立相信下一波生成式人工智能应用程序不仅适用于 Python 开发人员,而且将在许多编程语言中普遍存在。 Spring AI 的核心提供了抽象,作为开发 AI 应用程序的基础。 这些抽象有多种实现,可以通过最少的代码更改轻松进行组件交换。 一、Spring AI 提供以下功能 Support for all major Model providers such as OpenAI, Microsoft, Amazon, Google, and Huggingface. (支持所有主要模型提供商,例如 OpenAI、Microsoft、Amazon、Google 和 Huggingface) Supported Model types are Chat and Text to Image with more on the way. (支持的模型类型包括"聊天"和"文本到图像",还有更多模型类型正在开发中) Portable API across AI providers for Chat and for Embedding models. Both synchronous and stream API options are supported. Dropping down to access model specific features is also supported. (跨 AI 提供商的可移植 API,用于聊天和嵌入模型。 支持同步和流 API 选项。 还支持下拉访问模型特定功能) Mapping of AI Model output to POJOs. (AI 模型输出到 POJO 的映射) Support for all major Vector Database providers such as Azure Vector Search, Chroma, Milvus, Neo4j, PostgreSQL/PGVector, PineCone, Qdrant, Redis, and Weaviate (支持所有主要矢量数据库提供商,例如 Azure 矢量搜索、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate) Portable API across Vector Store providers, including a novel SQL-like metadata filter API that is also portable. (跨 Vector Store 提供商的可移植 API,包括同样可移植的新颖的类似 SQL 的元数据过滤器 API) Function calling (函数调用) Spring Boot Auto Configuration and Starters for AI Models and Vector Stores. (AI 模型和向量存储的 Spring Boot 自动配置和启动器) ETL framework for Data Engineering (数据工程的 ETL 框架)

2023 年 2 篇记录
2023国庆川西自驾游

国庆在家待了2天,自驾游川西3天,提前2天回来。整个行程3天,时间比较紧,全程700公里左右,主要景点:红海子、斯丁措、鱼子西、新都桥、折多山。 自驾车型:城市SUV,1.4T排量。 自驾前准备: 0、安全:检车辆车胎气压、油、玻璃水、机油等情况。 1、药品:防高反药、氧气筒,一些退烧药、感冒药、葡萄糖、碘伏等药品。 2、衣服:冬季羽绒服、保暖裤、防风外套、短袖T恤额、长袖秋衣、牛仔裤、运动鞋2双。 3、棉絮、厚被子、雨伞、遮阳帽、防嗮袖。 4、方便面、矿泉水、水果、零食、红牛、八宝粥。 5、工具:水果刀、湿巾纸、干纸巾、透明胶、剪刀、灭火器、帐篷、超大保温杯。 上面有些东西不是必须的,只是防止为了突发情况时使用,比如半夜在高速堵车、或者高速上车外了,或者车着火了等极端情况。 人员:计划自己和老爸2人自驾的,因为妈身体不太好,也不想去那么远的地方,老婆要带儿子回娘家玩,由于担心开车太累或者出现紧急情况所以叫上了亲姐,结果侄儿也要去,所以一路上4个人,能开车的就我和姐。 线路规划: 计划路线:成都环球上高速->京昆高速->雅叶高速->康定 实际路线:成都环球上高速->京昆高速->雅叶高速->泸定县 本来计划先到达康定住一晚,第二天开始游玩。但是用于第3天中午吃了午饭才从成都出发,6个小时才能到达康,预估到不了康定,所以第一天先到泸定县住了一晚,晚上逛了一下泸定县,至此第一天就结束了,第一天影响比较深的就是下高速后进泸定县的路比较陡,城市很难见到,然后就是所谓的勇士路,很堵,停车场爆满,直接开发泸定县城附近在找到合适停车地方。 第二天一大早,到泸定县吃了早餐,逛了泸定桥、大渡河、泸定纪念公园就直接出发康定红海子,期间途径康定县城堵车严重,之后翻过折多山(堵得要命)。在上折多山途中停车弄了一碗泡面当作午餐、吃了一些水果。然后一路拍照,之后逛了红海子、斯丁措、鱼子西、新都桥,经过康定机场。空气、天气很好,蓝天白云、就是太堵了。第二天时间比较紧,加上姐高反比较严重,所以一直催着到低海拔的新都桥镇去休息。我和老爸高反还不是特别严重。上面几个地方拍照完后,下午直接奔新都桥,也是一路堵,下午半天差不多就一直堵在车上,就这样第二天就匆匆忙忙的完了。晚上到达新都桥找好住宿后,休息一会在新都桥镇上吃了晚上,就早早的休息了。 第三天吃完早饭,一早去了新都桥,结果和想象的景色不太一样,拍了一些照片。然后看到附近的贡嘎山观景台,去爬了一会实在爬不动,加上还要开车就爬了一点点。接着买了一些牛肉120元/每斤。接着就往回走了,本来计划是还要到塔公草原、墨石公园玩的,但是姐高反加姐只有3天假期所以就放弃了。下午返回途径318国道,回途中印象最深的就是回来的318国道开车比较有意思,陡峭、弯急,回途经过折多山也是堵的要命。 去和回的高速上基本不堵车,车速保持在110~125左右,开车印象最深的就是二郎山附近的几个隧道全长20公里左右,去基本是上坡,回的时候基本保持80码的速度,全是下坡,基本不怎么耗油。3天全是我一个人开,膝盖都酸了,翻越折多山时叫姐开,她不敢开,最后还是自己来,3天全部行车700公里左右。来途径成都浦江服务区、雅安天全服务区、泸定服务区,回去途中在雅安天全服务区、浦江服务区休息了一会。回家途中天全服务区侄儿开门不小心将旁边的马自达车门碰了一个小吭,陪了200元。幸好是个马子达,就当吸取教训吧。 晚上达到姐家,爸和姐去超市买菜炒了一份回锅肉、买了一瓶大可乐,吃完饭,去了附近姐经常自助洗车的洗车店4个人一起把车洗了,晚上晚上开车回家。 感受:3天时间比较紧,加上高反,各景点没有好好玩,各个地方的乡镇没有好好逛逛、好吃的也没怎么吃到。感觉像是去拍了照就走了,没什么留下也没带走什么,老爸、老姐一直在喊太累,可能是晚上没修改好。最大的好处就是开车技术应该有了一定提升。在姐高反严重,都要打120时,感受最深的就是,身体健康才是本钱,回去还是要坚持运动,保持一项个人喜欢的运动,保持好的生活习惯。晚上急急忙忙的记录了一下,就到这吧,儿子媳妇都睡了,我也像睡了。

近况

其实一直想写一些关于生活方面的文章,但是由于担心隐私泄露以及文笔不好,表达能力不好,就搁置了。 最近突然觉得还是应该记录一些生活,有时有很多想写的,但是组织语言又不好,估计只能写一小段,就不好意思写了。 先分享一首刚刚听见的歌曲,感觉此刻听着还不错。 今晚带儿子游泳,回家已经9点了,感觉时间比较晚了,因为回来还要刷牙喝奶,然后才上传睡觉,但是媳妇还是要给儿子复习一下在培训机构学习的英语课程,不然估计下周又忘了。 最近一直没怎么学习技术方面的东西,一是下班回家后不想动,二是最近想运动一下,原因是感觉有白头发了,加上以前一直晚睡,养成了不好的习惯。今晚媳妇和儿子睡了,才有自己时间听听歌,看看新闻,才静下心来写这篇文章。 也不知道写些什么,就说一下这种关于运动的事情吧。 周一下班骑自行车回家,从公司到小区门口6.05公里,用时23分钟,平均速度15.52km/h. 周二晚上19:30吃了晚饭,带着儿子去游泳馆游到20:30回家,接着玩了会手机23:15睡觉。 周三今晚19:10出发去游泳馆,20:20冲洗骑车回家,到家21:00,接口给儿子冲奶,儿子喝完奶媳妇帮儿子刷牙。21:20媳妇在床上给儿子复习英语,然后每晚必备的睡前故事。大概21:40睡觉,感觉时间还是比较晚了。 明天周四不去了,一是时间有点紧,二是刚上一年级,才开学几天,还是要以学校学习为主。计划明晚让儿子早点睡,老师提倡的是20:30准备睡觉。 附上几张儿子开学的照片记念一下儿子开学当天。 感觉上面写的都是一些口水话,自认为没有任何文笔,记得初中时语文,英文差的不是一点点。今晚就写到这吧,不管有没有人看,就当个人垃圾桶吧。 本来想放几张儿子一年级开学照片,但是水果机的.HEIC格式图片windows居然不能打开,那就先搞个解码器,等可以预览了在放吧。找到一张大运会期间的照片。

2022 年 7 篇记录
JAVA8-Lambda方法推导详细解析

Lambda方法推导详细解析 什么情况下可以进行方法推导? 类的方法(静态方法) 对象的方法 构造方法 自定义函数式接口 范例: package com.example.study.java8.InterfaceFunction; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; /** * 方法推导 * 什么情况下可以进行方法推导: * 1、类的方法(静态方法) * 2、对象的方法 * 3、构造方法 */ public class MethodReferenceUsageDemoOne { public static void main(String[] args) { //方法推导,范例一:类的方法 //原始写法 Consumer<String> consumer = s-> System.out.println(s); genericityConsumer(consumer, "hello"); //1、进化一 genericityConsumer(s->System.out.println(s), "world"); //2、进化二 genericityConsumer(System.out::println, "nice"); System.out.println("==========================="); //方法推导,范例二:类的方法 List<Apple> list = Arrays.asList(new Apple("red", 10) , new Apple("red", 60) , new Apple("blue", 40) , new Apple("black", 30) , new Apple("green", 80) , new Apple("blue", 90) , new Apple("green", 60) , new Apple("green", 50) , new Apple("red", 20)); System.out.println(list); list.sort((oneParameter,twoParameter)->oneParameter.getColor().compareTo(twoParameter.getColor())); System.out.println(list); System.out.println("============排序另一种写法==============="); //排序另一种写法 List<Apple> list2= Arrays.asList(new Apple("red", 10) , new Apple("red", 60) , new Apple("blue", 40) , new Apple("black", 30) , new Apple("green", 80) , new Apple("blue", 90) , new Apple("green", 60) , new Apple("green", 50) , new Apple("red", 20)); System.out.println(list2); list2.sort(Comparator.comparing(Apple::getColor)); //排序说明: //1、匿名内部类 //2、上面第一种写法 //3、上面第二种写法 //代码越来越简单 System.out.println("==========================="); //方法推导,范例三:类的方法 //原始写法 list.stream().forEach(apple->System.out.println(apple)); System.out.println("==========================="); //进化1 list.stream().forEach(System.out::println); System.out.println("==========================="); //方法推导,范例四:类的方法 //原始写法 Integer integer = Integer.parseInt("123"); System.out.println(integer); System.out.println("==========================="); //进化 Function<String, Integer> function = Integer::parseInt; Integer integerTwo = function.apply("321"); System.out.println(integerTwo); System.out.println("==========================="); //方法推导,范例五:对象的方法 BiFunction<String, Integer, Character> stringIntegerCharacterBiFunction = String::charAt; Character character = stringIntegerCharacterBiFunction.apply("Hello", 1); System.out.println(character); System.out.println("==========================="); //方法推导,范例六:构造方法,1个参数 Supplier<String> supplier = String::new; String str = supplier.get(); System.out.println(str.getClass()); System.out.println("==========================="); //方法推导,范例七:构造方法,2个参数 //说明:Apple::new; 就是掉构造方法,自动推导参数和类型 BiFunction<String,Integer,Apple> appleBiFunction = Apple::new; Apple apple = appleBiFunction.apply("red", 50); System.out.println(apple); System.out.println("==========================="); //方法推导,范例八:构造方法-自定义FunctionalInterface接口,多个参数 CustomThreeFunctionalInterface<String,Integer,String,ComplexApple> appleSupplier = ComplexApple::new; ComplexApple complexApple = appleSupplier.apply("苹果",20,"green"); System.out.println(complexApple); } /** * 泛型Consumer * @param consumer * @param t * @param <T> */ public static <T> void genericityConsumer(Consumer<T> consumer, T t){ consumer.accept(t); } } 自定义函数式接口

JAVA8-自定义Collector

自定义Collector 实现Collector接口 package com.example.study.java8.collector; import java.util.*; import java.util.function.BiConsumer; import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; /** * 自定义Collector * T:元素类型 * List<T>:要创建的类型 * List<T>:最后要返回的类型 */ public class ToListCollector<T> implements Collector<T, List<T>, List<T>> { private void log(final String log) { System.out.println(Thread.currentThread().getName()+"-"+log); } //一定时可变的supplier,不是固定值 @Override public Supplier<List<T>> supplier() { log("supplier"); return ArrayList::new; } //要进行的操作 @Override public BiConsumer<List<T>, T> accumulator() { log("accumulator"); return List::add; } //将结果整合 @Override public BinaryOperator<List<T>> combiner() { log("combiner"); return (list1,list2)->{ list1.addAll(list2); return list1; }; } //返回结果 @Override public Function<List<T>, List<T>> finisher() { log("finisher"); // return t->t; return Function.identity(); } //特征值:CONCURRENT-并行,UNORDERED-排序,IDENTITY_FINISH-入参就出参 @Override public Set<Characteristics> characteristics() { log("characteristics"); return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT)); } } 使用测试 package com.example.study.java8.collector; import java.util.Arrays; import java.util.List; import java.util.stream.Collector; /** * 使用自定义Collector测试 */ public class CustomToListCollector { public static void main(String[] args) { String[] arrs = new String[]{"chinese","japanse","english","freach","Korean"}; //测试 //对应接口中的: //Supplier<A> supplier(), BiConsumer<A, T> accumulator(),Function<A, R> finisher(); Collector<String, List<String>, List<String>> collector = new ToListCollector<String>(); List<String> list = Arrays.stream(arrs).filter(s -> s.length() > 6).collect(collector); System.out.println(list); System.out.println("================================"); //parallelStream(),并行使用测试 List<String> parallelList = Arrays.asList(new String[]{"chinese", "japanse", "english", "freach", "Korean"}) .parallelStream() .filter(s -> s.length() > 6) .collect(collector); System.out.println(parallelList); } } 输出结果 main-supplier main-accumulator main-combiner main-characteristics main-characteristics [chinese, japanse, english] ================================ main-characteristics main-characteristics main-supplier main-accumulator main-combiner main-characteristics main-characteristics [chinese, japanse, english] 由于数据量少,并没有出现并行。

JAVA8-Stream基本使用及特点

Stream基本使用及特点 Stream说明 Stream操作分为:2组,//注意,流式管道操作,只能操作一次,否则报错。 1、可连续操作: filter, map, and limit can be connected together to form a pipeline. 2、操作中断 collect causes the pipeline to be executed and closes it. 使用范例: package com.example.study.java8.streams; import java.util.*; import java.util.stream.Stream; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.toList; /** * Stream使用 * Stream操作分为:2组,//注意,流式管道操作,只能操作一次,否则报错 * You can see two groups of operations: * 1、可连续操作 *  filter, map, and limit can be connected together to form a pipeline. * 2、操作中断 *  collect causes the pipeline to be executed and closes it. */ public class SimpleStream { public static void main(String[] args) { List<Dish> menu = Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT), new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER), new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH)); Stream<Dish> stream = menu.stream(); stream.forEach(System.out::println); //注意,流式管道操作,只能操作一次,否则报错 //stream has already been operated upon or closed // stream.forEach(System.out::println); System.out.println("===================================="); //原始调用 List<String> namesByCollections = getDishNamesByCollections(menu); namesByCollections.stream().forEach(System.out::println); System.out.println("===================================="); //lambda stream调用 List<String> lambdaStreamNames = lambdaStream(menu); lambdaStreamNames.stream().forEach(System.out::println); } //原始写法 public static List<String> getDishNamesByCollections(List<Dish> menu) { List<Dish> caloriesDis = new ArrayList<>(); for (Dish dish : menu) { if (dish.getCalories() < 400) { caloriesDis.add(dish); } } Collections.sort(caloriesDis, (dish1, dish2) -> { return Integer.compare(dish1.getCalories(), dish2.getCalories()); }); List<String> names = new ArrayList<>(); for(Dish dish : caloriesDis){ names.add(dish.getName()); } return names; } //stream lambda处理 public static List<String> lambdaStream(List<Dish> menu){ return menu.stream().filter(dis -> dis.getCalories() < 400) .sorted(comparing(Dish::getCalories)) .map(Dish::getName).collect(toList()); } } 主要掌握Stream接口中常用方法。 Stream源码:

JAVA8-CompletableFuture基本用法

针对<实现一个异步基于事件回调的Future程序> 用CompletableFuture进行改进。 代码示例: package com.example.study.java8.completableFutures; import java.util.Optional; import java.util.Random; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; /** * CompletableFuture 基本用法 */ public class CompletableFutureInAction1 { private final static Random RANDOM = new Random(System.currentTimeMillis()); public static void main(String[] args) throws ExecutionException, InterruptedException { //实际开发中,一般不直接new,而是使用工厂创建 // CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(); CompletableFuture<Double> completableFuture = new CompletableFuture<>(); new Thread(()->{ double value = get(); completableFuture.complete(value); }).start(); //不会阻塞程序执行 System.out.println("===========no====block====.."); //1、后面获取程序执行结果 // Optional.ofNullable(completableFuture.get()).ifPresent(System.out::println); //2、执行完后,通过回调自动返回结果 completableFuture.whenComplete((v,t)->{ Optional.ofNullable(v).ifPresent(System.out::println); Optional.ofNullable(t).ifPresent(x->x.printStackTrace()); }); } private static double get(){ try { Thread.sleep(RANDOM.nextInt(10000)); } catch (InterruptedException e) { e.printStackTrace(); } return RANDOM.nextDouble(); } } 输出结果:

JAVA8-completableFuture 异步编排

通过线程池性能稳定,也可以获取执行结果,并捕获异常。但是,在业务复杂情况下,一个异步调用可能会依赖于另一个异步调用的执行结果。因此我们可以使用completableFuture 异步编排方案。 比如:一个业务场景,需要同时获取多个数据,如果同步线程挨个执行,则需要时间为所有线程执行时间的总和。 如果我们使用异步线程执行,所需时间则为耗时最长那个异步线程的执行时间。 如果多个异常线程之间还存在依赖关系,比如线程3需要线程1的执行结果,线程6依赖线程3、线程2,那这个问题怎么解决呢。那就可以使用completableFuture 异步编排方案实现。 注意:completableFuture 是jdk1.8之后添加的一个功能。 CompletableFuture接口: public class CompletableFuture<T> implements Future<T>, CompletionStage<T> { public interface Future<V> { 以前用到的FutureTask就是用到的Future可以得到返回结果 public class FutureTask<V> implements RunnableFuture<V> { public interface RunnableFuture<V> extends Runnable, Future<V> { /** * Sets this Future to the result of its computation * unless it has been cancelled. */ void run(); } Future可以得到返回结果

JAVA8-CompletableFuture LocalDate, LocalTime, Instant, Duration, Period

JAVA8-Date and Time API: LocalDate, LocalTime, Instant, Duration, Period 旧API问题 1、代码不清晰,一下看不出要转换的日期。 **2、Date日期中还包含了时间 ** 3、多线程情况下会报错。 代码重现: package com.example.study.java8.datetime; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateTest { public static void main(String[] args) throws ParseException { //122格林威治时间 11月 23日期 //问题:不能清晰表示日期 Date date = new Date(122, 11, 23); System.out.println(date); //多线程情况下会出现一些问题: //30个线程,每个线程下循环100次,执行时间格式转换。 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); for (int i = 0; i < 30; i++) { new Thread(() -> { for (int j = 0; j < 100; j++) { Date parseDate = null; try { parseDate = sdf.parse("20221123"); } catch (ParseException e) { e.printStackTrace(); } System.out.println(parseDate); } }).start(); } } } 输出结果:

JAVA8-CompletableFuturerunAfterBoth、applyToEither、acceptEither 、runAfterEither 、allOf、anyOf

AVA8-CompletableFuture常用API:runAfterBoth、applyToEither、acceptEither 、runAfterEither 、allOf、anyOf 1、runAfterBoth :2个CompletableFuture都执行完后,再执行其它操作 package com.example.study.java8.completableFutures.api; import java.util.concurrent.CompletableFuture; public class CompletableFutureAction2 { public static void main(String[] args) throws InterruptedException { //API-- 1、runAfterBoth: 2个都执行完后,再执行其它操作 CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "this is runing 1......"); return 1; }) .runAfterBoth(CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "this is runing 2......"); return 2; }), () -> System.out.println("done")); //为了防止主线程结束后,守护线程被关闭,模拟修改10000毫秒 Thread.sleep(10000); } } 输出结果: