1. 模块概述

KafkaModule 是一个用于与 Apache Kafka 集群进行交互的 Python 模块。该模块支持带认证和无认证的 Kafka 集群操作,提供了丰富的功能,包括创建和删除 Topic、发送和消费消息、获取集群和 Topic 的详细信息等
 
 
def test_kafkaModule():
"""测试Kafka模块功能"""
import json
import time

# 初始化配置
config = {
"bootstrap_servers": "***",
"security_protocol": "SASL_PLAINTEXT",
"sasl_mechanism": "PLAIN",
"sasl_plain_username": "***",
"sasl_plain_password": "***",
"timeout": 30
}

kafka_module = KafkaModule(config)

try:
# 1. 测试连接
print("\n" + "=" * 60)
print("1. 测试连接")
print("=" * 60)
result = kafka_module.test_connection()
print("测试连接结果:", json.dumps(result, indent=2, ensure_ascii=False))

# 2. 获取集群信息
print("\n" + "=" * 60)
print("2. 获取集群信息")
print("=" * 60)
cluster_info = kafka_module.get_cluster_info()
print("集群信息:", json.dumps(cluster_info, indent=2, ensure_ascii=False))

# 3. 列出所有Topic
print("\n" + "=" * 60)
print("3. 列出所有Topic")
print("=" * 60)
topics = kafka_module.list_topics()
if topics.get('success'):
print(f"找到 {topics.get('count', 0)} 个Topic")
print("前10个Topic:", topics.get('topics', [])[:10])
else:
print("获取Topic列表失败:", topics.get('error'))

# 4. 创建测试Topic
print("\n" + "=" * 60)
print("4. 创建测试Topic")
print("=" * 60)
test_topic = f"test-topic-{int(time.time())}"
create_result = kafka_module.create_topic(
topic=test_topic,
partitions=3,
replication_factor=1
)
print("创建Topic结果:", json.dumps(create_result, indent=2, ensure_ascii=False))

if create_result.get('success'):
# 等待Topic创建完成(Kafka需要时间同步)
print("\n等待5秒让Topic创建完成...")
time.sleep(5)

# 5. 获取Topic详情
print("\n" + "=" * 60)
print("5. 获取Topic详情")
print("=" * 60)
topic_detail = kafka_module.get_topic_details(
topic=test_topic
)
print("Topic详情:", json.dumps(topic_detail, indent=2, ensure_ascii=False))

# 6. 发送单条消息
print("\n" + "=" * 60)
print("6. 发送单条消息")
print("=" * 60)
send_result = kafka_module.send_message(
topic=test_topic,
key="test-key-1",
value={"message": "Hello Kafka", "timestamp": time.time()},
headers={"source": "test", "type": "json"}
)
print("发送结果:", json.dumps(send_result, indent=2, ensure_ascii=False))

# 7. 批量发送消息
print("\n" + "=" * 60)
print("7. 批量发送消息")
print("=" * 60)
messages = []
for i in range(5):
messages.append({
"key": f"batch-key-{i}",
"value": f"Batch message {i}",
"headers": {"batch_id": "batch-001"}
})

batch_result = kafka_module.send_batch_messages(
topic=test_topic,
messages=messages
)
print("批量发送结果:", json.dumps(batch_result, indent=2, ensure_ascii=False))

# 等待消息写入完成
time.sleep(2)

# 8. 消费消息
print("\n" + "=" * 60)
print("8. 消费消息")
print("=" * 60)
consume_result = kafka_module.consume_messages(
topic=test_topic,
group_id=f"test-group-{int(time.time())}",
num_messages=3,
timeout=10,
offset="beginning"
)
print("消费结果:", json.dumps(consume_result, indent=2, ensure_ascii=False))

# 9. 验证消息
print("\n" + "=" * 60)
print("9. 验证消息")
print("=" * 60)
verify_result = kafka_module.verify_message(
topic=test_topic,
group_id=f"verify-group-{int(time.time())}",
expected_value="Batch message 2",
expected_key="batch-key-2"
)
print("验证结果:", json.dumps(verify_result, indent=2, ensure_ascii=False))

# 10. 列出消费者组
print("\n" + "=" * 60)
print("10. 列出消费者组")
print("=" * 60)
groups = kafka_module.list_consumer_groups() # 不需要传认证信息
print("消费者组:", json.dumps(groups, indent=2, ensure_ascii=False))

# 11. 删除测试Topic
print("\n" + "=" * 60)
print("11. 删除测试Topic")
print("=" * 60)
delete_result = kafka_module.delete_topic(
topic=test_topic
)
print("删除结果:", json.dumps(delete_result, indent=2, ensure_ascii=False))

except Exception as e:
print(f"测试过程中出错: {e}")
import traceback
traceback.print_exc()
finally:
# 关闭连接
kafka_module.close()
print("\n" + "=" * 60)
print("测试完成,连接已关闭")
print("=" * 60)
 
 
 
 
def test_call_business():
"""测试通话业务层"""

kafka_config = {
"bootstrap_servers": "1****",
"security_protocol": "SASL_PLAINTEXT",
"sasl_mechanism": "PLAIN",
"sasl_plain_username": "****",
"sasl_plain_password": "****"
}

call_business = CallBusiness(kafka_config)

try:
# 获取通话数据
result = call_business.get_call_data_by_callid(
topic="MSXF_NODE_REPORT_EVENT_TOPIC",
callid="4531e0c8-f146-4ae7-82c0-84375f1c38c0",
max_messages=1000,
saveAsVariable="call_data"
)

print(f"找到 {result.get('message_count')} 条通话消息")
print(
f"通话时间: {result.get('call_data', {}).get('start_time')} - {result.get('call_data', {}).get('end_time')}")

# 2. 测试提取字段功能(复用get_call_data_by_callid的结果)
print("\n" + "=" * 60)
print("测试提取字段功能")
print("=" * 60)

# 提取endType=HungUp的消息的nodeId
extract_result = call_business.extract_field_from_message(
topic="MSXF_NODE_REPORT_EVENT_TOPIC",
callid="4531e0c8-f146-4ae7-82c0-84375f1c38c0",
condition_field="endType",
condition_value="HungUp",
target_field="nodeId"
)

if extract_result.get('success'):
print(f"endType=HungUp的消息,nodeId = {extract_result.get('field_value')}")
else:
print(f"未找到: {extract_result.get('error')}")

# 如果没有条件字段,直接返回第一条消息的目标字段,即提取第一条消息的nodeId
extract_result2 = call_business.extract_field_from_message(
topic="MSXF_NODE_REPORT_EVENT_TOPIC",
callid="4531e0c8-f146-4ae7-82c0-84375f1c38c0",
target_field="nodeId"
)

if extract_result2.get('success'):
print(f"第一条消息的nodeId = {extract_result2.get('field_value')}")

finally:
call_business.close()
posted on 2026-03-09 10:59  小海海宁宁  阅读(5)  评论(0)    收藏  举报