我对 Oracle NoSQL 数据库 - SQL shell 工具中的导入命令有疑问。我想知道是否有一个选项可以读取文件并将数据放入 JSON 列中,而不是将数据放入表中。目前,它正在将 JSON 文件中的字段与表中的列进行匹配。类似的东西
import -table <table_name> - column <myjsoncolumn> -file <myfile>
这里是一个例子 - 简化版
sql-> create table stat (reportTime long, reportTimeHuman string , primary key (reportTime));
Statement completed successfully
sql-> import -table stat -file file.json
Loaded 736 rows to stat.
sql-> select * from stat limit 5;
{"reportTime":1624370080000,"reportTimeHuman":"2021-06-22 13:54:40.000 UTC"}
{"reportTime":1624366760000,"reportTimeHuman":"2021-06-22 12:59:20.000 UTC"}
{"reportTime":1624368660000,"reportTimeHuman":"2021-06-22 13:31:00.000 UTC"}
{"reportTime":1624370980002,"reportTimeHuman":"2021-06-22 14:09:40.002 UTC"}
我想做
CREATE TABLE IF NOT EXISTS stat
( id INTEGER GENERATED ALWAYS AS IDENTITY, myJson JSON, PRIMARY KEY (id))
import -table stat -column myJSON -file file.json
sql-> select myJson from stat limit 5;
{"reportTime":1624370080000,"reportTimeHuman":"2021-06-22 13:54:40.000 UTC"}
{"reportTime":1624366760000,"reportTimeHuman":"2021-06-22 12:59:20.000 UTC"}
{"reportTime":1624368660000,"reportTimeHuman":"2021-06-22 13:31:00.000 UTC"}
{"reportTime":1624370980002,"reportTimeHuman":"2021-06-22 14:09:40.002 UTC"}
我希望在 myJson 列中有 json 文档。在这种情况下,id 是生成的数字。
答案 0 :(得分:0)
不,使用 shell 没有这样的选项,但迁移工具可以做到这一点
第 1 步:创建您的表格
第 2 步:创建以下文件 ./migrator-export.json
{
"source" : {
"type" : "file",
"format" : "json",
"dataPath" : "/data/test/kvstore_export/"
},
"sink" : {
"type" : "nosqldb",
"storeName" : "OUG",
"helperHosts" : ["localhost:5000"],
"table" : "stat",
"requestTimeoutMs" : 5000
},
"transforms": {
"aggregateFields" : {
"fieldName" : "myJson",
"skipFields" : []
}
},
"abortOnError" : true,
"migratorVersion" : "1.0.0"
}
第三步;使用之前的配置文件执行命令 Migrator。
您也可以按照向导操作
Would you like to create table as part of migration process?
Use this option if you want to create table through the migration tool.
If you select yes, you will be asked to provide a file that contians table DDL or to use default schema.
(y/n) (n): y
We identified source as file.
Would you like to use below default schema?
CREATE TABLE IF NOT EXISTS stat(id LONG GENERATED ALWAYS AS IDENTITY(CACHE 5000), document JSON, PRIMARY KEY(SHARD(id)))
Where 'id' will be auto generated and 'document' is all the
fields aggregated into one JSON column.
Please note that tool will internally create table with above mentioned schema.
For aggregation below transforms are applied
internally by the tool."transforms" : {
"aggregateFields" : {
"fieldName" : "document"
}
}
(y/n) (n): y