镜片MCP培训工作坊实践实验室
实验室大纲
实验1:熟悉Lenses UI
实验2:配置MCP服务器
实验3:用法学硕士探索数据
实验4:用LLM编写SQL查询
实验5:用你的法学硕士构建一个毒丸过滤器
实验1:熟悉Lenses UI
第一步:登录并探索。您的讲师将为您分配一个包含登录信息的Lenses URL。转到该URL并登录。您将看到一个欢迎屏幕,您可以暂时关闭,但如果您是Lenses的新手,这是获取更多信息的绝佳资源。
您看到的第一个屏幕是环境屏幕。这是所有连接的Kafka环境的列表。出于培训目的,我们将使用一个“本地”开源Kafka标签的Dev和一个在云中使用Strimzi的测试环境。
步骤2:如果您单击右侧导航栏上的下一个图标,它将带您进入主题视图。
这是主题视图。它列出了所有Kafka环境中可用的所有主题。请注意,我们以管理员身份登录,因此可以查看所有主题。在“现实世界”中,这里出现的主题将取决于Lenses RBAC。
Topics视图可以通过主题名称以及每个主题模式中的键名进行完全搜索。搜索关键字“经度”-这将显示所有包含地理字段的主题。单击“在模式中搜索”复选框,也可以显示每个主题的所有关键字名称匹配。
步骤3:让我们将鼠标悬停在列表中的sea_vessel_position_reports主题上,然后单击鼠标悬停时出现的右侧的SQL按钮,从而更深入地了解该主题。
这将我们带到Lenses最常用的方面——SQL Studio。它旨在让开发人员与Lenses进行交互,就像他们与VS code等代码编辑器进行交互一样。默认情况下,当您从主题视图中单击时,它将在该主题中运行基本搜索,显示该主题中跨分区的最新事件。
您还可以在搜索主题的同时并排查看模式、主题配置和连接的消费者。稍后,我们将在课程中使用Lenses UI的其他方面,在我们重新开始讲座之前,请自行探索一下。
实验2:配置MCP服务器
步骤1:将您的Lenses MCP服务器连接到LLM。
您的讲师将为您正在使用的LLM提供特定的连接说明。无论您选择哪种LLM客户端,一般过程都是相同的:将MCP服务器URL提供给LLM的MCP连接器设置。
例如,在Claude.ai中,您可以转到“设置”>“连接器”>“添加自定义连接器”并填写URL。其他LLM客户端将具有类似的配置屏幕。
连接后,您现在可以使用LLM查询并与Lenses及其底层数据流通信。
实验3:用法学硕士探索数据
在本实验室中,您将使用LLM和Lenses MCP连接来执行常见的开发人员任务:理解不熟悉的数据,在构建消费者之前验证模式,以及生成测试数据需求。
实验3A:模式发现和文档
脚本: 您刚刚加入了一个处理航空公司数据流的团队。在做出贡献之前,您需要了解可用的数据以及主题之间的关系。
第一步: 让你的法学硕士调查你所在环境中可用的航空公司相关主题。
Using your Lenses MCP connection, list all topics in the staging environment that appear to be related to airline or flight data. For each topic, tell me:
- The topic name
- Number of partitions
- Whether it has a schema registered第二步: 一旦你有了一个主题列表,选择2-3个看起来相关的主题,让你的法学硕士详细检查它们的模式。
Examine the schemas for [topic1] and [topic2] in staging.
Give me a field-by-field breakdown including data types.
Do these topics share any common fields that could be used to join or correlate events?步骤3: 让你的法学硕士生成一个可以与你的团队分享的快速数据字典。
Based on what you've learned about these airline topics, generate a markdown data dictionary I could add to our team's documentation. Include topic names, descriptions of what each topic likely contains, key fields, and any relationships between topics.这正是开发人员在实践中使用MCP的方式——快速探索不熟悉的数据环境,而无需手动点击UI中的数十个主题。
______________________________________________________________________
实验3B:新消费者的数据验证
脚本: 您被指派构建一个使用金融交易数据的新微服务。在编写任何代码之前,您需要了解实际的数据格式、边缘情况和潜在的陷阱。
第一步: 从采样真实消息开始,了解您将使用的内容。
I'm building a consumer for the credit-card-transactions topic in the dev environment.
Sample 100 recent messages and give me a summary:
- What fields are present in every message?
- Are there any null or missing fields I'll need to handle defensively?
- What are the data types for each field?第二步: 了解您需要处理的值范围和分布。
Looking at those same messages from credit-card-transactions:
- What's the range of transaction amounts?
- What are all the different values you see for status or transaction_type fields?
- Are there any fields with unexpected or potentially problematic values?步骤3: 为您的消费者代码生成测试夹具。
Based on your analysis of credit-card-transactions, give me 3 representative JSON messages I can use as test fixtures:
1. A "happy path" typical transaction
2. An edge case with minimum/maximum values
3. A message with any optional or nullable fields set to null此工作流可帮助您从第一天开始编写健壮的消费者代码,而不是在生产中发现边缘情况。
______________________________________________________________________
实验室3C:建筑测试数据要求
脚本: 您的团队需要使用真实数据的集成测试。您将分析真实流以构建准确的测试数据规范,而不是发明可能与生产模式不匹配的虚假数据。
第一步: 选择一个主题,让你的法学硕士分析数据中的实际模式。
Analyze the sea_vessel_position_reports topic in dev. Sample 50 messages and tell me:
- What are the realistic ranges for latitude and longitude?
- What vessel_type values appear and how frequently?
- What's the typical format and length of vessel identifiers?
- Are there any fields that follow specific patterns (like timestamps or codes)?第二步: 让你的法学硕士确定你的测试应该涵盖的边缘案例。
Based on your analysis of sea_vessel_position_reports, what edge cases should our integration tests cover?
Look for:
- Boundary values (min/max coordinates, speeds, etc.)
- Rare but valid values in enum-like fields
- Any data quality issues we should test our error handling against步骤3: 让你的LLM生成一个测试数据规范文档。
Generate a test data specification for sea_vessel_position_reports that our team can use to build realistic mock data. Format it as a markdown document with:
- Field names and types
- Valid value ranges with examples
- Required vs optional fields
- Edge cases to include in test suites
- Sample valid and invalid messages这种方法确保您的测试数据反映实际的生产模式,使您的集成测试更有价值。
实验4:用LLM编写SQL查询
在之前的实验室中,我们让LLM自由地探索数据。现在,让我们更具体地说,让它为我们编写可以在Lenses SQL Studio中运行的有针对性的SQL查询。
将LLM与Lenses配对的最强大的方面之一是,它可以通过MCP连接检查主题模式,了解哪些字段可用,然后为您编写准确的SQL——无需自己记住模式。
第一步:让我们从地理查询开始。这 nyc_yellow_taxi_trip_data 主题包含纽约市出租车行程的接送坐标。让你的法学硕士写一个查询,找到特定行政区的旅行。以下是一个示例提示:
Using your Lenses MCP connection, look at the schema for the nyc_yellow_taxi_trip_data topic in the dev environment. Then write me a Lenses SQL Studio query that finds all taxi trips that were picked up in Brooklyn. Use the pickup latitude and longitude coordinates and a bounding box for Brooklyn's approximate boundaries. 你的LLM应该检查模式,确定 pickup_latitude 和 pickup_longitude 字段,并编写类似以下内容:
SELECT *
FROM nyc_yellow_taxi_trip_data
WHERE pickup_latitude > 40.57
AND pickup_latitude -74.04
AND pickup_longitude < -73.85复制LLM提供的SQL,并在Lenses SQL Studio中运行它以验证结果。尝试更换行政区——询问曼哈顿、皇后区或肯尼迪机场地区(提示: RateCodeID = 2 是JFK统一费率)。
步骤2:现在让我们查询财务数据。我们的数据生成器生成了几个财务主题,包括 credit-card-transactions 和 paypal-transactions。让你的法学硕士写一些能展现有趣模式的查询。以下是一些示例提示:
Look at the schema for credit-card-transactions in the dev environment. Write me a Lenses SQL Studio query that finds all transactions over $500 at gas stations. Those seem suspicious to me.Write me a SQL Studio query for the paypal-transactions topic in dev that finds transactions where the amount is greater than 1000 and the city is not in the United States.第三步:让我们试试海洋数据。这 sea_vessel_position_reports 本主题包含斯堪的纳维亚水域船舶的AIS(自动识别系统)位置数据。请您的法学硕士帮助您找到特定的船舶活动:
Look at the sea_vessel_position_reports topic schema in dev. Write me a Lenses SQL Studio query that finds all vessels that appear to be stationary — where the Speed field is 0 or very close to 0. 第四步:试着写自己的提示。以下是一些让你思考的想法:
- 查找小费比例异常高的出租车行程(小费金额与车费金额)
- 查询
telecom_italia_data寻找高流量蜂窝塔网格广场的主题 - 查找特定商户类别中的信用卡交易
- 查找发送特定AIS消息类型的海船
这里的关键要点是,你的LLM可以阅读模式,理解字段语义,并编写有效的Lenses SQL——使你不必在几十个数据源中记忆主题结构。当你的查询没有返回你期望的结果时,将结果粘贴回你的LLM,并要求它优化SQL。
实验5:构建毒丸过滤器
步骤1:使用Lenses SQL Studio,让我们在nyc_well_taxi_trip_data中搜索不良记录或毒丸。具体来说,我们将寻找负票价,因为即使取消行程,最坏的情况下票价也为零。
打开Lenses UI SQL Studio,在您的开发环境中找到nyc_taxi_trip_data。运行以下SQL搜索,查看是否存在这些类型的事件:
SELECT *
FROM nyc_yellow_taxi_trip_data
WHERE fare_amount < 0步骤2:现在我们知道这些事件存在并且相当常见,让我们的LLM为我们编写一些SQL Processor语句,为我们进行过滤。
以下是一个示例提示:
There are events with errors (poison pills) in the nyc_yellow_taxi_trip_data topic on our dev kafka cluster. events with fare_amount < 0
Write a lenses sql processor to filter out these poison pills into their own separate topic. this sql processor should create two new topics. One with just the poison pills called nyc_taxi_dlq and another called nyc_taxi_filtered.
Be careful. SQL Processors use a different SQL syntax than SQL Studio in lenses. It's stream processing so keep that in mind when you double check the SQL you're going to write.请注意,最后一句话是因为LLM可能会将SQL Studio语法与SQL Processor语法混淆——两者基本相似,但有一些关键区别。
希望你的法学硕士能给你一个类似的结果。如果它看起来偏离了基础,请将其发送回去,并提醒SQL处理器语法与SQL Studio语法不同。
步骤3:一旦你的LLM为你的处理器提出了正确的SQL,深入到你的Dev集群,然后转到App部分。
在应用程序屏幕中,单击“创建SQL处理器”。给它起一个名字,比如“出租车旅行毒丸过滤器”,然后从LLM复制并粘贴SQL。
注意!有时你的法学硕士会忘记包括 SET defaults.topic.autocreate=true; 在SQL语句中,如有必要,请将其保留在预填框中。
一旦你复制了SQL,并且没有列出错误。点击“创建处理器”按钮。
创建处理器后,单击启动处理器按钮将其启动。一旦它启动并运行,请转到拓扑视图查看它在那里的显示。
然后转到主题查看器,查看您的两个新主题。
