#!/usr/bin/env python
# -*- coding:utf-8 -*-
# *************************************
# @Time : 2019/8/12
# @Author : Zhang Fan
# @Desc : Library
# @File : MyDatabases.py
# @Update : 2019/8/23
# *************************************
import elasticsearch
import phoenixdb
import pysolr
import pymysql class MyELS(object):
"""
===================================================================
===================== MyELS =========================
===================================================================
"""
def __init__(self):
self.els_conn = None def connect_to_els(self, host, port):
"""
连接到ElasticSearch服务器.
"""
self.els_conn = elasticsearch.Elasticsearch([{'host': host, 'port': port}])
print('Executing : Connect To Elastic Search | %s' % self.els_conn) def get_els_data(self, query, index):
"""
获取ElasticSearch数据
"""
print('Executing : Search | %s' % query)
try:
rst = self.els_conn.search(index=index, q=query)
return rst['hits']
except Exception as e:
print('Elastic Search Error | %s' % e)
raise Exception(e) class MyPhoenix(object):
"""
===================================================================
===================== MyPhoenix ======================
===================================================================
"""
def __init__(self):
self.phoenix_conn = None
self.phoenix_cursor = None def connect_to_phoenix(self, host, port=8765):
"""
连接到phoenix服务器
"""
address = 'http://{0}:{1}/'.format(host, port)
print('Executing : Connect To Phoenix | %s' % address)
self.phoenix_conn = phoenixdb.connect(address, autocommit=True)
self.phoenix_cursor = self.phoenix_conn.cursor() def set_schema(self, sql, schema):
"""
设置schema
"""
pre_sub, sub, fol_sub = sql.upper().partition('FROM')
fol_sub = ' ' + schema + '.' + fol_sub.strip()
new_sql = ''.join([pre_sub, sub, fol_sub])
return new_sql def execute_phoenix_sql(self, sql):
"""
执行sql语句
"""
# sql = self.set_schema(sql, schema)
print('Executing : Execute | %s' % sql)
self.phoenix_cursor.execute(sql) def get_from_phoenix(self, sql):
"""
获取phoenix数据
"""
# sql = self.set_schema(sql, schema)
print('Executing : Query | %s' % sql)
try:
self.phoenix_cursor.execute(sql)
except Exception as e:
print('Phoenix Error | %s' % e)
raise Exception(e)
return self.phoenix_cursor.fetchall() def disconnect_from_phoenix(self):
"""
断开phoenix连接
"""
print('Executing : Disconnect From HBase')
self.phoenix_cursor.close()
self.phoenix_conn.close() class MySolr(object):
"""
===================================================================
===================== MySolr =========================
===================================================================
"""
def __init__(self):
self.solr_conn = None
self.base_url = None def connect_to_solr(self, address, selector):
"""连接到solr服务器.
"""
self.base_url = 'http://{0}/solr/{1}/'.format(address, selector)
self.solr_conn = pysolr.Solr(self.base_url)
print('Executing : Connect To Solr | %s' % self.base_url) def get_solr_data(self, query):
"""
获取solr数据
"""
results = list()
print('Executing : Search | %s' % query)
try:
items = self.solr_conn.search(query)
for item in items:
results.append(item)
except Exception as e:
print('Solr Error | %s' % e)
raise Exception(e)
return results def add_solr_data(self, data):
"""
添加solr数据
"""
print('Executing : add | %s' % data)
try:
self.solr_conn.add([data])
self.solr_conn.commit()
except Exception as e:
print('Solr Error | %s' % e)
raise Exception(e) def del_solr_byId(self, data):
"""
删除solr数据
"""
print('Executing : del | %s' % data)
try:
self.solr_conn.delete(id=data)
self.solr_conn.commit()
except Exception as e:
print('Solr Error | %s' % e)
raise Exception(e) if __name__ == '__main__':
print('This is test.')
ms = MySolr()
me = MyELS()
mp = MyPhoenix()

Python 调用 ES、Solr、Phoenix的更多相关文章

  1. python调用ggsci.exe程序

    需求:通过python调用windows server 2008下的ogg同步程序,实现图形化控制. 简单GUI

  2. 【初学python】使用python调用monkey测试

    目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...

  3. python调用py中rar的路径问题。

    1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...

  4. python调用其他程序或脚本方法(转)

    python运行(调用)其他程序或脚本 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地 ...

  5. python调用c\c++

    前言 python 这门语言,凭借着其极高的易学易用易读性和丰富的扩展带来的学习友好性和项目友好性,近年来迅速成为了越来越多的人们的首选.然而一旦拿python与传统的编程语言(C/C++)如来比较的 ...

  6. Python调用C++

    /***gcc -o libpycall.so -shared -fPIC pycall.c*/ #include <stdio.h> #include <stdlib.h> ...

  7. 使用Python调用Flickr API抓取图片数据

    Flickr是雅虎旗下的图片分享网站,上面有全世界网友分享的大量精彩图片,被认为是专业的图片网站.其API也很友好,可以实现多种功能.这里我使用了Python调用其API获得了大量的照片数据.需要注意 ...

  8. python调用zabbix接口实现Action配置

    要写这篇博客其实我的内心是纠结的,老实说,我对zabbix的了解实在不多.但新公司的需求不容置疑,当我顶着有两个头大的脑袋懵懵转入运维领域时,面前摆着两百多组.上千台机器等着写入zabbix监控的需求 ...

  9. linux下python调用c模块

    在C调用Python模块时需要初始化Python解释器,导入模块等,但Python调用C模块却比较简单,下面还是以helloWorld.c 和 main.py 做一说明:   (1)编写C代码,hel ...

随机推荐

  1. Ubuntu系统下arm-linux-gcc交叉编译环境搭建过程

    搭建所需环境Linux版本:Ubuntu 14.10 交叉编译器版本:arm-linux-gcc-4.4.3资源链接 何为交叉编译环境搭建交叉编译环境,即安装.配置交叉编译工具链.在Ubuntu环境下 ...

  2. ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze (分层dijkstra)

    There are NN cities in the country, and MMdirectional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). ...

  3. Django forms组件与钩子函数

    目录 一.多对多的三种创建方式 1. 全自动 2. 纯手撸(了解) 3. 半自动(强烈推荐) 二.forms组件 1. 如何使用forms组件 2. 使用forms组件校验数据 3. 使用forms组 ...

  4. 《Java练习题》习题集二

    编程合集: https://www.cnblogs.com/jssj/p/12002760.html Java总结:https://www.cnblogs.com/jssj/p/11146205.ht ...

  5. VS2019 开发Django(九)------内置模板和过滤器

    导航:VS2019开发Django系列 紧接上篇,继续介绍Django中的模板,考虑可能篇幅过长,所以分为两部分来讲,今天的主要内容: 1)内置模板和过滤器 母版,继承关系.头部导航和页脚,是需要与其 ...

  6. IT兄弟连 HTML5教程 响应式网站的内容设计

    基于响应式开发网站,除了页面的布局是我们设计的重点,网站中显示的图片和文字也是我们不能轻视的内容. 1  响应式图片显示内容设计 真正具有响应性的Web设计是完全调整网站以满足访问者的设备.我们需要在 ...

  7. 从《彩色圆环》一题探讨一类环上dp的解法

    清橙A1202 bzoj2201 bsoj4074 试题来源 2010中国国家集训队命题答辩 问题描述 小A喜欢收集宝物.一天他得到了一个圆环,圆环上有N颗彩色宝石,闪闪发光.小A很爱惜这个圆环,天天 ...

  8. linux系统centos7安装最新版本nginx

    一.准备环境 1.安装centos,一般买一个阿里云测试 2.下载nginx,链接http://nginx.org/download/nginx-1.10.2.tar.gz 二.开始安装 1.cent ...

  9. Python—执行系统命令的四种方法

    一.os.system方法 这个方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息. os.system(cmd)的返回值.如果执行成功,那么会 ...

  10. 分布式事务之解决方案(XA和2PC)

    3. 分布式事务解决方案之2PC(两阶段提交) 针对不同的分布式场景业界常见的解决方案有2PC.TCC.可靠消息最终一致性.最大努力通知这几种. 3.1. 什么是2PC 2PC即两阶段提交协议,是将整 ...