trustgraph/trustgraph-flow/trustgraph/storage/triples/cassandra/write.py

104 lines
2.6 KiB
Python
Raw Normal View History

2024-07-10 23:20:06 +01:00
"""
2024-07-12 15:12:40 +01:00
Graph writer. Input is graph edge. Writes edges to Cassandra graph.
2024-07-10 23:20:06 +01:00
"""
import pulsar
import base64
import os
import argparse
import time
from .... direct.cassandra import TrustGraph
from .... base import TriplesStoreService
2024-07-10 23:20:06 +01:00
default_ident = "triples-write"
2024-07-15 17:17:04 +01:00
default_graph_host='localhost'
class Processor(TriplesStoreService):
2024-07-10 23:20:06 +01:00
def __init__(self, **params):
id = params.get("id", default_ident)
graph_host = params.get("graph_host", default_graph_host)
graph_username = params.get("graph_username", None)
graph_password = params.get("graph_password", None)
2024-07-10 23:20:06 +01:00
super(Processor, self).__init__(
**params | {
"graph_host": graph_host,
"graph_username": graph_username
}
2024-07-10 23:20:06 +01:00
)
self.graph_host = [graph_host]
self.username = graph_username
self.password = graph_password
self.table = None
2024-07-10 23:20:06 +01:00
async def store_triples(self, message):
2024-07-10 23:20:06 +01:00
table = (message.metadata.user, message.metadata.collection)
if self.table is None or self.table != table:
self.tg = None
try:
if self.username and self.password:
self.tg = TrustGraph(
hosts=self.graph_host,
keyspace=message.metadata.user,
table=message.metadata.collection,
username=self.username, password=self.password
)
else:
self.tg = TrustGraph(
hosts=self.graph_host,
keyspace=message.metadata.user,
table=message.metadata.collection,
)
except Exception as e:
print("Exception", e, flush=True)
time.sleep(1)
raise e
self.table = table
for t in message.triples:
self.tg.insert(
t.s.value,
t.p.value,
t.o.value
)
2024-07-10 23:20:06 +01:00
@staticmethod
def add_args(parser):
2024-07-10 23:20:06 +01:00
TriplesStoreService.add_args(parser)
2024-07-15 17:17:04 +01:00
parser.add_argument(
'-g', '--graph-host',
default="localhost",
help=f'Graph host (default: localhost)'
)
parser.add_argument(
'--graph-username',
default=None,
help=f'Cassandra username'
)
parser.add_argument(
'--graph-password',
default=None,
help=f'Cassandra password'
)
2024-07-10 23:20:06 +01:00
def run():
Processor.launch(default_ident, __doc__)
2024-07-10 23:20:06 +01:00