|
| 1 | +# cli.py |
| 2 | +# Author: Thomas MINIER - MIT License 2019 |
| 3 | +import happybase |
| 4 | +import click |
| 5 | +import coloredlogs |
| 6 | +import logging |
| 7 | + |
| 8 | +from sys import exit |
| 9 | +from os.path import isfile |
| 10 | +from yaml import load |
| 11 | +from hdt import HDTDocument |
| 12 | +from time import time |
| 13 | +from sage.database.hbase.utils import build_row_key |
| 14 | +from sage.cli.utils import load_graph, get_nb_triples |
| 15 | +from sage.cli.parsers import ParserFactory, ParseError |
| 16 | + |
| 17 | +coloredlogs.install(level='INFO', fmt='%(asctime)s - %(levelname)s %(message)s') |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +@click.command() |
| 22 | +@click.argument("config") |
| 23 | +@click.argument("graph_name") |
| 24 | +def init_hbase(config, graph_name): |
| 25 | + """ |
| 26 | + Initialize the RDF Graph DATASET_NAME with a Apache HBase backend, described in the configuration file CONFIG. |
| 27 | + """ |
| 28 | + # load graph from config file |
| 29 | + graph, kind = load_graph(config, graph_name, logger, backends=['hbase']) |
| 30 | + |
| 31 | + thrift_port = graph['thrift_port'] if 'thrift_port' in graph else 9090 |
| 32 | + |
| 33 | + logger.info("Connexion to the HBase server...") |
| 34 | + connection = happybase.Connection(graph['thrift_host'], protocol="compact", transport="framed", port=thrift_port, table_prefix=graph_name) |
| 35 | + logger.info("Connected to the HBase server !") |
| 36 | + |
| 37 | + # create HBase tables |
| 38 | + families = {'rdf': dict()} |
| 39 | + logger.info("Creating HBase tables for RDF Graph '{}'...".format(graph_name)) |
| 40 | + connection.create_table('spo', families) |
| 41 | + connection.create_table('pos', families) |
| 42 | + connection.create_table('osp', families) |
| 43 | + logger.info("RDF Graph '{}' successfully created in HBase".format(graph_name)) |
| 44 | + connection.close() |
| 45 | + |
| 46 | + |
| 47 | +@click.command() |
| 48 | +@click.argument("rdf_file") |
| 49 | +@click.argument("config") |
| 50 | +@click.argument("graph_name") |
| 51 | +@click.option("-f", "--format", type=click.Choice(["nt", "hdt"]), |
| 52 | + default="nt", show_default=True, |
| 53 | + help="Format of the input file. Supported: nt (N-triples) and hdt (HDT).") |
| 54 | +@click.option("-b", "--batch-size", type=int, default=1000, show_default=True, |
| 55 | + help="Batch size used for batch loading") |
| 56 | +def put_hbase(rdf_file, config, graph_name, format, batch_size): |
| 57 | + """ |
| 58 | + Insert RDF triples from HDT file HDT_FILE into the RDF Graph graph_name, described in the configuration file CONFIG. The dataset must use the Apache HBase backend. |
| 59 | + """ |
| 60 | + # load graph from config file |
| 61 | + graph, kind = load_graph(config, graph_name, logger, backends=['hbase']) |
| 62 | + |
| 63 | + thrift_port = graph['thrift_port'] if 'thrift_port' in graph else 9090 |
| 64 | + |
| 65 | + logger.info("Connexion to the HBase server...") |
| 66 | + connection = happybase.Connection(graph['thrift_host'], protocol="compact", transport="framed", port=thrift_port, table_prefix=graph_name) |
| 67 | + logger.info("Connected to the HBase server !") |
| 68 | + |
| 69 | + spo_batch = connection.table('spo').batch(batch_size=batch_size) |
| 70 | + pos_batch = connection.table('pos').batch(batch_size=batch_size) |
| 71 | + osp_batch = connection.table('osp').batch(batch_size=batch_size) |
| 72 | + |
| 73 | + logger.info("Reading RDF source file...") |
| 74 | + nb_triples = get_nb_triples(rdf_file, format) |
| 75 | + logger.info(f"Found ~{nb_triples} RDF triples to ingest.") |
| 76 | + |
| 77 | + start = time() |
| 78 | + inserted = 0 |
| 79 | + dropped = 0 |
| 80 | + |
| 81 | + with click.progressbar(length=nb_triples, label=f"Inserting RDF triples 0/{nb_triples} - {dropped} triples dropped.") as bar: |
| 82 | + |
| 83 | + def on_bucket(bucket): |
| 84 | + nonlocal inserted, dropped |
| 85 | + for (s, p, o) in bucket: |
| 86 | + columns = { |
| 87 | + b'rdf:subject': s.encode('utf-8'), |
| 88 | + b'rdf:predicate': p.encode('utf-8'), |
| 89 | + b'rdf:object': o.encode('utf-8') |
| 90 | + } |
| 91 | + spo_key = build_row_key(s, p, o) |
| 92 | + pos_key = build_row_key(p, o, s) |
| 93 | + osp_key = build_row_key(o, s, p) |
| 94 | + spo_batch.put(spo_key, columns) |
| 95 | + pos_batch.put(pos_key, columns) |
| 96 | + osp_batch.put(osp_key, columns) |
| 97 | + inserted = inserted + len(bucket) |
| 98 | + bar.label = f"Inserting RDF triples {inserted}/{nb_triples} - {dropped} triples dropped." |
| 99 | + bar.update(len(bucket)) |
| 100 | + |
| 101 | + def on_error(error): |
| 102 | + nonlocal dropped, inserted |
| 103 | + if isinstance(error, ParseError): |
| 104 | + logger.warning(error) |
| 105 | + dropped = dropped + 1 |
| 106 | + bar.label = f"Inserting RDF triples {inserted}/{nb_triples} - {dropped} triples dropped." |
| 107 | + bar.update(0) |
| 108 | + else: |
| 109 | + logger.error(error) |
| 110 | + exit(1) |
| 111 | + |
| 112 | + def on_complete(): |
| 113 | + nonlocal start |
| 114 | + # send last batch |
| 115 | + spo_batch.send() |
| 116 | + pos_batch.send() |
| 117 | + osp_batch.send() |
| 118 | + logger.info(f"RDF triples ingestion successfully completed in {time() - start}s") |
| 119 | + logger.info("Committing and cleaning up...") |
| 120 | + connection.close() |
| 121 | + logger.info(f"RDF data from file '{rdf_file}' successfully inserted into RDF graph '{graph_name}'") |
| 122 | + |
| 123 | + logger.info("Starting RDF triples ingestion...") |
| 124 | + parser = ParserFactory.create_parser(format, batch_size) |
| 125 | + parser.on_bucket = on_bucket |
| 126 | + parser.on_error = on_error |
| 127 | + parser.on_complete = on_complete |
| 128 | + parser.parsefile(rdf_file) |
0 commit comments