ali_ots 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ali_ots.rb +127 -0
- data/lib/ali_ots/client.rb +183 -0
- data/lib/ali_ots/connection.rb +118 -0
- data/lib/ali_ots/metas/batch_get_row_request.rb +17 -0
- data/lib/ali_ots/metas/batch_get_row_response.rb +17 -0
- data/lib/ali_ots/metas/batch_write_row_request.rb +17 -0
- data/lib/ali_ots/metas/batch_write_row_response.rb +17 -0
- data/lib/ali_ots/metas/capacity_unit.rb +19 -0
- data/lib/ali_ots/metas/column.rb +19 -0
- data/lib/ali_ots/metas/column_schema.rb +18 -0
- data/lib/ali_ots/metas/column_update.rb +21 -0
- data/lib/ali_ots/metas/column_value.rb +27 -0
- data/lib/ali_ots/metas/condition.rb +16 -0
- data/lib/ali_ots/metas/consumed_capacity.rb +17 -0
- data/lib/ali_ots/metas/create_table_request.rb +18 -0
- data/lib/ali_ots/metas/delete_row_in_batch_write_row_request.rb +19 -0
- data/lib/ali_ots/metas/delete_row_request.rb +21 -0
- data/lib/ali_ots/metas/delete_row_response.rb +17 -0
- data/lib/ali_ots/metas/delete_table_request.rb +17 -0
- data/lib/ali_ots/metas/describe_table_request.rb +16 -0
- data/lib/ali_ots/metas/describe_table_response.rb +18 -0
- data/lib/ali_ots/metas/enums/column_type.rb +32 -0
- data/lib/ali_ots/metas/enums/direction.rb +21 -0
- data/lib/ali_ots/metas/enums/operation_type.rb +22 -0
- data/lib/ali_ots/metas/enums/row_existence_expectation.rb +21 -0
- data/lib/ali_ots/metas/error.rb +19 -0
- data/lib/ali_ots/metas/get_range_request.rb +27 -0
- data/lib/ali_ots/metas/get_range_response.rb +21 -0
- data/lib/ali_ots/metas/get_row_request.rb +21 -0
- data/lib/ali_ots/metas/get_row_response.rb +18 -0
- data/lib/ali_ots/metas/list_table_request.rb +14 -0
- data/lib/ali_ots/metas/list_table_response.rb +16 -0
- data/lib/ali_ots/metas/put_row_in_batch_write_row_request.rb +21 -0
- data/lib/ali_ots/metas/put_row_request.rb +22 -0
- data/lib/ali_ots/metas/put_row_response.rb +17 -0
- data/lib/ali_ots/metas/reserved_throughput.rb +16 -0
- data/lib/ali_ots/metas/reserved_throughput_details.rb +22 -0
- data/lib/ali_ots/metas/row.rb +19 -0
- data/lib/ali_ots/metas/row_in_batch_get_row_request.rb +17 -0
- data/lib/ali_ots/metas/row_in_batch_get_row_response.rb +23 -0
- data/lib/ali_ots/metas/row_in_batch_write_row_response.rb +21 -0
- data/lib/ali_ots/metas/table_in_batch_get_row_request.rb +21 -0
- data/lib/ali_ots/metas/table_in_batch_get_row_response.rb +20 -0
- data/lib/ali_ots/metas/table_in_batch_write_row_request.rb +23 -0
- data/lib/ali_ots/metas/table_in_batch_write_row_response.rb +23 -0
- data/lib/ali_ots/metas/table_meta.rb +18 -0
- data/lib/ali_ots/metas/table_primary_key.rb +21 -0
- data/lib/ali_ots/metas/update_row_in_batch_write_row_request.rb +21 -0
- data/lib/ali_ots/metas/update_row_request.rb +22 -0
- data/lib/ali_ots/metas/update_row_response.rb +17 -0
- data/lib/ali_ots/metas/update_table_request.rb +18 -0
- data/lib/ali_ots/metas/update_table_response.rb +16 -0
- data/lib/ali_ots/version.rb +3 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8172fa691d991156c6d158a1213d8a6d7589f8aa
|
4
|
+
data.tar.gz: bb245a29a197ae6a62ffc848249c158cc98185f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f1391e81b061da8aa5dc0bcd618debc5c49e492e4be2d0b99ef6158d4e4ddb067c2cea4658e40e7a212c5bc8f0a8a033470f250414fd99d53f014e89ff2d06c6
|
7
|
+
data.tar.gz: 0b4d76743dc888383116c86fbfbfef6a64e6be01f51c7d1742f222a3b4e159f9f96c7a21a17e48b7891e8d7e3174d6616a3f8b9f21357adfb1cb940c5e4d50aa
|
data/lib/ali_ots.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'protobuf'
|
3
|
+
require 'faraday'
|
4
|
+
require 'addressable/uri'
|
5
|
+
|
6
|
+
require 'ali_ots/client'
|
7
|
+
require 'ali_ots/connection'
|
8
|
+
require 'ali_ots/version'
|
9
|
+
require 'ali_ots/metas/enums/column_type'
|
10
|
+
require 'ali_ots/metas/enums/direction'
|
11
|
+
require 'ali_ots/metas/enums/operation_type'
|
12
|
+
require 'ali_ots/metas/enums/row_existence_expectation'
|
13
|
+
require 'ali_ots/metas/error'
|
14
|
+
require 'ali_ots/metas/condition'
|
15
|
+
require 'ali_ots/metas/column_schema'
|
16
|
+
require 'ali_ots/metas/column_value'
|
17
|
+
require 'ali_ots/metas/column_update'
|
18
|
+
require 'ali_ots/metas/column'
|
19
|
+
require 'ali_ots/metas/table_meta'
|
20
|
+
require 'ali_ots/metas/row_in_batch_get_row_request'
|
21
|
+
require 'ali_ots/metas/table_in_batch_get_row_request'
|
22
|
+
require 'ali_ots/metas/batch_get_row_request'
|
23
|
+
require 'ali_ots/metas/capacity_unit'
|
24
|
+
require 'ali_ots/metas/consumed_capacity'
|
25
|
+
require 'ali_ots/metas/row'
|
26
|
+
require 'ali_ots/metas/row_in_batch_get_row_response'
|
27
|
+
require 'ali_ots/metas/table_in_batch_get_row_response'
|
28
|
+
require 'ali_ots/metas/batch_get_row_response'
|
29
|
+
require 'ali_ots/metas/put_row_in_batch_write_row_request'
|
30
|
+
require 'ali_ots/metas/put_row_request'
|
31
|
+
require 'ali_ots/metas/put_row_response'
|
32
|
+
require 'ali_ots/metas/delete_row_in_batch_write_row_request'
|
33
|
+
require 'ali_ots/metas/update_row_in_batch_write_row_request'
|
34
|
+
require 'ali_ots/metas/table_in_batch_write_row_request'
|
35
|
+
require 'ali_ots/metas/row_in_batch_write_row_response'
|
36
|
+
require 'ali_ots/metas/table_in_batch_write_row_response'
|
37
|
+
require 'ali_ots/metas/batch_write_row_request'
|
38
|
+
require 'ali_ots/metas/batch_write_row_response'
|
39
|
+
require 'ali_ots/metas/reserved_throughput'
|
40
|
+
require 'ali_ots/metas/reserved_throughput_details'
|
41
|
+
require 'ali_ots/metas/create_table_request'
|
42
|
+
require 'ali_ots/metas/delete_row_request'
|
43
|
+
require 'ali_ots/metas/delete_row_response'
|
44
|
+
require 'ali_ots/metas/delete_table_request'
|
45
|
+
require 'ali_ots/metas/describe_table_request'
|
46
|
+
require 'ali_ots/metas/describe_table_response'
|
47
|
+
require 'ali_ots/metas/get_range_request'
|
48
|
+
require 'ali_ots/metas/get_range_response'
|
49
|
+
require 'ali_ots/metas/get_row_request'
|
50
|
+
require 'ali_ots/metas/get_row_response'
|
51
|
+
require 'ali_ots/metas/list_table_request'
|
52
|
+
require 'ali_ots/metas/list_table_response'
|
53
|
+
require 'ali_ots/metas/table_primary_key'
|
54
|
+
require 'ali_ots/metas/update_row_request'
|
55
|
+
require 'ali_ots/metas/update_row_response'
|
56
|
+
require 'ali_ots/metas/update_table_request'
|
57
|
+
require 'ali_ots/metas/update_table_response'
|
58
|
+
|
59
|
+
|
60
|
+
module AliOts
|
61
|
+
|
62
|
+
AliOts::CONFIG = {
|
63
|
+
DEBUG_LEVEL: ::Logger::DEBUG,
|
64
|
+
END_POSITION: nil,
|
65
|
+
ACCESS_ID: nil,
|
66
|
+
ACCESS_KEY: nil,
|
67
|
+
INSTANCE_NAME: nil,
|
68
|
+
APIVERSION: "2014-08-08"
|
69
|
+
}
|
70
|
+
|
71
|
+
extend self
|
72
|
+
def configure
|
73
|
+
yield(AliOts::CONFIG)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
@@ -0,0 +1,183 @@
|
|
1
|
+
module AliOts
|
2
|
+
class Client
|
3
|
+
attr_accessor :conn
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
self.conn = AliOts::Connection.new
|
7
|
+
end
|
8
|
+
|
9
|
+
# 创建表
|
10
|
+
# example:
|
11
|
+
# schema_of_primary_keys = [AliOts::Metas::ColumnSchema.new(name: 'id', type: AliOts::Metas::Enums::ColumnType::INTEGER)]
|
12
|
+
#
|
13
|
+
# client.create_table("myTable", schema_of_primary_keys, 9, 9)
|
14
|
+
def create_table(table_name, schema_of_primary_keys, capacity_read, capacity_write)
|
15
|
+
reserved_throughput_meta = ::AliOts::Metas::ReservedThroughput.new(capacity_unit: ::AliOts::Metas::CapacityUnit.new(read: capacity_read, write: capacity_write))
|
16
|
+
table_meta = AliOts::Metas::TableMeta.new(table_name: table_name, primary_key: schema_of_primary_keys)
|
17
|
+
request_meta = AliOts::Metas::CreateTableRequest.new(table_meta: table_meta, reserved_throughput: reserved_throughput_meta)
|
18
|
+
|
19
|
+
self.conn.request("CreateTable", request_meta)
|
20
|
+
end
|
21
|
+
|
22
|
+
# 删除
|
23
|
+
# example:
|
24
|
+
# client.delete_table("myTable")
|
25
|
+
def delete_table(table_name)
|
26
|
+
request_meta = AliOts::Metas::DeleteTableRequest.new(table_name: table_name)
|
27
|
+
|
28
|
+
self.conn.request("DeleteTable", request_meta)
|
29
|
+
end
|
30
|
+
|
31
|
+
# 列出所有表
|
32
|
+
# example:
|
33
|
+
# client.list_table()
|
34
|
+
def list_table
|
35
|
+
request_meta = AliOts::Metas::ListTableRequest.new()
|
36
|
+
|
37
|
+
status, body, response_status = self.conn.request("ListTable", request_meta)
|
38
|
+
body = AliOts::Metas::ListTableResponse.decode(body) if status == "OK"
|
39
|
+
return status, body, response_status
|
40
|
+
end
|
41
|
+
|
42
|
+
# 更新表
|
43
|
+
# example:
|
44
|
+
# schema_of_primary_keys = [AliOts::Metas::ColumnSchema.new(name: 'id', type: AliOts::Metas::Enums::ColumnType::INTEGER)]
|
45
|
+
#
|
46
|
+
# client.update_table("myTable", 9, 9)
|
47
|
+
def update_table(table_name, capacity_read, capacity_write)
|
48
|
+
reserved_throughput_meta = ::AliOts::Metas::ReservedThroughput.new(capacity_unit: ::AliOts::Metas::CapacityUnit.new(read: capacity_read, write: capacity_write))
|
49
|
+
request_meta = AliOts::Metas::UpdateTableRequest.new(table_name: table_name, reserved_throughput: reserved_throughput_meta)
|
50
|
+
|
51
|
+
status, body, response_status = self.conn.request("UpdateTable", request_meta)
|
52
|
+
body = AliOts::Metas::UpdateTableResponse.decode(body) if status == "OK"
|
53
|
+
return status, body, response_status
|
54
|
+
end
|
55
|
+
|
56
|
+
# 查询指定表的结构信息和预留读写吞吐量设置信息
|
57
|
+
# example:
|
58
|
+
# client.describe_table("myTable")
|
59
|
+
def describe_table(table_name)
|
60
|
+
request_meta = AliOts::Metas::DescribeTableRequest.new(table_name: table_name)
|
61
|
+
|
62
|
+
status, body, response_status = self.conn.request("DescribeTable", request_meta)
|
63
|
+
body = AliOts::Metas::DescribeTableResponse.decode(body) if status == "OK"
|
64
|
+
return status, body, response_status
|
65
|
+
end
|
66
|
+
|
67
|
+
# 插入数据到指定的行,如果该行不存在,则新增一行;若该行存在,则覆盖原有行
|
68
|
+
# example:
|
69
|
+
# #IGNORE 表示不做行存在性检查。 EXPECT_EXIST 表示期待该行存在。 EXPECT_NOT_EXIST 表示期待该行不存在。
|
70
|
+
# condition_type = AliOts::Metas::Enums::RowExistenceExpectation::EXPECT_NOT_EXIST
|
71
|
+
# primary_keys = [AliOts::Metas::Column.new(name: "id", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 1))]
|
72
|
+
# user_name_column = AliOts::Metas::Column.new(name: "user_name", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::STRING, v_string: "zhao"))
|
73
|
+
# age_column = AliOts::Metas::Column.new(name: "age", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 27))
|
74
|
+
# attribute_columns = [user_name_column, age_column]
|
75
|
+
#
|
76
|
+
# client.put_row("myTable", condition_type, primary_keys, attribute_columns)
|
77
|
+
def put_row(table_name, condition_type, primary_keys, attribute_columns = [])
|
78
|
+
return ["ERROR", "primary_keys is empty", 0] unless primary_keys.present?
|
79
|
+
condition = AliOts::Metas::Condition.new(row_existence: condition_type)
|
80
|
+
request_meta = AliOts::Metas::PutRowRequest.new(table_name: table_name, condition: condition, primary_key: primary_keys, attribute_columns: attribute_columns)
|
81
|
+
|
82
|
+
status, body, response_status = self.conn.request("PutRow", request_meta)
|
83
|
+
body = AliOts::Metas::PutRowResponse.decode(body) if status == "OK"
|
84
|
+
return status, body, response_status
|
85
|
+
end
|
86
|
+
|
87
|
+
# 根据给定的主键读取单行数据。
|
88
|
+
# example:
|
89
|
+
# primary_keys = [AliOts::Metas::Column.new(name: "id", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 1))]
|
90
|
+
# columns = ["user_name", "age"]
|
91
|
+
#
|
92
|
+
# client.get_row("myTable", primary_keys, columns)
|
93
|
+
def get_row(table_name, primary_keys, columns = [])
|
94
|
+
request_meta = AliOts::Metas::GetRowRequest.new(table_name: table_name, primary_key: primary_keys, columns_to_get: columns)
|
95
|
+
|
96
|
+
status, body, response_status = self.conn.request("GetRow", request_meta)
|
97
|
+
body = AliOts::Metas::GetRowResponse.decode(body) if status == "OK"
|
98
|
+
return status, body, response_status
|
99
|
+
end
|
100
|
+
|
101
|
+
# 更新指定行的数据,如果该行不存在,则新增一行;若该行存在,则根据请求的内容在这一行中新增、修改或者删除指定列的值。
|
102
|
+
# example:
|
103
|
+
# condition_type = AliOts::Metas::Enums::RowExistenceExpectation::IGNORE
|
104
|
+
# primary_keys = [AliOts::Metas::Column.new(name: "id", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 1))]
|
105
|
+
# user_name_column = AliOts::Metas::Column.new(name: "user_name", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::STRING, v_string: "zhao"))
|
106
|
+
# age_column = AliOts::Metas::Column.new(name: "age", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 27))
|
107
|
+
# attribute_columns = [user_name_column, age_column]
|
108
|
+
#
|
109
|
+
# client.update_row("myTable", condition_type, primary_keys, attribute_columns)
|
110
|
+
def update_row(table_name, condition_type, primary_keys, attribute_columns = [])
|
111
|
+
condition = AliOts::Metas::Condition.new(row_existence: condition_type)
|
112
|
+
request_meta = AliOts::Metas::UpdateRowRequest.new(table_name: table_name, conditon: condition, primary_key: primary_keys, attribute_columns: attribute_columns)
|
113
|
+
|
114
|
+
status, body, response_status = self.conn.request("UpdateRow", request_meta)
|
115
|
+
body = AliOts::Metas::UpdateRowResponse.decode(body) if status == "OK"
|
116
|
+
return status, body, response_status
|
117
|
+
end
|
118
|
+
|
119
|
+
# 删除一行数据
|
120
|
+
# example:
|
121
|
+
# condition_type = AliOts::Metas::Enums::RowExistenceExpectation::IGNORE
|
122
|
+
# primary_keys = [AliOts::Metas::Column.new(name: "id", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 1))]
|
123
|
+
#
|
124
|
+
# client.delete_row("myTable", condition_type, primary_keys)
|
125
|
+
def delete_row(table_name, condition_type, primary_keys = [])
|
126
|
+
return ["ERROR", "primary_keys is empty", 0] unless primary_keys.present?
|
127
|
+
condition = AliOts::Metas::Condition.new(row_existence: condition_type)
|
128
|
+
request_meta = AliOts::Metas::DeleteRowRequest.new(table_name: table_name, condition: condition, primary_key: primary_keys)
|
129
|
+
|
130
|
+
status, body, response_status = self.conn.request("DeleteRow", request_meta)
|
131
|
+
body = AliOts::Metas::DeleteRowResponse.decode(body) if status == "OK"
|
132
|
+
return status, body, response_status
|
133
|
+
end
|
134
|
+
|
135
|
+
# 读取指定主键范围内的数据
|
136
|
+
# example:
|
137
|
+
# direction = AliOts::Metas::Enums::Direction::FORWARD #FORWARD 表示此次查询按照主键由小到大的顺序进行, BACKWARD 表示此次查询按照主键由大到小的顺序进行
|
138
|
+
# inclusive_start_primary_keys = [AliOts::Metas::Column.new(name: "id", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 1))]
|
139
|
+
# exclusive_end_primary_keys = [AliOts::Metas::Column.new(name: "id", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 999))]
|
140
|
+
#
|
141
|
+
# client.get_range("myTable", direction, inclusive_start_primary_keys, exclusive_end_primary_keys)
|
142
|
+
def get_range(table_name, inclusive_start_primary_keys, exclusive_end_primary_keys, direction = AliOts::Metas::Enums::Direction::FORWARD, columns = [], limit = nil)
|
143
|
+
request_meta = AliOts::Metas::GetRangeRequest.new(table_name: table_name, direction: direction, inclusive_start_primary_key: inclusive_start_primary_keys, exclusive_end_primary_key: exclusive_end_primary_keys, columns_to_get: columns, limit: limit)
|
144
|
+
|
145
|
+
status, body, response_status = self.conn.request("GetRange", request_meta)
|
146
|
+
body = AliOts::Metas::GetRangeResponse.decode(body) if status == "OK"
|
147
|
+
return status, body, response_status
|
148
|
+
end
|
149
|
+
|
150
|
+
# 批量读取一个或多个表中的若干行数据
|
151
|
+
# example:
|
152
|
+
# primary_keys = [AliOts::Metas::Column.new(name: "id", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 1))]
|
153
|
+
# row1 = AliOts::Metas::RowInBatchGetRowRequest.new(primary_key: primary_keys)
|
154
|
+
# rows = [row]
|
155
|
+
# table1 = AliOts::Metas::TableInBatchGetRowRequest.new(table_name: "myTable", rows: rows, columns_to_get: ['name', 'age'])
|
156
|
+
# tables = [table1]
|
157
|
+
# client.batch_get_row(tables)
|
158
|
+
def batch_get_row(tables)
|
159
|
+
request_meta = AliOts::Metas::BatchGetRowRequest.new(tables: tables)
|
160
|
+
|
161
|
+
status, body, response_status = self.conn.request("BatchGetRow", request_meta)
|
162
|
+
body = AliOts::Metas::BatchGetRowResponse.decode(body) if status == "OK"
|
163
|
+
return status, body, response_status
|
164
|
+
end
|
165
|
+
|
166
|
+
# 批量插入,修改或删除一个或多个表中的若干行数据。
|
167
|
+
# example:
|
168
|
+
# condition = AliOts::Metas::Condition.new(row_existence: AliOts::Metas::Enums::RowExistenceExpectation::EXPECT_NOT_EXIST)
|
169
|
+
# primary_keys = [AliOts::Metas::Column.new(name: "id", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 100))]
|
170
|
+
# columns = [AliOts::Metas::Column.new(name: "name", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::STRING, v_string: 'liu')), AliOts::Metas::Column.new(name: "age", value: AliOts::Metas::ColumnValue.new(type: AliOts::Metas::Enums::ColumnType::INTEGER, v_int: 93))]
|
171
|
+
# row1 = AliOts::Metas::PutRowInBatchWriteRowRequest.new(condition: condition, primary_key: primary_keys, attribute_columns: columns)
|
172
|
+
# rows = [row1]
|
173
|
+
# tables = [AliOts::Metas::TableInBatchWriteRowRequest.new(table_name: 'myTable', put_rows: rows)]
|
174
|
+
# client.batch_write_row(tables)
|
175
|
+
def batch_write_row(tables)
|
176
|
+
request_meta = AliOts::Metas::BatchWriteRowRequest.new(tables: tables)
|
177
|
+
|
178
|
+
status, body, response_status = self.conn.request("BatchWriteRow", request_meta)
|
179
|
+
body = AliOts::Metas::BatchWriteRowResponse.decode(body) if status == "OK"
|
180
|
+
return status, body, response_status
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
module AliOts
|
2
|
+
class Connection
|
3
|
+
attr_accessor :conn
|
4
|
+
attr_accessor :logger
|
5
|
+
|
6
|
+
def initialize(base_url = AliOts::CONFIG[:END_POSITION])
|
7
|
+
self.conn = Faraday.new(:url => base_url) do |faraday|
|
8
|
+
faraday.request :url_encoded # form-encode POST params
|
9
|
+
faraday.response :logger # log requests to STDOUT
|
10
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
11
|
+
end
|
12
|
+
self.logger = Logger.new(STDOUT)
|
13
|
+
self.logger.level = AliOts::CONFIG[:DEBUG_LEVEL]
|
14
|
+
end
|
15
|
+
|
16
|
+
def request(action, body, headers = {})
|
17
|
+
self.logger.info "body:==========================================================="
|
18
|
+
self.logger.info body.to_json
|
19
|
+
|
20
|
+
headers ||= {}
|
21
|
+
url = AliOts::END_POSITION + "/" + action
|
22
|
+
serialize_body = body.serialize_to_string
|
23
|
+
ots_headers = make_headers(url, serialize_body)
|
24
|
+
headers.merge!(ots_headers)
|
25
|
+
|
26
|
+
response = conn.post do |req|
|
27
|
+
req.url action
|
28
|
+
req.headers.merge!(headers)
|
29
|
+
req.body = serialize_body
|
30
|
+
end
|
31
|
+
|
32
|
+
self.logger.info "response:==========================================================="
|
33
|
+
self.logger.info response
|
34
|
+
|
35
|
+
return parse_response(response.status, response.headers, response.body)
|
36
|
+
end
|
37
|
+
|
38
|
+
def make_headers(query, body)
|
39
|
+
md5 = Base64.encode64(Digest::MD5.digest(body)).gsub("\n", "")
|
40
|
+
date = Time.now.gmtime.strftime('%a, %d %b %Y %H:%M:%S GMT')
|
41
|
+
|
42
|
+
headers = {
|
43
|
+
'x-ots-date' => date,
|
44
|
+
'x-ots-apiversion' => AliOts::CONFIG[:APIVERSION],
|
45
|
+
'x-ots-accesskeyid' => AliOts::CONFIG[:ACCESS_ID],
|
46
|
+
'x-ots-instancename' => AliOts::CONFIG[:INSTANCE_NAME],
|
47
|
+
'x-ots-contentmd5' => md5,
|
48
|
+
}
|
49
|
+
|
50
|
+
signature = make_request_signature(query, headers)
|
51
|
+
headers['x-ots-signature'] = signature
|
52
|
+
|
53
|
+
return headers
|
54
|
+
end
|
55
|
+
|
56
|
+
def make_request_signature(query, headers)
|
57
|
+
require "addressable/uri"
|
58
|
+
uri = URI.parse(query)
|
59
|
+
path = uri.path
|
60
|
+
query_string = uri.query
|
61
|
+
|
62
|
+
addr_uri = Addressable::URI.new
|
63
|
+
addr_uri.query = query_string
|
64
|
+
addr_uri.query_values = addr_uri.query_values.sort_by{|q| q[0]} rescue {}
|
65
|
+
sorted_query = addr_uri.query
|
66
|
+
signature_string = path + "\n" + 'POST' + "\n" + sorted_query + "\n"
|
67
|
+
|
68
|
+
headers_string = make_headers_string(headers)
|
69
|
+
signature_string += headers_string + "\n"
|
70
|
+
|
71
|
+
signature = call_signature_method(signature_string)
|
72
|
+
return signature
|
73
|
+
end
|
74
|
+
|
75
|
+
def make_headers_string(headers)
|
76
|
+
headers.sort_by{|k,v| k}.map{|k,v| "#{k}:#{v}"}.join("\n")
|
77
|
+
end
|
78
|
+
|
79
|
+
def call_signature_method(signature_string)
|
80
|
+
digest = OpenSSL::Digest.new('sha1')
|
81
|
+
hmac = OpenSSL::HMAC.digest(digest, AliOts::CONFIG[:ACCESS_KEY], signature_string)
|
82
|
+
Base64.encode64(hmac).gsub("\n", "")
|
83
|
+
end
|
84
|
+
|
85
|
+
def parse_response(status, headers, body)
|
86
|
+
check_headers(headers, body)
|
87
|
+
|
88
|
+
return ["OK", body, status] if status >= 200 && status < 300
|
89
|
+
return ["ERR", body, status]
|
90
|
+
end
|
91
|
+
|
92
|
+
def check_headers(headers, body)
|
93
|
+
# 1, check headers
|
94
|
+
header_names = [
|
95
|
+
'x-ots-contentmd5',
|
96
|
+
'x-ots-requestid',
|
97
|
+
'x-ots-date',
|
98
|
+
'x-ots-contenttype',
|
99
|
+
]
|
100
|
+
raise AliOts::Exceptions::ResponseException.new("Missing Parameter: #{(header_names-headers.symbolize_keys.keys).join(", ")}") unless header_names == header_names&&headers.symbolize_keys.keys
|
101
|
+
|
102
|
+
# 2, check md5
|
103
|
+
md5 = Base64.encode64(Digest::MD5.digest(body)).gsub("\n", "")
|
104
|
+
raise AliOts::Exceptions::ResponseException.new("MD5 mismatch in response") unless md5 == headers['x-ots-contentmd5']
|
105
|
+
|
106
|
+
# 3, check date
|
107
|
+
raise AliOts::Exceptions::ResponseException.new("Invalid date format in response") unless server_time = (Time.parse(headers['x-ots-date']) rescue nil)
|
108
|
+
|
109
|
+
# 4, check date range
|
110
|
+
raise AliOts::Exceptions::ResponseException.new("The difference between date in response and system time is more than 15 minutes") unless (Time.now - server_time).abs < 15.minute
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module AliOts
|
2
|
+
module Metas
|
3
|
+
#批量读取一个或多个表中的若干行数据。
|
4
|
+
class BatchGetRowRequest < ::Protobuf::Message
|
5
|
+
repeated AliOts::Metas::TableInBatchGetRowRequest, :tables, 1
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
=begin
|
13
|
+
message BatchGetRowRequest {
|
14
|
+
repeated TableInBatchGetRowRequest tables = 1;
|
15
|
+
}
|
16
|
+
|
17
|
+
=end
|