def test_create_table(self): base = get_declarative_base() class TestTable(base): x = Column(types.Int32, primary_key=True) y = Column(types.String) __table_args__ = ( engines.Memory(), ) self.assertEqual( self.compile(CreateTable(TestTable.__table__)), 'CREATE TABLE test_table (x Int32, y String) ENGINE = Memory' )
def test_create_drop_mat_view(self): Base = get_declarative_base(self.metadata()) class Statistics(Base): date = Column(types.Date, primary_key=True) sign = Column(types.Int8, nullable=False) grouping = Column(types.Int32, nullable=False) metric1 = Column(types.Int32, nullable=False) __table_args__ = (engines.CollapsingMergeTree( sign, partition_by=func.toYYYYMM(date), order_by=(date, grouping)), ) # Define storage for Materialized View class GroupedStatistics(Base): date = Column(types.Date, primary_key=True) metric1 = Column(types.Int32, nullable=False) __table_args__ = (engines.SummingMergeTree( partition_by=func.toYYYYMM(date), order_by=(date, )), ) # Define SELECT for Materialized View MatView = MaterializedView( GroupedStatistics, select([ Statistics.date.label('date'), func.sum(Statistics.metric1 * Statistics.sign).label('metric1') ]).where(Statistics.grouping > 42).group_by(Statistics.date)) Statistics.__table__.create() MatView.create() inspector = inspect(self.session.connection()) self.assertTrue(inspector.has_table(MatView.name)) MatView.drop() self.assertFalse(inspector.has_table(MatView.name))
class TestTable(get_declarative_base()): x = Column(types.Int8, primary_key=True) y = Column(types.Int8, clickhouse_materialized=x) __table_args__ = (engines.Memory(), )
class TestTable(get_declarative_base()): x = Column(types.Int8, primary_key=True) y = Column(types.Int8, server_default=x) __table_args__ = (engines.Memory(), )
def base(self): return get_declarative_base()
return filter_key else: filter_key = filter_key.split('_') filter_key[-1] = str(int(filter_key[-1]) + 1) filter_key = '_'.join(filter_key) return ClickhouseStore.__clause_key_combine( filter_key, filter_dict) @staticmethod def __update_format(update_dict): update_sql = ','.join( ['{}={}'.format(key, value) for key, value in update_dict.items()]) return update_sql CkBase = get_declarative_base() class DetailsTraffic(CkBase): __tablename__ = 'details_traffic' id = Column(types.String, primary_key=True, default=uuid.uuid4()) # 主键id req_time = Column(types.DateTime) # 请求时间 10位时间戳 req_date = Column(types.Date) # 请求时间 Y-m-d req_hour = Column(types.Int) # 请求时间 H plat = Column(types.Int8) # 平台 agent_type = Column(types.Int8) # 设备来源: 1/PC 2/WAP 3/公众号 4/IOS 5/Android website = Column(types.String) # 网页标题 url = Column(types.String) # url host = Column(types.String) # 域名 ref_url = Column(types.String) # 上级url ip = Column(types.String) # ip
# "password": "", "server_host": "localhost", "port": "8123", "db": "system", } connection = "clickhouse://default:@{server_host}:{port}/{db}".format(**conf) engine = create_engine(connection, echo=False, pool_size=100, pool_recycle=3600, pool_timeout=20) # Base = declarative_base(bind=engine) metadata = MetaData(bind=engine) metadata.reflect(schema="system", only=["asynchronous_metrics"]) Base = get_declarative_base(metadata=metadata) class System(Base): __tablename__ = "asynchronous_metrics" __table__ = Table(__tablename__, metadata, autoload=True, autoload_with=engine) # metric = Column(String, primary_key=True) __mapper_args__ = {"primary_key": [__table__.c.metric]} # Session = sessionmaker(bind=engine) # session = Session() session = make_session(engine)