source.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #encoding:UTF8
  2. from BaseDataMaintenance.dataSource.setttings import *
  3. import requests
  4. import json
  5. import pymysql
  6. import pymongo
  7. import tablestore
  8. import oss2
  9. import redis
  10. def solrQuery(collection,args):
  11. if collection in solr_collections:
  12. _arg = ""
  13. for k,v in args.items():
  14. _arg += "&%s=%s"%(k,v)
  15. _arg = _arg[1:]
  16. url = "%s%s/select?%s"%(solr_collections[collection],collection,_arg)
  17. resp = requests.get(url)
  18. if resp.status_code==200:
  19. return json.loads(resp.content.decode())
  20. return None
  21. def solrQuery_url(url):
  22. resp = requests.get(url)
  23. if resp.status_code==200:
  24. return json.loads(resp.content.decode())
  25. return None
  26. def getConnection_mysql(db=None):
  27. if db is None:
  28. db = mysql_db
  29. connect = pymysql.Connect(host=mysql_host, port=mysql_port, db=db, user=mysql_user, passwd=mysql_pass)
  30. return connect
  31. def getConnection_testmysql(db=None):
  32. if db is None:
  33. db = test_mysql_db
  34. connect = pymysql.Connect(host=test_mysql_host, port=test_mysql_port, db=db, user=test_mysql_user, passwd=test_mysql_pass)
  35. return connect
  36. def getConnection_oracle():
  37. import cx_Oracle
  38. connect = cx_Oracle.connect(oracle_user,oracle_pass,'%s:%s/%s'%(oracle_host,oracle_port,oracle_db), encoding = "UTF-8", nencoding = "UTF-8")
  39. # connect = cx_Oracle.connect('%s/%s@%s:%s/%s'%(oracle_user,oracle_pass,oracle_host,oracle_port,oracle_db))
  40. return connect
  41. def getConnect_mongodb():
  42. client = pymongo.MongoClient(mongo_host,mongo_port)
  43. db = client[mongo_db]
  44. db.authenticate(mongo_user,mongo_pass)
  45. return db
  46. def make_elasticSearch(query):
  47. resp = requests.post(elasticSearch_url,json=query)
  48. if resp.status_code==200:
  49. return json.loads(resp.content.decode())
  50. return None
  51. def getConnect_neo4j():
  52. from py2neo import Graph,NodeMatcher
  53. graph = Graph(host=neo4j_host,auth=(neo4j_user,neo4j_pass))
  54. return graph
  55. # finded = graph.run("MATCH (n:Organization)-[R:ZhaoBiaoRelation]->(p:Project) where n.name='昆山市周市基础建设开发有限公司的昆山市恒迪服装辅料公司' RETURN p LIMIT 25")
  56. # print(json.loads(json.dumps(finded.data())))
  57. # print(finded)
  58. from urllib import request
  59. import os
  60. def check_net(testserver):
  61. try:
  62. response = os.system("ping -c 1 " + testserver)
  63. if response == 0:
  64. return True
  65. except:
  66. return False
  67. return False
  68. import platform
  69. is_internal = False
  70. if platform.system()=="Windows":
  71. OTS_URL = "https://bxkc-ots.cn-hangzhou.ots.aliyuncs.com"
  72. OTS_URL = "https://bxkc-ots.cn-hangzhou.vpc.tablestore.aliyuncs.com"
  73. else:
  74. OTS_URL = "https://bxkc-ots.cn-hangzhou.vpc.tablestore.aliyuncs.com"
  75. check_url = "oss-cn-hangzhou-internal.aliyuncs.com"
  76. is_internal = check_net(check_url)
  77. if not is_internal:
  78. is_internal = False
  79. OTS_URL = "https://bxkc-ots.cn-hangzhou.ots.aliyuncs.com"
  80. print(OTS_URL)
  81. # if platform.system()=="Windows":
  82. # OTS_URL = "https://bxkc-ots.cn-hangzhou.ots.aliyuncs.com"
  83. # else:
  84. # OTS_URL = "https://bxkc-ots.cn-hangzhou.vpc.tablestore.aliyuncs.com"
  85. def getConnect_ots():
  86. ots_client = tablestore.client.OTSClient(OTS_URL, ots_AccessKeyId, ots_AccessKeySecret,
  87. 'bxkc-ots', logger_name = 'table_store.log',
  88. retry_policy = tablestore.WriteRetryPolicy(),socket_timeout=120)
  89. return ots_client
  90. def getConnect_ots_capacity():
  91. ots_client = tablestore.client.OTSClient(OTS_URL, ots_AccessKeyId, ots_AccessKeySecret,
  92. 'bxkc-capacity', logger_name = 'table_store.log',
  93. retry_policy = tablestore.WriteRetryPolicy(),socket_timeout=120)
  94. return ots_client
  95. def getConnect_gdb():
  96. from gremlin_python.driver import client
  97. client = client.Client('ws://gds-bp130d7rgd9m7n61150070pub.graphdb.rds.aliyuncs.com:3734/gremlin', 'g', username="bxkc", password="k0n1bxkc!0K^Em%j")
  98. callback = client.submitAsync("g.V('北京赛克德利科贸有限公司').outE('ZhongBiaoRelation').inV().inE('ZhaoBiaoRelation').outV()")
  99. for result in callback.result():
  100. for item in result:
  101. print(item.id)
  102. return client
  103. def getConnection_postgres():
  104. import psycopg2
  105. conn = psycopg2.connect(dbname=attach_postgres_db,user=attach_postgres_user,password=attach_postgres_pswd,host=attach_postgres_host,port=attach_postgres_port)
  106. return conn
  107. def getAuth():
  108. auth = oss2.Auth(ots_AccessKeyId, ots_AccessKeySecret)
  109. return auth
  110. def getConnect_activateMQ():
  111. import stomp
  112. conn = stomp.Connection(host_and_ports=[(activateMQ_host, activateMQ_port)])
  113. conn.connect(login=activateMQ_user, passcode=activateMQ_pswd)
  114. return conn
  115. def getConnect_activateMQ_ali():
  116. import stomp
  117. conn = stomp.Connection(host_and_ports=[(activateMQ_ali_host, activateMQ_ali_port)])
  118. conn.connect(login=activateMQ_ali_user, passcode=activateMQ_ali_pswd)
  119. return conn
  120. def getConnect_redis_baseline():
  121. db = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,
  122. db=6,password=REDIS_PASS)
  123. return db
  124. def getConnect_redis_doc():
  125. db = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,
  126. db=7,password=REDIS_PASS)
  127. return db
  128. def getConnect_redis_product():
  129. db = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,
  130. db=9,password=REDIS_PASS)
  131. return db
  132. def getConnect_redis_product_pool():
  133. pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT,
  134. db=9,password=REDIS_PASS,max_connections=40)
  135. return pool
  136. if __name__=="__main__":
  137. # solrQuery("document",{"q":"*:*"})
  138. # getConnect_mongodb()
  139. # data = solrQuery_url('http://47.97.221.63:8983/solr/document/select?fq=(publishtime:[2020-01-01T00:00:00Z%20TO%202020-08-12T23:59:59Z])&q=dochtmlcon:"防盗门"')
  140. # data = solrQuery("document",{"q":'dochtmlcon:"防盗门"',"fq":'(publishtime:[2020-01-01T00:00:00Z%20TO%202020-08-12T23:59:59Z])',"fl":"city","rows":1})
  141. # data = make_elasticSearch({"query":{"bool":{"must":[{"wildcard":{"nicknames.keyword":"*服装*"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"aggs":{}})
  142. # print(data)
  143. # getConnect_neo4j()
  144. # conn = getConnection_oracle()
  145. # cursor = conn.cursor()
  146. # getConnect_gdb()
  147. import sys,os
  148. import platform
  149. print(platform.system())
  150. print(sys.platform)