使用Apache AGE扩展创建图数据库时出现问题。

huangapple go评论68阅读模式
英文:

Issue creating graph database using apache AGE extension

问题

I'm trying to create a graph database from an existing relational database. So a little background information is that I have a table with product numbers and serial numbers, and I want a graph database that illustrates their relations. I have a tree-like structure in the product numbers, that's why I want to make a graph database with Apache AGE.

I have managed to run the command:

SELECT * FROM ag_catalog.create_graph('part_numbers_graph');

But when I run:

INSERT INTO part_numbers_graph
    SELECT agtype(format('{"id": %L, "label": "SSN", "properties": {"name": %L}}', SSN, SSN)::jsonb)
    FROM serial_table;

I get the following error:

relation "part_numbers_graph" does not exist

I have run the command:

SELECT * FROM ag_catalog.ag_graph;

I can see that the graph is there.

 graphid |        name        |     namespace
---------+--------------------+--------------------
  32924  | part_numbers_graph | part_numbers_graph

I have successfully been able to run both:

CREATE EXTENSION age;

and

LOAD 'age';

When I run:

pg_config

I can see that I have installed:

VERSION = PostgreSQL 12.14 (Ubuntu 12.14-1.pgdg22.04+1)
英文:

I'm trying to create a graph database from an existing relational database. So a little background information is that I have a tbale with product numbers and serial numbers and I want a graph database that illustrates their relations. I have a tree like structure in the product numbers, that's why I want to make a graph database with the apache AGE.

I have managed to run the command:

SELECT * FROM ag_catalog.create_graph('part_numbers_graph');

But when I run:

INSERT INTO part_numbers_graph
    SELECT agtype(format('{"id": %L, "label": "SSN", "properties": {"name": %L}}', SSN, SSN)::jsonb)
    FROM serial_table;

I get the following error:

relation "part_numbers_graph" does not exist

I have ran the command:

SELECT * FROM ag_catalog.ag_graph;

I can see that the graph is there.

 graphid |        name        |     namespace
---------+--------------------+--------------------
32924 | part_numbers_graph | part_numbers_graph

I have succesfully been able to run both

CREATE EXTENSION age;

and

LOAD 'age';

When I run

pg_config

I can see that I have installed
VERSION = PostgreSQL 12.14 (Ubuntu 12.14-1.pgdg22.04+1)

答案1

得分: 1

以下是已翻译的部分:

有一个与图的命名有关的问题。通常,按照最佳实践,应在图名之前添加模式名,以确保您正在使用正确的命名方案。以下是您可以编写查询的示例:

INSERT INTO part_numbers_graph.part_numbers_graph
SELECT agtype(format('{"id": %L, "label": "SSN", "properties": {"name": %L}}', SSN, SSN)::jsonb)
FROM serial_table;
英文:

There is an issue with the naming of the graph. Generally, as best practice the schema name is prepended before the graph name so make sure you are using the correct naming scheme. Here's an example of how you could write the query:

INSERT INTO part_numbers_graph.part_numbers_graph
SELECT agtype(format('{"id": %L, "label": "SSN", "properties": {"name": %L}}', SSN, SSN)::jsonb)
FROM serial_table;

答案2

得分: 0

part_numbers_graph不是普通的Postgres表,而是一个以顶点和边的形式存储数据的图表,需要使用cypher查询插入数据。有关cypher查询的更多信息,请参见官方Apache AGE文档

对于您的特定示例,cypher查询看起来可能是这样的:

SELECT * FROM cypher('part_numbers_graph', $$
CREATE (n:SSN {id: 'SSN' , name: 'SSN' }) 
$$) as (a agtype);
英文:

part_numbers_graph is not a regular Postgres table, but a graph table which stores data in the form of vertices and edges and requires data to be inserted using cypher queries.
For more information above cypher queries, see the official Apache AGE documentation.

For your particular example, a cypher query would look something like:

SELECT * FROM cypher('part_numbers_graph', $$
CREATE (n:SSN {id: 'SSN' , name: 'SSN' }) 
$$) as (a agtype);

答案3

得分: 0

插入值到图中应使用Cypher函数。例如,在您的情况下,查询将类似于以下内容:

SELECT * FROM cypher('part_numbers_graph', $$ 
CREATE (u:SSN {"id": "<insert_id>", "name": "<insert_name>"})
RETURN u $$) AS (u agtype);

使用此查询,您将创建一个带有SSN标签的节点,具有2个属性字段idname。但是,年龄会以其自己的方式创建id,所以也许您不必在查询中硬编码自己的id,只需让年龄跟踪不同节点和边的id

英文:

To insert values into a graph you should use the cypher function.
For example in your case the query will look something like this :

SELECT * FROM cypher(&#39;part_numbers_graph&#39;, $$ 
CREATE (u:SSN {&quot;id&quot;: &quot;&lt;insert_id&gt;&quot;, &quot;name&quot;: &quot;&lt;insert_name&gt;&quot;&quot;})
RETURN u $$) AS (u agtype);

With this query you create a node that has SSN label and has 2 property fields id and name. Age has its own way to create id&#39;s though so maybe you don't have to actually hard code your own id in the query and just let age keep track of the id&#39;s of the different nodes and edges.

答案4

得分: 0

虽然您已成功创建了图表,但错误提示“关系 'part_number_graph' 不存在”表明它在当前模式中不存在。我建议您在插入查询之前创建上述表格:
创建表格:

    CREATE TABLE part_numbers_graph (
  id bigserial primary key,
  label text,
  properties jsonb
);

现在,使用插入查询:

INSERT INTO part_numbers_graph
SELECT agtype(format('{"id": %L, "label": "SSN", "properties": {"name": %L}}', SSN, SSN)::jsonb)
FROM serial_table;

希望这有所帮助!

英文:

Although you have successfully created the graph, the error stating "relation 'part_number_graph' does not exist suggest that it is not present in the current scchema. What I suggest is that you should create the above-mentioned table before insert query:
Creating a table:

    CREATE TABLE part_numbers_graph (
  id bigserial primary key,
  label text,
  properties jsonb
);

Now, use the insert query:

INSERT INTO part_numbers_graph
    SELECT agtype(format(&#39;{&quot;id&quot;: %L, &quot;label&quot;: &quot;SSN&quot;, &quot;properties&quot;: {&quot;name&quot;: %L}}&#39;, SSN, SSN)::jsonb)
    FROM serial_table;

Hope it helped!

答案5

得分: 0

你可能首先要检查关系表是否存在,以及是否正确引用它,因为错误显示它不存在。反斜杠命令\dt可用于检查您拥有的现有关系列表。

除了这里已经建议的解决方案,您还可以使用预处理语句或pgsql函数将数据从表中导入到图形中,您可以在这里了解更多信息。

英文:

You might want to first check if the relational table exists and that you are referencing it correctly as the error shows that it does not exist. The backslash command \dt can be used to check the existing list of relations you have.

Adding to the solutions already suggested here, you can also use prepared statements or pgsql functions to import data from a table into a graph, which you can read more about here

答案6

得分: 0

你不能像插入普通的Postgres表一样将数据插入part_numbers_graph中,而是应该使用AGE扩展提供的Cypher函数。

因此,你的查询应该修改如下:

SELECT * FROM cypher('part_numbers_graph', $$
MERGE (u:SSN {id: 'your_id', name: 'your_name' })
$$) as (res agtype);

这个查询将创建一个具有标签SSN和两个属性idname的新顶点。

如果该顶点已存在,它将被更新而不是创建新的,因为我在这里使用的是MERGE

如果你希望每次都创建一个新的顶点,即使它是重复的,你可以使用CREATE

英文:

You can't insert in the part_numbers_graph as inserting in a normal postgres table instead you should use the cypher function that is offered by the AGE extension.

So your query should be modified as follows:

SELECT * FROM cypher(&#39;part_numbers_graph&#39;, $$
MERGE (u:SSN {id: &#39;your_id&#39; , name: &#39;your_name&#39; }) 
$$) as (res agtype);

So this query will create a new vertex with label SSN and with two properties : id and name.

If the vertex already exists the vertex will be updated instead of creating new one because here I am using MERGE.

If you want to create a new vertex every time even if it is a duplicate you can use CREATE.

答案7

得分: 0

  1. CREATE EXTENSION age;

  2. LOAD 'age';

  3. SELECT * FROM ag_catalog.ag_graph;

CREATE TABLE part_numbers_graph (
  id bigserial primary key,
  label text,
  properties jsonb
);
  1. SELECT * FROM ag_catalog.ag_graph;

INSERT INTO part_numbers_graph
SELECT agtype(format('{"id": %L, "label": "SSN", "properties": {"name": %L}}', SSN, SSN)::jsonb)
FROM serial_table;
英文:

1.CREATE EXTENSION age&#39;;

2.LOAD &#39;age&#39;;

  1. SELECT * FROM ag_catalog.ag_graph;

4.

     CREATE TABLE part_numbers_graph (
  id bigserial primary key,
  label text,
  properties jsonb
);
  1. SELECT * FROM ag_catalog.ag_graph;
    6.

    INSERT INTO part_numbers_graph
    SELECT agtype(format('{"id": %L, "label": "SSN", "properties": {"name": %L}}', SSN, SSN)::jsonb)
    FROM serial_table;

答案8

得分: 0

part_numbers_graph 不是典型的Postgres表。相反,它是一个以顶点和边为数据的图表。您需要使用名为“cypher查询”的特殊命令来放入数据。

SELECT * FROM cypher('part_numbers_graph', $$
CREATE (u:SSN {"id": "<insert_id>", "name": "<insert_name>"})
RETURN u $$) AS (u agtype);

英文:

The part_numbers_graph isn't a typical Postgres table. Instead, it's a graph table that holds data as vertices and edges. You need to use special commands called 'cypher queries' to put data into it.

    SELECT * FROM cypher(&#39;part_numbers_graph&#39;, $$ 
    CREATE (u:SSN {&quot;id&quot;: &quot;&lt;insert_id&gt;&quot;, &quot;name&quot;: &quot;&lt;insert_name&gt;&quot;&quot;})
    RETURN u $$) AS (u agtype);

答案9

得分: -1

你可以使用cypher()函数和编写Cypher查询来定义你的图结构。

SELECT * FROM cypher('part_numbers_graph', $$
CREATE (u:SSN {"id": 12345, "name": "John"})
RETURN u $$) AS (u agtype);

在这个查询中,你正在创建一个带有标签"SSN"和两个属性字段" id "和" name "的节点。

我希望这对你有帮助。

英文:

You can use the cypher() function and write Cypher queries to define your graph structure.

SELECT * FROM cypher(&#39;part_numbers_graph&#39;, $$ 
CREATE (u:SSN {&quot;id&quot;: 12345, &quot;name&quot;: &quot;John&quot;})
RETURN u $$) AS (u agtype);

In this query, you are creating a node with the label "SSN" and two property fields: "id" and "name".

I hope this would help.

huangapple
  • 本文由 发表于 2023年5月17日 16:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定