Skip to main content

K2data内部的数据分析工具包

Project description

k2magic

K2Magic是K2Assets提供的数据分析开发包(以下简称SDK),用于简化Python里访问各类数据库的操作。

安装sdk

执行以下命令安装:

pip install k2magic

使用sdk

基本用法

使用SDK可以直接连接到指定数据库,并读写其中关系数据表的数据。假设数据库里有关系表table1的结构如下::

CREATE TABLE table1 (
    k_device VARCHAR(255) PRIMARY KEY,
    col1 FLOAT,
    col2 FLOAT,
    col3 FLOAT
);

对上述关系表操作的示例代码:

import pandas as pd
from k2magic.dataframe_db import DataFrameDB

db = DataFrameDB('postgresql+psycopg2://...')
df = pd.DataFrame({'k_device': ['a', 'b', 'c'], 'col1': [1, 2, 3], 'col2': [4, 5, 6]})

db.insert('table1', df)
db.update('table1', df, index_keys=['k_device'])
db.upsert('table1', df, index_keys=['k_device'])
df = db.select('table1', condition='col1 > 1')
df = db.select('table1', limit=3, order_by=['k_device DESC'])
df = db.sql_select('select * from table1')
db.delete('table1')

db.create_table(df, 'table2', primary_keys=['k_device'])
db.drop_table('table2')

切换数据库

SDK兼容多种数据库的方言,用户只需修改初始化DataFrameDB时的连接字符串参数,其余代码不需要修改,即可切换到另一个数据库。 各数据库的连接字符串格式如下:

已经过测试的:

  • postgresql+psycopg2://username:password@hostname/database_name
  • oracle+cx_oracle://username:password@hostname:port/database_name
  • mssql+pymssql://username:password@hostname:port/database_name
  • calcite://hostname:port/k2repo

未经过测试的:

注意:使用前请确保已安装相应的数据库的驱动,例如pip install pymysql

读取K2Assets数据

除了能够直接访问各类数据库外,SDK还支持访问K2Assets Repo里的数据,即可以将Repo作为一张表来使用。

使用方法与其他数据库是相同的,只要更换连接字符串即可,下面是一个示例:

import pandas as pd
from k2magic.dataframe_db import DataFrameDB

# 连接到dev环境的K2Assets,每个repo视为一张表
db = DataFrameDB('calcite://192.168.132.167:8765/k2repo')

# 查询数据(默认返回最新数据)
df = db.select('repo_XiLinHaoTe_1sec', columns=['k_device', 'k_ts', 'NacWdSpdFltS', 'CnvW'])

# 查询数据(带时间和设备过滤条件)
df = db.select('repo_XiLinHaoTe_1sec', columns=['k_device', 'k_ts', 'NacWdSpdFltS', 'CnvW'],
              condition='k_ts between 1656691200000 AND 1656777600000 and k_device = \'XiLinHaoTe002\'')

注意:

  • 由于Repo的数据接口只支持读取数据,因此通过SDK也只能读取Repo数据。
  • K2Assets环境需要部署k2a-calcite-service服务,目前此服务还没有包含在K2Assets产品包里,只在dev环境部署了用于测试。

异常处理

SDK进行数据库操作时,如果中途遇到异常情况会抛出DataFrameDBException,并自动回滚数据库到操作前的状态。用户可以使用try..except捕获此异常进行必要的处理:

try:
    db.insert('table1', df)
except DataFrameDBException as e:
    print('数据插入(部分)失败,操作已回滚')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

k2magic-0.2.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

k2magic-0.2.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file k2magic-0.2.0.tar.gz.

File metadata

  • Download URL: k2magic-0.2.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.13

File hashes

Hashes for k2magic-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f25ed5b91ad25ee801b6c445623500dff94154598fb9ecb0f1d105adb94e0cf4
MD5 9048f3c0f9ff94a517ccf2cb7238df4c
BLAKE2b-256 0e37b4966c490e844925af96da184d56010fe819fc2d473571ecb7770c0c66c0

See more details on using hashes here.

File details

Details for the file k2magic-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: k2magic-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.13

File hashes

Hashes for k2magic-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f55adffb79492a01ed3354d76ba7695281f5cdd5f704b17f4d626588c6eedd1f
MD5 01e9ff7eb8c843d88df38c261e955d31
BLAKE2b-256 278a3ae3bcb589e2f065a032c1f208411c023e7cef503277658d443a997da53d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page