CData Python Connector for Apache Impala

Build 21.0.7930

Reflecting Metadata

SQLAlchemy can act as an ORM, which allows you to treat records of a database table as instantiable records. To leverage this functionality, you will need to reflect the underlying metadata in one of the following ways.

Model Data Using a Mapping Class

Declare a mapping class for the table you wish to model in the ORM. This is done using "sqlalchemy.ext.declarative.declarative_base". A known table in the data model is modeled either partially or completely as in the below example:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class [CData].[Default].Customers(Base):
	__tablename__ = "[CData].[Default].Customers"
	_id = Column(String, primary_key=True)
	City = Column(String)
	CompanyName = Column(String)

Automatically Reflect Metadata

Rather than mapping tables manually, SQLAlchemy can discover the metadata for one or more tables automatically. This can be accomplished across the entire data model with automap_base:

from sqlalchemy import MetaData
from sqlalchemy.ext.automap import automap_base
meta = Metadata()
base = automap_base(metadata=meta)
base.prepare()
[CData].[Default].Customers = base.classes.[CData].[Default].Customers

Alternatevely, a single table can be reflected with an inspector. When reflecting this way, providing a list of specific columns to map is optional:

from sqlalchemy import MetaData, Table
from sqlalchemy.engine.reflection import inspector
meta = Metadata()
insp = Inspector.from_engine(engine)
[CData].[Default].Customers_table = Table("[CData].[Default].Customers", meta)
insp.reflecttable([CData].[Default].Customers_table, ["_id","City","CompanyName"])

Copyright (c) 2021 CData Software, Inc. - All rights reserved.
Build 21.0.7930