gpt4 book ai didi

python - 面向对象编程Python : Where to instantiate Cassandra and elasticsearch cluster?

转载 作者:行者123 更新时间:2023-12-01 03:18:39 25 4
gpt4 key购买 nike

我有一个与elasticsearch和cassandra交互很多的对象。但我不知道在哪里实例化我的 Cassandra 和 elasticsearch session 。我应该把它放在我的“代码”中,并将 session 传递到我的函数的参数中,如下所示:

cassandra_cluster = Cluster()
session = cassandra_cluster.connect()
es = Elasticsearch()

class Article:

document_type = "cnn_article"

def __init__(self):
self.author = ""
self.url = ""
...

@classmethod
def from_crawl(cls, url):
obj = cls()
# Launch a crawler and fill the fields and return the object

@classmethod
def from_elasticseacrh(cls, elastic_search_document):
obj = cls()
# Read the response from elasticsearch and return the object

def save_to_cassandra(self):
# Save an object into cassandra
session.execute(.....)

def save_to_elasticsearch(self, index_name, es):
# Save an object into elasticsearch
es.index(index=index_name, ...)

...

article = Article.from_crawl("http://cnn.com/article/blabla")
article.save_to_cassandra(session)
article.save_to_elasticsearch("cnn", es)

或者我应该将 cassandra 和 elasticsearch session 的实例化作为实例变量,如下所示:

class Article:

cassandra_cluster = Cluster()
session = cassandra_cluster.connect()
es = Elasticsearch()
document_type = "cnn_article"

def __init__(self):
self.author = ""
self.url = ""
...

@classmethod
def from_crawl(cls, url):
obj = cls()
# Launch a crawler and fill the fields and return the object

@classmethod
def from_elasticseacrh(cls, elastic_search_document):
obj = cls()
# Read the response from elasticsearch and return the object

def save_to_cassandra(self):
# Save an object into cassandra
session.execute(.....)

def save_to_elasticsearch(self):
# Save an object into elasticsearch
es.index(....)

...

article = Article.from_crawl("http://cnn.com/article/blabla")
article.save_to_cassandra()
article.save_to_elasticsearch()

最佳答案

基于他们的文档和这里的一些示例:http://www.datastax.com/dev/blog/datastax-python-driver-multiprocessing-example-for-improved-bulk-data-throughput

我会采用你的第二种方法。他们提到 session 只是用于关闭连接的上下文管理器,并且他们的查询管理器将它们显示为类属性。

我认为两者都可以,但如果您想对其进行多处理,则使用后一种方法可能会稍微容易一些。

关于python - 面向对象编程Python : Where to instantiate Cassandra and elasticsearch cluster?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42207677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com