如何在 Python 中为现有 Cassandra 表创建 Table 对象?

发布于 2025-01-12 21:05:18 字数 180 浏览 5 评论 0原文

我在键空间中有一些表,我想在 python DataStax 驱动程序中像 ORM 一样进行查询,

我有很多具有巨大架构的表。无需编写 Table 类即可创建表对象的任何其他方法,

例如: 如果我有一个名为 test_table 的表 那么
table_object =表(表名,元数据,自动加载= True)

I have some tables in a keyspace and I want query like ORM in python DataStax driver

I have lots of tables with huge schema. any other method to create a table object without writing the Table class

eg:
if I have a table called test_table
then
table_object=Table(table_name,metadata,autoload=True)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⒈起吃苦の倖褔 2025-01-19 21:05:19

您没有提供太多信息,所以我不太确定您想要什么。无论如何,您可能对 Python 驱动程序中的模型感兴趣。

以下是类 Person: 的文档示例,

from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model

 class Person(Model):
     id = columns.UUID(primary_key=True)
     first_name  = columns.Text()
     last_name = columns.Text()

该类映射到此 CQL 表:

CREATE TABLE cqlengine.person (
    id uuid,
    first_name text,
    last_name text,
    PRIMARY KEY (id)
)

然后,您可以使用 Person 对象在表上执行 CRUD 操作。

有关详细信息和更多示例,请参阅具有 Cassandra Python 驱动程序的模型。干杯!

You haven't provided a lot of information so I'm not really sure what you're after. In any case, you might be interested in Models in the Python driver.

Here's an example from the docs for a class Person:

from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model

 class Person(Model):
     id = columns.UUID(primary_key=True)
     first_name  = columns.Text()
     last_name = columns.Text()

which maps to this CQL table:

CREATE TABLE cqlengine.person (
    id uuid,
    first_name text,
    last_name text,
    PRIMARY KEY (id)
)

You can then perform CRUD operations on the table using the Person object.

For details and more examples, see Models with the Cassandra Python driver. Cheers!

黑寡妇 2025-01-19 21:05:19

我使用这个函数在 pyspark 中获取 Cassandra 表对象

def get_table(keys_space_name, table_name):
    table_df = spark.read\
        .format("org.apache.spark.sql.cassandra")\
        .option("spark.cassandra.connection.host","127.0.0.1")\
        .option("spark.cassandra.connection.port", "9042")\
        .options(table='test', keyspace='test_keyspace')\
        .load()
    return table_df

I used this function for getting Cassandra table object in pyspark

def get_table(keys_space_name, table_name):
    table_df = spark.read\
        .format("org.apache.spark.sql.cassandra")\
        .option("spark.cassandra.connection.host","127.0.0.1")\
        .option("spark.cassandra.connection.port", "9042")\
        .options(table='test', keyspace='test_keyspace')\
        .load()
    return table_df
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文