Comments (10)
Yes, you can create custom Engine in following way:
Without parameters following code:
class Graph(Engine):
def get_params(self):
return []
class TestTable(get_declarative_base()):
date = Column(types.Date, primary_key=True)
x = Column(types.Int32)
__table_args__ = (Graph(), )
Will render into:
CREATE TABLE test_table (date Date, x Int32) ENGINE = Graph
Variant with parameters is a little bit tricky. See rendering Engine method clickhouse_sqlalchemy.drivers.base.ClickHouseDDLCompiler.visit_engine
.
There is name
method of Engine
clickhouse_sqlalchemy.engines.Engine.name
that now produces name of the engine.
You can extend it in following way:
class Graph(Engine):
def __init__(self, unique, symmetric):
self.unique = unique
self.symmetric = symmetric
super(Graph, self).__init__()
def get_params(self):
return []
def name(self):
return (
super(Graph, self).name() +
' setting unique={}, symmetric={}'.format(
self.unique, self.symmetric
)
)
class TestTable(get_declarative_base()):
date = Column(types.Date, primary_key=True)
x = Column(types.Int32)
__table_args__ = (
Graph(1, 0),
)
And voila:
CREATE TABLE test_table (date Date, x Int32) ENGINE = Graph setting unique=1, symmetric=0
from clickhouse-sqlalchemy.
I know I can execute raw sql to do so, but could I use table_args ?
from clickhouse-sqlalchemy.
Also, we use such statement to set the graph property :
create table graph1 (w Float64) engine=Graph setting unique=1,symmetric=0;
from clickhouse-sqlalchemy.
Thanks,That's what I want!
And after define my engine,how could I view the sql?
CREATE TABLE test_table (date Date, x Int32) ENGINE = Graph setting unique=1, symmetric=0
from clickhouse-sqlalchemy.
Also,could you show me an example about how to use your engine MergeTree to create such a table?
CREATE TABLE edge (src String, dst String, time Date, year String) engine=MergeTree order by src partition by year;
from clickhouse-sqlalchemy.
@dalek-who you can view sql after compiling CreateTable
expression. See tests for example: tests.test_engines.EnginesDeclarativeTestCase
.
Currently modern syntax (ENGINE = MergeTree ... order by ... partition by...
) of engine definition is not supported. Dialect now produce old syntax: ENGINE = MergeTree(date, (date, x), 8192)
.
from clickhouse-sqlalchemy.
Is clickhouse-sqlalchemy capable with flask-sqlalchemy? I'm hesitating between clickhouse-sqlalchemy and flask-sqlalchemy. clickhouse-sqlalchemy have engine, but flask-sqlalchemy helps me deal with session automatically.
I don't know if flask-sqlalchemy's session could use clickhouse-sqlalchemy's model with engine, or if flask -sqlalchemy's model without engine could be used for clickhouse's insert/update/select/delete (giving up create table automatically). Next week I will have a try.
from clickhouse-sqlalchemy.
Unfortunately clickhouse-sqlalchemy is not capable with flask-sqlalchemy. The thing is ClickHouse dialect is more than traditional SQL. Some expressions must be rendered explicitly.
In out project we use both flask-sqlalchemy and clickhouse-sqlalchemy. The first is used for OLTP and the second for OLAP (ClickHouse).
Here we create singletone session object:
from sqlalchemy import create_engine, MetaData
from clickhouse_sqlalchemy import make_session, get_declarative_base
from project import app
engine = create_engine(app.config['CLICKHOUSE_DATABASE_URI'])
session = make_session(engine)
metadata = MetaData(bind=engine)
Base = get_declarative_base(metadata=metadata)
from clickhouse_sqlalchemy import engines, types as Types
from sqlalchemy import Column
from ... import Base
class Model(Base):
clicks = Column(Types.Int64)
...
__table_args__ = ( engines.MergeTree(...), )
When we need to query data from ClickHouse we just use session
object.
from ... import session as clickhouse_session
query = clickhouse_session.query(Model.clicks)
In other words clickhouse-sqlalchemy can be used for building queries with sqlalchemy engine. But it can't be used from flask-sqlalchemy.
from clickhouse-sqlalchemy.
session = make_session(engine)
Is this session a scoped_session like sqlalchemy?
from clickhouse-sqlalchemy.
No, currently it is not scoped, just usual Session
instance. You can create own scoped-session wrapper and try it. PR is appreciated if everything will go well.
from clickhouse-sqlalchemy.
Related Issues (20)
- Table name included in CRUD update while ClickHouse does not accept it
- Bulk update fails on ClickHouse
- Native driver fails with stream results
- Alembic couldn't create table alembic_version HOT 14
- Collate is not generating a correct query
- Error connecting with the database when password contains a special character (+%...) with native engine. HOT 1
- Does `clickhouse-sqlalchemy` 3.0.0 support `sqlalchemy` of the version 1.4.*? HOT 5
- Create a cluster table orm class with engines.Distributed which has a logs attribute, how to use variable to indicate it?
- alembic does not autogenerate engine for clickhouse table HOT 4
- Patreon does not work HOT 1
- Add support for Date32 HOT 2
- Handling Clickhouse Alembic Migrations for Clickhouse cluster HOT 2
- Support/example for creating views HOT 2
- `create_all` for all the `MaterializedView`
- With chdb this clickhouse downsized memory database, can clickhouse-sqlalchemy support it or not?
- Support VariantType
- Table reflection for DateTime64 timezone will be extra quoted
- Alembic_Version Records disappered after clickhouse alembic migration HOT 5
- using http mode, connecting database failed when account password ends with @ HOT 2
- Support nested Map column
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clickhouse-sqlalchemy.