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
  • k2assets://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('k2assets://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.2.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: k2magic-0.2.2.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.2.tar.gz
Algorithm Hash digest
SHA256 873c0f619d74d6bf2a6f35ae50e681f2cf09f174a490de636f03b0d444d5efbc
MD5 b9e34d92f85c7b891248525bc3addb9f
BLAKE2b-256 a61b3a5d999fe64cedc53811859d82c316eacd402f9b63bcbb720efcb46157d5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: k2magic-0.2.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 61b282922388681df8ecc68e2272600abb718695afe5f617d8468173cf8869dc
MD5 77c785c60182d94d9f5c5814a35ba180
BLAKE2b-256 7843e46c1e4b641c9e8d0a7e687c917875e49d70fca67d49ae7bacd848dbb114

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