Skip to content

Instantly share code, notes, and snippets.

@shohi
Last active July 17, 2019 02:02
Show Gist options
  • Save shohi/123ac32d7de21bb18d9dc79ba5d7d5e6 to your computer and use it in GitHub Desktop.
Save shohi/123ac32d7de21bb18d9dc79ba5d7d5e6 to your computer and use it in GitHub Desktop.
`select for update` feature test for ignite 2.7.5 using python3
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import OrderedDict
from decimal import Decimal
from pyignite import Client, GenericObjectMeta
from pyignite.datatypes import DoubleObject, IntObject, String
from pyignite.datatypes.prop_codes import *
from pyignite.exceptions import CacheError
CITY_TABLE_NAME = 'City'
DROP_TABLE_QUERY = '''DROP TABLE {} IF EXISTS'''
# establish connection
client = Client()
client.connect('127.0.0.1', 10800)
CITY_CREATE_TABLE_QUERY = '''CREATE TABLE City (
ID INT(11),
Name CHAR(35),
CountryCode CHAR(3),
District CHAR(20),
Population INT(11),
)
'''
CITY_INSERT_QUERY = '''INSERT INTO City(
ID, Name, CountryCode, District, Population
) VALUES (?, ?, ?, ?, ?)'''
CITY_DATA = [
[3793, 'New York', 'USA', 'New York', 8008278],
[3794, 'Los Angeles', 'USA', 'California', 3694820],
[3795, 'Chicago', 'USA', 'Illinois', 2896016],
[3796, 'Houston', 'USA', 'Texas', 1953631],
[3797, 'Philadelphia', 'USA', 'Pennsylvania', 1517550],
[3798, 'Phoenix', 'USA', 'Arizona', 1321045],
[3799, 'San Diego', 'USA', 'California', 1223400],
[3800, 'Dallas', 'USA', 'Texas', 1188580],
[3801, 'San Antonio', 'USA', 'Texas', 1144646],
[3802, 'Detroit', 'USA', 'Michigan', 951270],
[3803, 'San Jose', 'USA', 'California', 894943],
[3804, 'Indianapolis', 'USA', 'Indiana', 791926],
[3805, 'San Francisco', 'USA', 'California', 776733],
]
try:
tmp = client.get_cache('SQL_PUBLIC_CITY')
if tmp != None:
tmp.destroy()
except CacheError:
# do nothing
print("no exit-----")
# create tables
city_cache = client.create_cache({
PROP_NAME: 'SQL_PUBLIC_CITY',
PROP_SQL_SCHEMA: 'PUBLIC',
PROP_CACHE_ATOMICITY_MODE: 2,
PROP_QUERY_ENTITIES: [
{
'table_name': 'CITY'.upper(),
'key_field_name': 'ID',
'key_type_name': 'java.lang.Integer',
'field_name_aliases': [],
'query_fields': [
{
'name': 'ID',
'type_name': 'java.lang.Integer',
},
{
'name': 'NAME',
'type_name': 'java.lang.String',
},
{
'name': 'CountryCode',
'type_name': 'java.lang.String',
},
{
'name': 'District',
'type_name': 'java.lang.String',
},
{
'name': 'Population',
'type_name': 'java.lang.Integer',
},
],
'query_indexes': [],
'value_type_name': 'SQL_PUBLIC_STUDENT_TYPE',
'value_field_name': None,
},
],
})
# load data
for row in CITY_DATA:
client.sql(CITY_INSERT_QUERY, query_args=row)
# 10 most populated cities (with pagination)
MOST_POPULATED_QUERY = '''
SELECT name, population FROM City ORDER BY population DESC LIMIT 10'''
result = client.sql(MOST_POPULATED_QUERY)
print('Most 10 populated cities:')
for row in result:
print(row)
# Most 10 populated cities:
# ['Mumbai (Bombay)', 10500000]
# ['Shanghai', 9696300]
# ['New York', 8008278]
# ['Peking', 7472000]
# ['Delhi', 7206704]
# ['Chongqing', 6351600]
# ['Tianjin', 5286800]
# ['Calcutta [Kolkata]', 4399819]
# ['Wuhan', 4344600]
# ['Harbin', 4289800]
QU_QUERY='''
SELECT NAME FROM City WHERE NAME='Chicago' FOR UPDATE
'''
print(client.get_cache_names())
print('select for update ---')
result = client.sql(QU_QUERY)
print(next(result))
# clean up
# DROP_TABLE_QUERY = '''DROP TABLE City IF EXISTS'''
# client.sql(DROP_TABLE_QUERY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment