インポートとエクスポート
CSV のインポート
curl -o - https://docs.machbase.com/assets/example/example.csv.gz | \
machbase-neo shell import \
--input - \
--compress gzip \
--timeformat s \
EXAMPLEcurl でリモート Web サーバーから圧縮 CSV をダウンロードします。-o - は取得した圧縮バイナリーを標準出力へ書き込み、パイプで machbase-neo shell import に渡します。import は --input - で標準入力を読み取ります。
パイプ(|)で接続すると、一時ファイルを作成せずに処理でき、ローカルストレージを節約できます。
次の結果は、1,000 件のレコードをインポートできたことを示します。
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 5352 100 5352 0 0 263k 0 --:--:-- --:--:-- --:--:-- 275k
Import 1,000 rows completed. 1000,0ファイルをローカルに保存してからインポートすることもできます。
curl -o data.csv.gz https://docs.machbase.com/assets/example/example.csv.gzCSV は圧縮の有無にかかわらずインポートできます。ローカルファイルを --input <ファイル> で指定し、gzip 圧縮の場合は --compress gzip を追加してください。
-v /mnt=. は、現在のディレクトリ(.)をシェルの実行環境の /mnt にマウントします。ローカルファイルには、マウント後のパス(例: /mnt/data.csv.gz)でアクセスします。-v を指定しない場合、現在のディレクトリには /work(例: /work/data.csv.gz)でアクセスできます。
machbase-neo shell -v /mnt=. \
import \
--input /mnt/data.csv.gz \
--compress gzip \
--timeformat s \
EXAMPLEインポート結果を確認します。
machbase-neo shell "select * from example order by time desc limit 5"┌────────┬──────────┬─────────────────────┬───────────┐
│ ROWNUM │ NAME │ TIME │ VALUE │
├────────┼──────────┼─────────────────────┼───────────┤
│ 1 │ wave.sin │ 2023-02-15 12:47:50 │ 0.99454 │
│ 2 │ wave.cos │ 2023-02-15 12:47:50 │ -0.104353 │
│ 3 │ wave.cos │ 2023-02-15 12:47:49 │ 0.309185 │
│ 4 │ wave.sin │ 2023-02-15 12:47:49 │ 0.951002 │
│ 5 │ wave.cos │ 2023-02-15 12:47:48 │ 0.669261 │
└────────┴──────────┴─────────────────────┴───────────┘
5 rows selected.サンプルには 1,000 件のレコードがあります。インポート後のテーブルにも同じ件数が格納されていることを確認します。
machbase-neo shell "select count(*) from example"┌────────┬──────────┐
│ ROWNUM │ COUNT(*) │
├────────┼──────────┤
│ 1 │ 1000 │
└────────┴──────────┘
a row selected.CSV のエクスポート
--output で出力先のファイルパスを、--format csv で CSV 形式を指定します。--timeformat ns は DATETIME 列をナノ秒単位の UNIX エポック時刻で出力します。
machbase-neo shell export --output ./example_out.csv --format csv --timeformat ns EXAMPLEエクスポートとインポートによるテーブルのコピー
エクスポートとインポートをパイプで接続すると、ローカルの一時ファイルなしでデータをコピーできます。
まず、コピー先のテーブルを作成します。
machbase-neo shell \
"create tag table EXAMPLE_COPY (name varchar(100) primary key, time datetime basetime, value double)"次に、export と import をパイプで接続します。
machbase-neo shell export \
--output - \
--no-header \
--format csv \
--timeformat ns \
EXAMPLE | \
machbase-neo shell import \
--input - \
--format csv \
--timeformat ns \
EXAMPLE_COPYコピー先の件数を確認します。
machbase-neo shell "select count(*) from EXAMPLE_COPY"┌────────┬──────────┐
│ ROWNUM │ COUNT(*) │
├────────┼──────────┤
│ 1 │ 1000 │
└────────┴──────────┘
a row selected.この方法は、データベース A から B へのコピーにも使用できます。--server <アドレス> でリモートの machbase-neo を指定し、export と import を別々のサーバーに対して実行できます。
クエリ結果のインポート
SELECT の結果を直接 import に渡すこともできます。
machbase-neo shell sql \
--output - \
--format csv \
--no-rownum \
--no-header \
--no-footer \
--timeformat ns \
"select * from example where name = 'wave.sin' order by time" | \
machbase-neo shell import \
--input - \
--format csv \
EXAMPLE_COPYこの例は wave.sin タグのデータを EXAMPLE_COPY にインポートします。import は入力 CSV のフィールド数と型を検証するため、sql に --no-rownum、--no-header、--no-footer を指定します。
HTTP API によるクエリ結果の取り込み
machbase-neo の HTTP API から取得したクエリ結果を、別のテーブルに取り込めます。
curl -o - http://127.0.0.1:5654/db/query \
--data-urlencode "q=select * from EXAMPLE order by time desc limit 100" \
--data-urlencode "format=csv" \
--data-urlencode "heading=false" | \
curl http://127.0.0.1:5654/db/write/EXAMPLE_COPY \
-H "Content-Type: text/csv" \
-X POST --data-binary @- インポートの書き込み方式
import は INSERT INTO ... ではなく append 方式でデータを書き込みます。append 方式は、数十万件以上の大量データを書き込む場合に効率的です。
例
データファイルをテーブルに取り込む例を示します。
CREATE TAG TABLE IF NOT EXISTS EXAMPLE (
NAME VARCHAR(20) PRIMARY KEY,
TIME DATETIME BASETIME,
VALUE DOUBLE SUMMARIZED
);CSV のインポート
次の内容で data.csv を作成します。
name-0,1687405320000000000,123.456
name-1,1687405320000000000,234.567000
name-2,1687405320000000000,345.678000データをインポートします。machbase-neo shell は現在のディレクトリを /work にマウントするため、data.csv があるディレクトリでコマンドを実行します。
machbase-neo shell import \
--input /work/data.csv \
--timeformat ns \
EXAMPLEデータを検索します。
machbase-neo shell "SELECT * FROM EXAMPLE";
┌────────┬────────┬─────────────────────┬─────────┐
│ ROWNUM │ NAME │ TIME │ VALUE │
├────────┼────────┼─────────────────────┼─────────┤
│ 1 │ name-0 │ 2023-06-22 12:42:00 │ 123.456 │
│ 2 │ name-1 │ 2023-06-22 12:42:00 │ 234.567 │
│ 3 │ name-2 │ 2023-06-22 12:42:00 │ 345.678 │
└────────┴────────┴─────────────────────┴─────────┘
3 rows selected.TQL によるインポート
テキストのインポート
以下の結果例は、空の EXAMPLE テーブルに各入力を 1 回ずつ取り込んだ場合です。時刻と表示順序は実行時によって変わります。
次の内容で import-data.csv を作成します。
1,100,value,10
2,200,value,11
3,140,value,12次のコードを TQL エディターに入力し、import-tql-csv.tql として保存します。
STRING(payload() ?? `1,100,value,10
2,200,value,11
3,140,value,12`, separator('\n'))
SCRIPT({
str = $.values[0].trim().split(',');
$.yield(
"tag-" + str[0],
(new Date().getTime()*1000000),
parseInt(str[1])+parseInt(str[3])
)
})
APPEND(table("example"))作成した TQL に CSV を送信します。
curl -o - --data-binary @import-data.csv http://127.0.0.1:5654/db/tql/import-tql-csv.tql
append 3 rows (success 3, fail 0).データを検索します。
machbase-neo shell "select * from example";
┌────────┬───────┬─────────────────────────┬───────┐
│ ROWNUM │ NAME │ TIME │ VALUE │
├────────┼───────┼─────────────────────────┼───────┤
│ 1 │ tag-1 │ 2026-09-17 17:10:05.268 │ 110 │
│ 2 │ tag-2 │ 2026-09-17 17:10:05.268 │ 211 │
│ 3 │ tag-3 │ 2026-09-17 17:10:05.268 │ 152 │
└────────┴───────┴─────────────────────────┴───────┘
3 rows selected.JSON のインポート
import-data.json を作成します。
{
"tag": "pump",
"data": {
"string": "Hello TQL?",
"number": "123.456",
"time": 1687405320,
"boolean": true
},
"array": ["elements", 234.567, 345.678, false]
}次のコードを TQL エディターに入力し、import-tql-json.tql として保存します。
STRING( payload() ?? {
{
"tag": "pump",
"data": {
"string": "Hello TQL?",
"number": "123.456",
"time": 1687405320,
"boolean": true
},
"array": ["elements", 234.567, 345.678, false]
}
})
SCRIPT({
obj = JSON.parse($.values[0]);
$.yield(obj.tag+"_0", obj.data.time*1000000000, parseFloat(obj.data.number))
$.yield(obj.tag+"_1", obj.data.time*1000000000, obj.array[1])
$.yield(obj.tag+"_2", obj.data.time*1000000000, obj.array[2])
for (i = 0; i < obj.array.length; i++) {
}
})
APPEND(table("example"))作成した TQL に JSON を送信します。
curl -o - --data-binary @import-data.json http://127.0.0.1:5654/db/tql/import-tql-json.tql
append 3 rows (success 3, fail 0).データを検索します。
machbase-neo shell "select * from example";
┌────────┬────────┬─────────────────────────┬─────────┐
│ ROWNUM │ NAME │ TIME │ VALUE │
├────────┼────────┼─────────────────────────┼─────────┤
│ 1 │ tag-1 │ 2026-09-17 17:10:05.268 │ 110 │
│ 2 │ pump_2 │ 2023-06-22 12:42:00 │ 345.678 │
│ 3 │ tag-2 │ 2026-09-17 17:10:05.268 │ 211 │
│ 4 │ tag-3 │ 2026-09-17 17:10:05.268 │ 152 │
│ 5 │ pump_1 │ 2023-06-22 12:42:00 │ 234.567 │
│ 6 │ pump_0 │ 2023-06-22 12:42:00 │ 123.456 │
└────────┴────────┴─────────────────────────┴─────────┘
6 rows selected.ブリッジからのインポート
事前準備
bridge add -t sqlite mem file::memory:?cache=shared;
bridge exec mem create table if not exists mem_example(name varchar(20), time datetime, value double);
bridge exec mem insert into mem_example values('tag0', '2021-08-12', 10);
bridge exec mem insert into mem_example values('tag0', '2021-08-13', 11);ブリッジのデータのインポート
TQL エディターで次のコードを実行します。
SQL(bridge('mem'), "select * from mem_example")
APPEND(table('example'))データを検索します。
machbase-neo shell "select * from example";
┌────────┬──────┬─────────────────────┬───────┐
│ ROWNUM │ NAME │ TIME │ VALUE │
├────────┼──────┼─────────────────────┼───────┤
│ 1 │ tag0 │ 2021-08-12 09:00:00 │ 10 │
│ 2 │ tag0 │ 2021-08-13 09:00:00 │ 11 │
└────────┴──────┴─────────────────────┴───────┘
2 rows selected.CSV のエクスポート
データをエクスポートします。
machbase-neo shell export \
--output ./data_out.csv \
--format csv \
--timeformat ns \
EXAMPLE出力したファイルを確認します。
cat data_out.csv
TAG0,1628694000000000000,100
TAG0,1628780400000000000,110JSON のエクスポート
HTTP API でデータをエクスポートします。
curl -o data_out.json http://127.0.0.1:5654/db/query \
--data-urlencode "q=select * from EXAMPLE" \
--data-urlencode "format=json" \
--data-urlencode "timeformat=ns"出力したファイルを確認します。
cat data_out.json
{"data":{"columns":["NAME","TIME","VALUE"],"types":["string","datetime","double"],"rows":[["TAG0",1628694000000000000,100],["TAG0",1628780400000000000,110]]},"success":true,"reason":"success","elapse":"865.833µs"}TQL によるエクスポート
CSV のエクスポート
SQL(`select * from example`)
CSV()JSON のエクスポート
SQL(`select * from example`)
JSON()TQL スクリプトで CSV をエクスポート
次のコードを TQL エディターに入力し、export-tql-csv.tql として保存します。
SQL( 'select * from example limit 30' )
SCRIPT({
if ($.values[2] % 2 == 0) {
r_value = "even"
} else {
r_value = "odd"
}
$.yield($.key + "-tql", $.values[2], r_value)
})
CSV()TQL の URL をブラウザーで開くか、ターミナルから curl で取得します。
TAG1-tql,11,odd
TAG0-tql,10,evenブリッジへのエクスポート
事前準備
bridge add -t sqlite mem file::memory:?cache=shared;
bridge exec mem create table if not exists mem_example(name varchar(20), time datetime, value double);ブリッジへのデータのエクスポート
次のコードを TQL エディターで実行します。
SQL("select * from example")
INSERT(bridge('mem'), table('mem_example'), 'name', 'time', 'value')ブリッジのテーブルを検索します。
machbase-neo shell bridge query mem "select * from mem_example";
┌──────┬───────────────────────────────┬───────┐
│ NAME │ TIME │ VALUE │
├──────┼───────────────────────────────┼───────┤
│ TAG0 │ 2021-08-12 00:00:00 +0900 KST │ 10 │
│ TAG1 │ 2021-08-13 00:00:00 +0900 KST │ 11 │
└──────┴───────────────────────────────┴───────┘