Python 调用 ES、Solr、Phoenix

1 1 #!/usr/bin/env python 2 2 # -*- coding:utf-8 -*- 3 3 # ************************************* 4 4 # @Time : 2019/8/12 5 5 # @Author : Zhang Fan 6 6 # @Desc : Library 7 7 # @File : MyDatabases.py 8 8 # @Update : 2019/8/23 9 9 # ************************************* 10 10 import elasticsearch 11 11 import phoenixdb 12 12 import pysolr 13 13 import pymysql 14 14 15 15 16 16 class MyELS(object): 17 17 """ 18 18 =================================================================== 19 19 ===================== MyELS ========================= 20 20 =================================================================== 21 21 """ 22 22 def __init__(self): 23 23 self.els_conn = None 24 24 25 25 def connect_to_els(self, host, port): 26 26 """ 27 27 连接到ElasticSearch服务器. 28 28 """ 29 29 self.els_conn = elasticsearch.Elasticsearch([{'host': host, 'port': port}]) 30 30 print('Executing : Connect To Elastic Search | %s' % self.els_conn) 31 31 32 32 def get_els_data(self, query, index): 33 33 """ 34 34 获取ElasticSearch数据 35 35 """ 36 36 print('Executing : Search | %s' % query) 37 37 try: 38 38 rst = self.els_conn.search(index=index, q=query) 39 39 return rst['hits'] 40 40 except Exception as e: 41 41 print('Elastic Search Error | %s' % e) 42 42 raise Exception(e) 43 43 44 44 45 45 class MyPhoenix(object): 46 46 """ 47 47 =================================================================== 48 48 ===================== MyPhoenix ====================== 49 49 =================================================================== 50 50 """ 51 51 def __init__(self): 52 52 self.phoenix_conn = None 53 53 self.phoenix_cursor = None 54 54 55 55 def connect_to_phoenix(self, host, port=8765): 56 56 """ 57 57 连接到phoenix服务器 58 58 """ 59 59 address = 'http://{0}:{1}/'.format(host, port) 60 60 print('Executing : Connect To Phoenix | %s' % address) 61 61 self.phoenix_conn = phoenixdb.connect(address, autocommit=True) 62 62 self.phoenix_cursor = self.phoenix_conn.cursor() 63 63 64 64 def set_schema(self, sql, schema): 65 65 """ 66 66 设置schema 67 67 """ 68 68 pre_sub, sub, fol_sub = sql.upper().partition('FROM') 69 69 fol_sub = ' ' + schema + '.' + fol_sub.strip() 70 70 new_sql = ''.join([pre_sub, sub, fol_sub]) 71 71 return new_sql 72 72 73 73 def execute_phoenix_sql(self, sql): 74 74 """ 75 75 执行sql语句 76 76 """ 77 77 # sql = self.set_schema(sql, schema) 78 78 print('Executing : Execute | %s' % sql) 79 79 self.phoenix_cursor.execute(sql) 80 80 81 81 def get_from_phoenix(self, sql): 82 82 """ 83 83 获取phoenix数据 84 84 """ 85 85 # sql = self.set_schema(sql, schema) 86 86 print('Executing : Query | %s' % sql) 87 87 try: 88 88 self.phoenix_cursor.execute(sql) 89 89 except Exception as e: 90 90 print('Phoenix Error | %s' % e) 91 91 raise Exception(e) 92 92 return self.phoenix_cursor.fetchall() 93 93 94 94 def disconnect_from_phoenix(self): 95 95 """ 96 96 断开phoenix连接 97 97 """ 98 98 print('Executing : Disconnect From HBase') 99 99 self.phoenix_cursor.close() 100100 self.phoenix_conn.close() 101101 102102 103103 class MySolr(object): 104104 """ 105105 =================================================================== 106106 ===================== MySolr ========================= 107107 =================================================================== 108108 """ 109109 def __init__(self): 110110 self.solr_conn = None 111111 self.base_url = None 112112 113113 def connect_to_solr(self, address, selector): 114114 """连接到solr服务器. 115115 """ 116116 self.base_url = 'http://{0}/solr/{1}/'.format(address, selector) 117117 self.solr_conn = pysolr.Solr(self.base_url) 118118 print('Executing : Connect To Solr | %s' % self.base_url) 119119 120120 def get_solr_data(self, query): 121121 """ 122122 获取solr数据 123123 """ 124124 results = list() 125125 print('Executing : Search | %s' % query) 126126 try: 127127 items = self.solr_conn.search(query) 128128 for item in items: 129129 results.append(item) 130130 except Exception as e: 131131 print('Solr Error | %s' % e) 132132 raise Exception(e) 133133 return results 134134 135135 def add_solr_data(self, data): 136136 """ 137137 添加solr数据 138138 """ 139139 print('Executing : add | %s' % data) 140140 try: 141141 self.solr_conn.add([data]) 142142 self.solr_conn.commit() 143143 except Exception as e: 144144 print('Solr Error | %s' % e) 145145 raise Exception(e) 146146 147147 def del_solr_byId(self, data): 148148 """ 149149 删除solr数据 150150 """ 151151 print('Executing : del | %s' % data) 152152 try: 153153 self.solr_conn.delete(id=data) 154154 self.solr_conn.commit() 155155 except Exception as e: 156156 print('Solr Error | %s' % e) 157157 raise Exception(e) 158158 159159 160160 if __name__ == '__main__': 161161 print('This is test.') 162162 ms = MySolr() 163163 me = MyELS() 164164 mp = MyPhoenix()
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Python 调用 ES、Solr、Phoenix - HelloWorld