From 4df8b3b52e01168ee6c510b2a951fa9703cb4de4 Mon Sep 17 00:00:00 2001 From: zwc Date: Fri, 1 Dec 2017 16:50:57 +0800 Subject: [PATCH] custom primary key --- README.md | 2 +- dite/__init__.py | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e978400..99025bc 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ CREATE FOREIGN TABLE articles_es ( id bigint, title text, content text -) SERVER multicorn_es OPTIONS (host '127.0.0.1', port '9200', node 'test', index 'articles'); +) SERVER multicorn_es OPTIONS (host '127.0.0.1', port '9200', node 'test', index 'articles', primary_key 'id'); diff --git a/dite/__init__.py b/dite/__init__.py index 66e7464..0e8fc5f 100644 --- a/dite/__init__.py +++ b/dite/__init__.py @@ -5,8 +5,6 @@ from multicorn import ForeignDataWrapper from multicorn.utils import log_to_postgres as log2pg -from functools import partial - import httplib import json import logging @@ -20,6 +18,7 @@ def __init__(self, options, columns): self.port = int(options.get('port', '9200')) self.node = options.get('node', '') self.index = options.get('index', '') + self._row_id_column = options.get('primary_key', columns.keys()[0]) self.columns = columns @@ -52,7 +51,7 @@ def execute(self, quals, columns): for hit in data['hits']['hits']: row = {} for col in columns: - if col == 'id': + if col == self.rowid_column: row[col] = hit['_id'] elif col in hit['_source']: row[col] = hit['_source'][col] @@ -66,7 +65,7 @@ def rowid_column(self): This column name should be subsequently present in every returned resultset. """ - return 'id'; + return self._row_id_column def es_index(self, id, values): content = json.dumps(values) @@ -85,15 +84,14 @@ def es_index(self, id, values): def insert(self, new_values): log2pg('MARK Insert Request - new values: %s' % new_values, logging.DEBUG) - if not 'id' in new_values: - log2pg('INSERT requires "id" column. Missing in: %s' % new_values, logging.ERROR) + if not self.rowid_column in new_values: + log2pg('INSERT requires "%s" column. Missing in: %s' % (self.rowid_colum, new_values), logging.ERROR) - id = new_values['id'] - new_values.pop('id', None) + id = new_values.pop(self.rowid_column) return self.es_index(id, new_values) def update(self, id, new_values): - new_values.pop('id', None) + new_values.pop(self.rowid_column, None) return self.es_index(id, new_values) def delete(self, id):