Hello. I am probably doing many things wrong on my baby steps in trying to load from sql to ES using python. I have some syntax error if you can help pls?
import pyodbc
from elasticsearch import Elasticsearch,helpers
from elasticsearch.helpers import bulk, streaming_bulk
ES_HOST = {"host" : "localhost", "port" : 9200}
INDEX_NAME = 'aaa'
TYPE_NAME = 'bbb'
server = 'xxx'
database = 'xxx'
username = 'xxx'
password = 'xxx'
driver= '{ODBC Driver 13 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("select top 10 a,b,c,d from table")
rows = cursor.fetchall()
bulk_data = []
for row in rows:
action = {
"index": {
"_index": INDEX_NAME,
"_type": TYPE_NAME,
"_source": {
"a":row.a,
"b":row.b,
"c":row.c,
"d":row.d
}
}
}
bulk_data.append(action)
es = Elasticsearch(hosts = [ES_HOST])
if es.indices.exists(INDEX_NAME):
res = es.indices.delete(index = INDEX_NAME)
request_body = {
"settings" : {
"number_of_shards": 1,
"number_of_replicas": 0
},
'mappings': {
'aaa_map': {
'properties': {
'a': {'type': 'text'},
'b': {'type': 'text'},
'c': {'type': 'integer'},
'd': {'type': 'integer'},
}}}
}
es.indices.create(index = INDEX_NAME, body = request_body)
es.bulk(index = INDEX_NAME, body = bulk_data)
es.search(index = INDEX_NAME, size=2, body={"query": {"match_all": {}}})