def main(): # OAUTH object, which builds the http authorization header OAUTH = Oauth(AUTH_DICTIONARY) # Initiate a connection to kdb on port 3000 conn = kdb.q('localhost', 3000, '') # Clear tweets table. Remove this line if you would like the # table to persiste when restarted conn.k('tweets:();') # The rxStream function initiates the http stream with the # correct authentication. The tweet object represents a single # tweet for tweet in rxStream(OAUTH): # Convert tweet to JSON format t = json.loads(tweet) # Only process tweets (there are some outputs from the # http stream that are not tweets if "created_at" in t.keys(): # Uncomment this line to see entire JSON output #pprint.pprint(t, width=2) # Form a q-formatted table table = formTable(t) # Append tweet to the 'tweets' table in kdb conn.k('.u.upd[`tweets;' + table + ']')
def main(): # OAUTH object, which builds the http authorization header OAUTH = Oauth(AUTH_DICTIONARY) # Initiate a connection to kdb on port 3000 conn = kdb.q('localhost', 3000, '') # Clear tweets table. Remove this line if you would like the # table to persiste when restarted conn.k('tweets:();') # The rxStream function initiates the http stream with the # correct authentication. The tweet object represents a single # tweet for tweet in rxStream(OAUTH): # Convert tweet to JSON format t = json.loads(tweet) # Only process tweets (there are some outputs from the # http stream that are not tweets if "created_at" in t.keys(): # Uncomment this line to see entire JSON output #pprint.pprint(t, width=2) # Form a q-formatted table table = formTable(t) # Append tweet to the 'tweets' table in kdb conn.k('.u.upd[`tweets;' + table + ']')
def connect_to_kdb(host,port,attempt=1,max_attempts=10): global TP_CONN try: L.info("Connecting to KDB: {}:{}".format(host,port)) TP_CONN = kdb.q(host,port,'') L.info("Success...") return True except: if attempt >= max_attempts: L.error("Max number of retries reached") return False L.error("Failed to connect, retrying (attempt {}/{})".format(attempt,max_attempts)) time.sleep(6*attempt) connect_to_kdb(host,port,attempt=attempt+1,max_attempts=max_attempts)
def kdblogin(port=5000): ''' shortcut to connect to q server Parameters ---------- port: int the port number of Q process Returns ------- object: the object to connect to Q process ''' return kdb.q("localhost",port,"")
def kdblogin(port=5000): ''' shortcut to connect to q server ''' return kdb.q("localhost",port,"")
def setUp(self): self.conn = kdb.q('v-kdbd-01', 5003, 'mwarren')