我目前正在尝试通过 JDBC 优化 Postgres 中的数据加载。我们正在使用 COPY FROM STDIN 和 FORMAT 'binary' 现在构建二进制字节数组对于字符串、长整型、uuid 等来说非常简单。但是在一个实例中,我们表中有一个 JSONB 字段,我不知道如何序列化我的json 对象转换为二进制 jsonb 格式。jsonb 有任何规范吗?
注意:我已经排除了只发送 utf-8 二进制序列化 json 字符串的可能性。
当我将数据保存到表中,然后从表中选择时,它会变成不同的顺序,我在 pgadmin 中检查也是如此。
为什么??以及如何解决?
CREATE TABLE IF NOT EXISTS "user_role_track"(
"id" SERIAL NOT NULL,
"create_date" timestamp without time zone,
"create_by_user_id" integer,
"action" integer,
"old_data" jsonb,
"new_data" jsonb
);
Run Code Online (Sandbox Code Playgroud)
在nodejs应用程序中创建数据
var newData = {
"id": userRoleId,
"create_date": timestamp,
"user_id": userId,
"role": role
};
...
// save with promise sync function
var dbQueryR = yield Promise.resolve( queryPromise(dbClient, dbQuery, dbParams) );
Run Code Online (Sandbox Code Playgroud)
从表中选择/通过 pgadmin 查看
"{"id": 2, "role": 1, "user_id": 17, "create_date": "2016-07-11 09:09:18"}"
Run Code Online (Sandbox Code Playgroud) 我是 JSON/JSONB 数据类型的新手,在从更深层次选择 JSONB 元素时遇到一些问题。
这是一个示例表:
CREATE TABLE person (id integer, details jsonb);
INSERT INTO person (id, details) VALUES
("id": 1, {"favorites":{"colors":"blue", "colors":"red"}}),
("id": 2),
("id": 3, {"favorites":{"colors":"blue", "colors":"red", "colors":"green"}});
Run Code Online (Sandbox Code Playgroud)
我想选择所有 p.details ->'favorites' ->>'colors',例如:
------------------------
| id | Favorite colors |
------------------------
| 1 | blue |
------------------------
| 1 | red |
------------------------
| 3 | blue |
------------------------
| 3 | red |
------------------------
| 3 | green |
------------------------
Run Code Online (Sandbox Code Playgroud)
以下(或类似)给出了最喜欢的颜色的空列(也尝试使用 jsonb_array_elements):
SELECT p.id, p.details ->'favorites' ->>'colors' AS …Run Code Online (Sandbox Code Playgroud) 我的 postgres 数据库中有以下数据结构 - 一个名为的 jsonb 列customer
{
"RequestId": "00000000-0000-0000-0000-000000000000",
"Customer": {
"Status": "A",
"AccountId": 14603582,
"ProfileId": 172,
"ReferralTypeId": 15
}
"Contact": {
"Telephone": "",
"Email": ""
}
}
Run Code Online (Sandbox Code Playgroud)
我想在该ProfileId字段上创建一个索引,该索引是一个整数。
我一直无法找到如何在嵌套字段上创建索引的示例。
我正在执行的查询(大约需要 300 秒)是:
select id, customer from where customer @> '{"Customer":{"ProfileId": 172}}'
Run Code Online (Sandbox Code Playgroud) $this->assertDatabaseHas()不使用JSON/JSONb列。
那么如何在 Laravel 中测试这些类型的列呢?
目前,我有一个商店行动。如何执行断言,保存具有预定义值的特定列。
就像是
['options->language', 'en']
不是一个选项,因为我有一个包含元内容的大量 JSON 。
如何JSON立即检查数据库中的内容?
如何将 jsonb 中的时间戳更新为函数 now() 的值?
我尝试像这样做smt
UPDATE test
SET column = jsonb_set(column,'{time}',to_char(now(),'some date format'),true)
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的错误
'jsonb_set(json,unknown,text,bool) is not available'
Run Code Online (Sandbox Code Playgroud)
我认为错误的原因是 now() 的值不是单引号,因为这个查询正在工作
UPDATE test
SET column = jsonb_set(column,'{time}','date with the same date format ',true)
Run Code Online (Sandbox Code Playgroud)
有什么解决办法吗?
select id,rules from links where id=2;
id | rules
----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 | {"{\"id\": \"61979e81-823b-419b-a577-e2acb34a2f40\", \"url\": \"https://www.wikijob.co.uk/about-us\", \"what\": \"country\", \"matches\": \"GB\", \"percentage\": null}"}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用此处的运算符获取 jsonb 的元素https://www.postgresql.org/docs/9.6/functions-json.html
无论我使用“url”还是下面的整数,我都会得到类似的结果。
select id,rules->1 from links where id=2;
ERROR: operator does not exist: jsonb[] -> integer
LINE 1: select id,rules->1 from links where id=2;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
PS Postgres 版本 9.6.12。
我在 postgres 表列中存储了一个大的 json 对象,模式的一个例子是这样的:
create_table “document” do |t|
t.jsonb “data”, default: []
end
Run Code Online (Sandbox Code Playgroud)
目前我正在更新列中的 json,如下所示:
# find document in rails then…
doucment.data[‘some_attribute’][2][‘another_attribute’] = 100
doucment.save
Run Code Online (Sandbox Code Playgroud)
然而,我多次写入这个 json 属性,有时数据会丢失,因为如果两个调用同时写入它,那么整个对象将被保存或覆盖当前对象的旧数据。
例如,如果有两个不同的保存同时通过以下
保存1:
doucment.data[‘some_attribute’][2][‘another_attribute’] = 100
doucment.save
Run Code Online (Sandbox Code Playgroud)
保存2:
doucment.data[‘some_attribute’][2][‘different_attribute’] = 200
doucment.save
Run Code Online (Sandbox Code Playgroud)
那么其中一个属性数据将丢失,因为另一个将保存它的 json 但带有尚未刷新的旧数据。
使两个调用正确保存新数据的最佳方法是什么。
是否有任何 json 方法可以进入并更新一个属性,例如update_attribute但是对于 jsonb 属性?
我的专栏之一是 jsonb 并且具有格式价值。单行列的值如下。
{
"835": {
"cost": 0,
"name": "FACEBOOK_FB1_6JAN2020",
"email": "test.user@silverpush.co",
"views": 0,
"clicks": 0,
"impressions": 0,
"campaign_state": "paused",
"processed":"in_progress",
"modes":["obj1","obj2"]
},
"876": {
"cost": 0,
"name": "MARVEL_BLACK_WIDOW_4DEC2019",
"email": "test.user@silverpush.co",
"views": 0,
"clicks": 0,
"impressions": 0,
"campaign_state": "paused",
"processed":"in_progress",
"modes":["obj1","obj2"]
}
}
Run Code Online (Sandbox Code Playgroud)
我想更新campaign_info(列名)列的内部键“已处理”和“模型”的campaign_id 是“876”。
我试过这个查询:
update safe_vid_info
set campaign_info -> '835' --> 'processed'='completed'
where cid = 'kiywgh';
Run Code Online (Sandbox Code Playgroud)
但它没有用。
任何帮助表示赞赏。谢谢。
我正在尝试序列化具有双向关系的实体:
class TypeA {
String name;
TypeB typeB;
}
class TypeB {
String identifier;
TypeA typeA;
}
Run Code Online (Sandbox Code Playgroud)
与杰克逊我解决@JsonBackReference中的TypeB属性和@JsonManagedReference中的typeA属性,但我怎么能做到这一点的JSONB(Eclipse的Yasson实现)?
Caused by: javax.json.bind.JsonbException: Recursive reference has been found in class class xxxxxx.model.Analysis.
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:76)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:107)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:125)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:121)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:69)
... 45 more
Run Code Online (Sandbox Code Playgroud)
OBS:我用 DTO 解决了,但疑问依然存在。
jsonb ×10
postgresql ×9
json ×5
arrays ×1
indexing ×1
jackson ×1
java ×1
laravel ×1
laravel-5.6 ×1
node.js ×1
sql ×1
sql-update ×1