def save_predict(time, symbol, percent): """ :param time: :param symbol: :param percent: :return: """ end_point = "https://deep-trade.cn-hangzhou.ots.aliyuncs.com" instance_name = "deep-trade" table_name = "t_singal" ots_client = OTSClient(end_point, table_store_access_key, table_store_access_secret, instance_name) primary_key = [('day', time), ('symbol', symbol)] attribute_columns = [('percent', percent)] row = Row(primary_key, attribute_columns) condition = Condition(RowExistenceExpectation.EXPECT_NOT_EXIST) try: # 调用put_row方法,如果没有指定ReturnType,则return_row为None。 consumed, return_row = ots_client.put_row(table_name, row, condition) # 打印出此次请求消耗的写CU。 print('put row succeed, consume %s write cu.' % consumed.write) # 客户端异常,一般为参数错误或者网络异常。 except OTSClientError as e: print("put row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message())) # 服务端异常,一般为参数错误或者流控错误。 except OTSServiceError as e: print( "put row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id()))
def handler(raw_event: RawEvent, context: Context) -> str: print("Got event:", raw_event) event = json.loads(raw_event) client = OTSClient( OTS_ENDPOINT, context.credentials.access_key_id, context.credentials.access_key_secret, OTS_INSTANCE_NAME, sts_token=context.credentials.security_token, ) result: Dict[str, Any] method = event["httpMethod"].upper() user_id = event["headers"]["X-User-Id"] if method == "GET": result = get(client, user_id) elif method == "POST" or method == "PUT": body = event["body"] if event["isBase64Encoded"]: body = base64.b64decode(body) result = update(client, user_id, json.loads(body)) else: result = {"statusCode": 405, "body": {"error": "Method Not Allowed"}} response = {**result, "isBase64Encoded": False} return json.dumps(response)
def __init__(self, table_name): self._client = OTSClient( access_key_id=Config['aliyun']['access_key'], access_key_secret=Config['aliyun']['access_secret'], end_point=Config['tablestore']['ots_endpoint'], instance_name=Config['tablestore']['ots_instance'], ) self._table_name = table_name
def __init__(self, ots_endpoint, ots_id, ots_secret, ots_instance, table_name, col_key="key", col_value="value", cache=False): self.client = OTSClient(ots_endpoint, ots_id, ots_secret, ots_instance) self.table = table_name self._k = col_key self._v = col_value self._c = cache self._data = {}
def client(self): return OTSClient(settings.OTS_ENDPOINT, settings.OTS_ID, settings.OTS_SECRET, settings.OTS_INSTANCE)