superset数据集birth_names的负时间戳处理
生活随笔
收集整理的這篇文章主要介紹了
superset数据集birth_names的负时间戳处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
處理負時間戳
superset有個數據集叫birth_names.json
里面的ds這一欄全是負的
這個數據集的時間范圍其實是:
1965-01-01 08:00:00~2008-01-01 08:00:00
下面的方案來自[2],但是有個要命的問題就是無法處理負時間戳
mysql> create table foo(id INT, mytimestamp INT(11)); Query OK, 0 rows affected (0.02 sec) Insert some valuesmysql> insert into foo values(1, 1381262848); Query OK, 1 row affected (0.01 sec) Take a lookmysql> select * from foo; +------+-------------+ | id | mytimestamp | +------+-------------+ | 1 | 1381262848 | +------+-------------+ 1 row in set (0.00 sec) Convert the number to a timestamp:mysql> select id, from_unixtime(mytimestamp) from foo; +------+----------------------------+ | id | from_unixtime(mytimestamp) | +------+----------------------------+ | 1 | 2013-10-08 16:07:28 | +------+----------------------------+ 1 row in set (0.00 sec) Convert it into a readable format:mysql> select id, from_unixtime(mytimestamp, '%Y %D %M %H:%i:%s') from foo; +------+-------------------------------------------------+ | id | from_unixtime(mytimestamp, '%Y %D %M %H:%i:%s') | +------+-------------------------------------------------+ | 1 | 2013 8th October 04:07:28 | +------+-------------------------------------------------+ 1 row in set (0.00 sec)?
?
苦思冥想,最后將[3]中的命令修改如下:
SELECT DATE_ADD('1970-01-01 00:00:00',INTERVAL ds/1000 SECOND) from birth_names;
如果想要全部展示,那么:
SELECT DATE_ADD('1970-01-01 00:00:00',INTERVAL ds/1000 SECOND), gender,name,num ,state,sum_boys,sum_girls from birth_names;?
處理后建立新的表格
如果想把查詢結果另外新建一張表格,那么:
①導出為csv(注意不要修改導出路徑)
SELECT DATE_ADD('1970-01-01 00:00:00',INTERVAL ds/1000 SECOND), gender,name,num ,state,sum_boys,sum_girls from birth_names into outfile '/var/lib/mysql-files/birth_names.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n';導出之后發現日期格式變成這樣了:
不用擔心,因為如果用sublime打開的話,依然和查詢結果一致
②csv通過datagrip導入mysql
先把老的表格刪除:
drop table birth_names;然后新建個表格:
create table birth_names (ds datetime null,gender text null,name text null,num int null,state text null,sum_boys int null,sum_girls int null );?
右鍵表格->Import Data from file:
?
最終效果如下:
?
?
?
Reference:
[1]https://tool.lu/timestamp/
[2]Convert timestamp to date in MySQL query
[3]Converting negative values from FROM_UNIXTIME
總結
以上是生活随笔為你收集整理的superset数据集birth_names的负时间戳处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux终端按下退格键自动覆盖上一行的
- 下一篇: superset可视化-Bar Char