mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-17 09:11:03 +02:00
Write and query working on triples
This commit is contained in:
parent
44b1d43989
commit
feac569cd1
7 changed files with 57 additions and 31 deletions
|
|
@ -4,10 +4,16 @@ from cassandra.auth import PlainTextAuthProvider
|
||||||
|
|
||||||
class TrustGraph:
|
class TrustGraph:
|
||||||
|
|
||||||
def __init__(self, hosts=None):
|
def __init__(
|
||||||
|
self, hosts=None,
|
||||||
|
keyspace="trustgraph", table="default",
|
||||||
|
):
|
||||||
|
|
||||||
if hosts is None:
|
if hosts is None:
|
||||||
hosts = ["localhost"]
|
hosts = ["localhost"]
|
||||||
|
|
||||||
|
self.keyspace = keyspace
|
||||||
|
self.table = table
|
||||||
|
|
||||||
self.cluster = Cluster(hosts)
|
self.cluster = Cluster(hosts)
|
||||||
self.session = self.cluster.connect()
|
self.session = self.cluster.connect()
|
||||||
|
|
@ -16,26 +22,26 @@ class TrustGraph:
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
|
|
||||||
self.session.execute("""
|
self.session.execute(f"""
|
||||||
drop keyspace if exists trustgraph;
|
drop keyspace if exists {self.keyspace};
|
||||||
""");
|
""");
|
||||||
|
|
||||||
self.init()
|
self.init()
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
|
|
||||||
self.session.execute("""
|
self.session.execute(f"""
|
||||||
create keyspace if not exists trustgraph
|
create keyspace if not exists {self.keyspace}
|
||||||
with replication = {
|
with replication = {{
|
||||||
'class' : 'SimpleStrategy',
|
'class' : 'SimpleStrategy',
|
||||||
'replication_factor' : 1
|
'replication_factor' : 1
|
||||||
};
|
}};
|
||||||
""");
|
""");
|
||||||
|
|
||||||
self.session.set_keyspace('trustgraph')
|
self.session.set_keyspace(self.keyspace)
|
||||||
|
|
||||||
self.session.execute("""
|
self.session.execute(f"""
|
||||||
create table if not exists triples (
|
create table if not exists {self.table} (
|
||||||
s text,
|
s text,
|
||||||
p text,
|
p text,
|
||||||
o text,
|
o text,
|
||||||
|
|
@ -43,66 +49,66 @@ class TrustGraph:
|
||||||
);
|
);
|
||||||
""");
|
""");
|
||||||
|
|
||||||
self.session.execute("""
|
self.session.execute(f"""
|
||||||
create index if not exists triples_p
|
create index if not exists {self.table}_p
|
||||||
ON triples (p);
|
ON {self.table} (p);
|
||||||
""");
|
""");
|
||||||
|
|
||||||
self.session.execute("""
|
self.session.execute(f"""
|
||||||
create index if not exists triples_o
|
create index if not exists {self.table}_o
|
||||||
ON triples (o);
|
ON {self.table} (o);
|
||||||
""");
|
""");
|
||||||
|
|
||||||
def insert(self, s, p, o):
|
def insert(self, s, p, o):
|
||||||
|
|
||||||
self.session.execute(
|
self.session.execute(
|
||||||
"insert into triples (s, p, o) values (%s, %s, %s)",
|
f"insert into {self.table} (s, p, o) values (%s, %s, %s)",
|
||||||
(s, p, o)
|
(s, p, o)
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_all(self, limit=50):
|
def get_all(self, limit=50):
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
f"select s, p, o from triples limit {limit}"
|
f"select s, p, o from {self.table} limit {limit}"
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_s(self, s, limit=10):
|
def get_s(self, s, limit=10):
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
f"select p, o from triples where s = %s limit {limit}",
|
f"select p, o from {self.table} where s = %s limit {limit}",
|
||||||
(s,)
|
(s,)
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_p(self, p, limit=10):
|
def get_p(self, p, limit=10):
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
f"select s, o from triples where p = %s limit {limit}",
|
f"select s, o from {self.table} where p = %s limit {limit}",
|
||||||
(p,)
|
(p,)
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_o(self, o, limit=10):
|
def get_o(self, o, limit=10):
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
f"select s, p from triples where o = %s limit {limit}",
|
f"select s, p from {self.table} where o = %s limit {limit}",
|
||||||
(o,)
|
(o,)
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_sp(self, s, p, limit=10):
|
def get_sp(self, s, p, limit=10):
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
f"select o from triples where s = %s and p = %s limit {limit}",
|
f"select o from {self.table} where s = %s and p = %s limit {limit}",
|
||||||
(s, p)
|
(s, p)
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_po(self, p, o, limit=10):
|
def get_po(self, p, o, limit=10):
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
f"select s from triples where p = %s and o = %s allow filtering limit {limit}",
|
f"select s from {self.table} where p = %s and o = %s allow filtering limit {limit}",
|
||||||
(p, o)
|
(p, o)
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_os(self, o, s, limit=10):
|
def get_os(self, o, s, limit=10):
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
f"select p from triples where o = %s and s = %s limit {limit}",
|
f"select p from {self.table} where o = %s and s = %s limit {limit}",
|
||||||
(o, s)
|
(o, s)
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_spo(self, s, p, o, limit=10):
|
def get_spo(self, s, p, o, limit=10):
|
||||||
return self.session.execute(
|
return self.session.execute(
|
||||||
f"""select s as x from triples where s = %s and p = %s and o = %s limit {limit}""",
|
f"""select s as x from {self.table} where s = %s and p = %s and o = %s limit {limit}""",
|
||||||
(s, p, o)
|
(s, p, o)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ class Processor(ConsumerProducer):
|
||||||
|
|
||||||
dim = len(vec)
|
dim = len(vec)
|
||||||
collection = (
|
collection = (
|
||||||
"d_" + v.user + "_" + v.collection +
|
"d_" + v.user + "_" + v.collection + "_" +
|
||||||
str(dim)
|
str(dim)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ class Processor(ConsumerProducer):
|
||||||
|
|
||||||
dim = len(vec)
|
dim = len(vec)
|
||||||
collection = (
|
collection = (
|
||||||
"t_" + v.user + "_" + v.collection +
|
"t_" + v.user + "_" + v.collection + "_" +
|
||||||
str(dim)
|
str(dim)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,8 @@ class Processor(ConsumerProducer):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
self.tg = TrustGraph([graph_host])
|
self.graph_host = [graph_host]
|
||||||
|
self.table = None
|
||||||
|
|
||||||
def create_value(self, ent):
|
def create_value(self, ent):
|
||||||
if ent.startswith("http://") or ent.startswith("https://"):
|
if ent.startswith("http://") or ent.startswith("https://"):
|
||||||
|
|
@ -52,6 +53,15 @@ class Processor(ConsumerProducer):
|
||||||
|
|
||||||
v = msg.value()
|
v = msg.value()
|
||||||
|
|
||||||
|
table = (v.user, v.collection)
|
||||||
|
|
||||||
|
if table != self.table:
|
||||||
|
self.tg = TrustGraph(
|
||||||
|
hosts=self.graph_host,
|
||||||
|
keyspace=v.user, table=v.collection,
|
||||||
|
)
|
||||||
|
self.table = table
|
||||||
|
|
||||||
# Sender-produced ID
|
# Sender-produced ID
|
||||||
id = msg.properties()["id"]
|
id = msg.properties()["id"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ class Processor(Consumer):
|
||||||
|
|
||||||
dim = len(vec)
|
dim = len(vec)
|
||||||
collection = (
|
collection = (
|
||||||
"d_" + v.metadata.user + "_" + v.metadata.collection +
|
"d_" + v.metadata.user + "_" + v.metadata.collection + "_" +
|
||||||
str(dim)
|
str(dim)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ class Processor(Consumer):
|
||||||
|
|
||||||
dim = len(vec)
|
dim = len(vec)
|
||||||
collection = (
|
collection = (
|
||||||
"t_" + v.metadata.user + "_" + v.metadata.collection +
|
"t_" + v.metadata.user + "_" + v.metadata.collection + "_" +
|
||||||
str(dim)
|
str(dim)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,12 +38,22 @@ class Processor(Consumer):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
self.tg = TrustGraph([graph_host])
|
self.graph_host = [graph_host]
|
||||||
|
self.table = None
|
||||||
|
|
||||||
def handle(self, msg):
|
def handle(self, msg):
|
||||||
|
|
||||||
v = msg.value()
|
v = msg.value()
|
||||||
|
|
||||||
|
table = (v.metadata.user, v.metadata.collection)
|
||||||
|
|
||||||
|
if self.table is None or self.table != self.table:
|
||||||
|
self.tg = TrustGraph(
|
||||||
|
hosts=self.graph_host,
|
||||||
|
keyspace=v.metadata.user, table=v.metadata.collection,
|
||||||
|
)
|
||||||
|
self.table = table
|
||||||
|
|
||||||
self.tg.insert(
|
self.tg.insert(
|
||||||
v.s.value,
|
v.s.value,
|
||||||
v.p.value,
|
v.p.value,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue