SQL_duckdb分析nuScenes数据
使用duckdb来了解数据
using self-driving car data from the nuScenes dataset,
and writing Python code to visualize its route, IMU acceleration, and perceived objects.
传统方法:
nuScenes官方提供了一个数据集开发工具nuscenes-devkit,封装了数据读取、索引、可视化等常用操作,可以直接使用pip安装:
pip install nuscenes-devkit
Python 内容
使用数据库的方式
# 安装wheel
pip install wheel
# 创建wheel分发
python setup.py bdist_wheel
# 安装wheel分发的包
pip install dist/my_package-0.1-py3-none-any.whl
pybind11依赖于python(2.7或3.5+)和C++标准库。pybind11核心功能:pybind11可以将以下C++功能映射到Python
setuptools_scm是PYPA推荐的一个自动管理Python包版本号的工具,是setuptools的一个插件
命令行执行.py文件时有两种加载方式:
python xxx.py与python -m xxx
直接启动:把run.py文件所在的目录放到了sys.path属性中。
模块启动:输入命令的目录(也就是当前路径) 把也就是当前路径放到了sys.path属性中
1.下载数据
Loading data
查询数据
query = "SELECT class_name,COUNT(*) as count FROM annotations GROUP BY class_name ORDER BY count DESC"
res = duckdb.query(query).df()
res.plot.bar(x="class_name", y="count", legend=False);
Data Visualization
robotic数据可视化
1.桌面应用 rviz是一款三维可视化工具,很好的兼容了各种基于ROS软件框架的机器人平台
2.Web-based。基于Web技术
基于web的RViz实现版本,目前主要有WebViz和FoxGlove两个,都是Cruise是一家做自动驾驶的美国初创公司来维护的
新颖的Web渲染技术,如WebGL和WebAssembly
json
Json字符串
json 数组用“[]”创建,对象用“{}”创建
ndJSON的格式,由换行符(0x0A)分隔每个json对象,最外面也没有闭合字符对。ndjson的mime类型是application/x-ndjson。
根据json创建表格
#! /usr/bin/python3
# -*- coding:utf-8 -*-
import duckdb
import os
import json
import ndjson
def create_table_from_json(src_json_dir):
json_file_nms = [ os.path.join(src_json_dir,src_json)
for src_json in sorted(os.listdir(src_json_dir) )
if src_json.endswith(".json") ]
duckdb.sql("select 2").show()
# 创建DB(嵌入式DBMS)
conn = duckdb.connect(r'D:\nuscenes\annot.duckdb')
c = conn.cursor()
# loda_data -创建表
for i,src_data in enumerate(json_file_nms) :
table_nm = os.path.basename(src_data).replace(".json","")
c.execute("drop table IF EXISTS {}".format(table_nm))
sql_cmd = ("CREATE TABLE {0} AS SELECT * FROM read_json('{1}',auto_detect=true,json_format='auto',"
"maximum_object_size=999777216);").format(
table_nm,src_data)
print(i,sql_cmd)
c.execute(sql_cmd)
conn.close()
def change_json_object_njson(src_json_dir):
""" converting the contents into newline-delimited JSON"""
for json_file in sorted(os.listdir(src_json_dir)):
json_src_file = os.path.join(src_json_dir,json_file)
json_dst_file = os.path.join(src_json_dir,"ndjson",json_file)
print(f'{json_src_file} --> {json_dst_file}')
with open(json_src_file) as f_json_src_file, open(json_dst_file, 'w') as outfile:
json_dict = json.load(f_json_src_file)
ndjson.dump(json_dict, outfile)
if __name__ =="__main__":
#auto_src_json_dir = r"D:\nuscenes\v1.0-mini"
sample_json_dir = r"D:\nuscenes\sample_data"
duckdb.sql("select 12").show()
# change_json_object_njson(sample_json_dir)
#create_table_from_json(auto_src_json_dir)
读写数据
Error: duckdb.InvalidInputException: Invalid Input Error: Malformed JSON in file
at byte 2097149 in object 2: unexpected end of data. Try increasing "maximum_object_size".
Error:duckdb.InternalException: INTERNAL Error: Unexpected yyjson tag in ValTypeToString
https://github.com/saubury/duckdb-fitbit
(duckdb.InvalidInputException) "INTERNAL Error: Unexpected yyjson tag in ValTypeToString"
Duck DB
只需指定要 EXCLUDE 的列:单个语句中处理来自多个表的排除项
SELECT * EXCLUDE (jar_jar_binks, midichlorians) FROM star_wars
DuckDB 中,使用 REPLACE 轻松地将更改应用于少量列:
SELECT * REPLACE (movie_count+3 as movie_count, show_count*1000 as show_count)
AsOf Joins 用于解决的问题之一是查找特定时间点的变化属性的值
非同步关联,asof join和window join是最常用的非同步关联
ASOF JOIN ASOF LEFT JOIN
window Joins
DuckDB User Defined Function (UDF)
DuckDB 可以根据正则表达式模式匹配、EXCLUDE 或 REPLACE 修饰符,甚至是 lambda 函数来选择和修改列
common table expression (CTE) 声明CTE的需要使用语法WITH
表达式 COLUMNS 可以接受作为正则表达式的字符串作为参数,并将返回与模式匹配的所有列名。
DuckDB 对每个标量函数都进行了同样的可读性改进!使用dot运算符将函数串联起来,
就像在 Python 中一样。链中的前一个表达式将作为后一个函数的第一个参数。
UNION 替换为 UNION BY NAME 或将 UNION ALL 替换为 UNION ALL BY NAME 即可。灵感来自 Pandas 中的 concat 函数
,DuckDB 的 PIVOT and UNPIVOT 子句可以创建或堆叠(stack)动态列名,以实现真正灵活的透视功
数据分析
fitbit手环 数据分析
参考
使用DuckDB更友好的SQL https://zhuanlan.zhihu.com/p/636792219
https://duckdb.org/2022/05/04/friendlier-sql.html
https://duckdb.org/2023/08/23/even-friendlier-sql.html
https://duckdb.org/2023/09/15/asof-joins-fuzzy-temporal-lookups.html
https://github.com/saubury/duckdb-fitbit
https://towardsdatascience.com/my-very-personal-data-warehouse-fitbit-activity-analysis-with-duckdb-8d1193046133
https://github.com/foxglove/studio
https://colab.research.google.com/github/foxglove/jupyter-data-platform/blob/main/FoxgloveDataPlatform.ipynb
分类:
SQL_数据仓库
, Python_Java_数据开发
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2021-09-27 ROS_Opencv-摄像头图像发布和订阅
2021-09-27 云计算_Docker的配置和使用
2020-09-27 数据开发-Python数据预处理案例
2020-09-27 Spark开发-SparkUI查看metric和日志