使用python-cassandra遇到的一个问题

在测试一个从Cassandra集群获取数据并生成配置文件的Python程序时,遇到运行时错误。程序在运维环境报错,原因是程序在部分连接建立时就开始执行,导致在关闭时仍有未完成的连接尝试。通过对`cassandra.cluster`模块的深入研究,发现`Cluster`构造方法中的`wait_for_all_pools_to_connect`参数能解决此问题,将其设置为True,确保所有连接建立完成后再执行程序,问题得到解决。
摘要由CSDN通过智能技术生成

今天和运维在一起测试我写的一个程序时,在日志中发现了一个error,然而我写完程序后自测时是OK的,想起测试之前我拍着胸脯打包票说“我的程序肯定没问题”时,红扑扑的小脸没地儿搁了。

有问题那就解决!

先说下程序背景,我的程序的目的是从Cassandra集群中取数据,然后根据取得的数据生成一个配置文件供别的程序使用。程序结构还蛮简单的,用Python来写。

Cassandra集群有6台,这里就用编号1-6表示。连接的代码如下:


# 设置登陆Cassandra的用户、密码
auth = PlainTextAuthProvider(_config['username'], _config['password'])

cluster = Cluster(_config['addresses'], auth_provider = auth)

session = cluster.connect(_config['keyspace'])

具体使用cql语句的时候就像session.execute(cql)这样子使用。

好了,以上代码在自己的测试环境中跑毫无问题。

but,运维跑得时候就有问题了,日志如下:(这里只贴了关键日志)

2016-12-13 15:26:42,725 - cassandra.pool - DEBUG - 76 - create_file_for_mbw : Host 1 is now marked up
2016-12-13 15:26:42,725 - cassandra.pool - DEBUG - 76 - create_file_for_mbw : Host 2 is now marked up
2016-12-13 15:26:42,725 - cassandra.pool - DEBUG - 76 - create_file_for_mbw : Host 3 is now marked up
2016-12-13 15:26:42,725 - cassandra.pool - DEBUG - 76 - create_file_for_mbw : Host 4 is now marked up
2016-12-13 15:26:42,726 - cassandra.pool - DEBUG - 76 - create_file_for_mbw : Host 5 is now marked up
2016-12-13 15:26:42,725 - cassandra.pool - DEBUG - 76 - create_file_for_mbw : Host 6 is now marked up
2016-12-13 15:26:42,970 - cassandra.cluster - DEBUG - 76 - create_file_for_mbw : [control connection] Finished fetching ring info
######  到这里是收集完成Cassandra集群信息

2016-12-13 15:26:42,970 - cassandra.cluster - DEBUG - 76 - create_file_for_mbw : [control connection] Rebuilding token map due to topology changes
2016-12-13 15:26:43,077 - cassandra.cluster - DEBUG - 76 - create_file_
Spring Boot整合GeoMesa Cassandra是为了利用GeoMesa库的强大地理空间处理能力,与Cassandra NoSQL数据库结合,以便在Spring应用中方便地执行地理数据的CRUD操作。GeoMesa是一个高度可扩展的开源框架,它抽象了底层的数据存储,使得开发者能够更专注于业务逻辑。 以下是整合步骤: 1. 添加依赖: 在你的`pom.xml`文件中添加GeoMesa和Spring Data Cassandra的依赖: ```xml <dependency> <groupId>org.locationtech.geomesa</groupId> <artifactId>geomesa-spring-boot-starter-cassandra</artifactId> <version>XX.YY.ZZ</version> <!-- 用最新版本替换 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-cassandra</artifactId> </dependency> ``` 记得替换版本号为最新的稳定版。 2. 配置GeoMesa Cassandra:在`application.properties`或`application.yml`中配置Cassandra连接信息,包括主机、端口用户名密码等。 3. 创建数据源:在Spring Boot应用中,使用Spring Data Cassandra的Repository接口来定义地理空间数据源。例如,假设你有一个`Feature`实体映射到GeoMesa中的一个表,你可以创建一个类似这样的Repository接口: ```java public interface FeatureRepository extends GeoMesaFeatureDao<Feature, UUID> { // CRUD operations List<Feature> findByBbox(BoundingBox bbox); Feature findById(UUID id); void save(Feature feature); void deleteById(UUID id); } ``` 4. 使用Repository:注入`FeatureRepository`并进行CRUD操作: ```java @Autowired private FeatureRepository featureRepository; public void createFeature(Feature feature) { featureRepository.save(feature); } public Feature getFeatureById(UUID id) { return featureRepository.findById(id); } public void updateFeature(Feature updatedFeature) { featureRepository.save(updatedFeature); } public void deleteFeature(UUID id) { featureRepository.deleteById(id); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值