说明
python cqlgenerator示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。
编程语言: Python
命名空间/包名称: ottertestresources
示例#1文件:
load_cql.py项目:
rackerlabs/otter
def generate(args):
"""
Generate CQL and/or load it into a cassandra instance/cluster.
"""
try:
generator = CQLGenerator(args.cql_dir, safe_only=args.ban_unsafe)
except Exception as e:
print(e.message)
sys.exit(1)
cql = generator.generate_cql(
keyspace_name=args.keyspace,
replication_factor=args.replication,
outfile=args.outfile)
if args.dry_run:
return
# filter out comments, to make debugging easier
cql = "\n".join(
[line for line in cql.split('\n')
if line.strip() and not line.strip().startswith('--')])
# no blank lines or pointless whitespace
commands = [x.strip() for x in cql.split(';') if x.strip()]
# connect
if args.verbose > 0:
print("Attempting to connect to {0}:{1}".format(args.host, args.port))
try:
connection = connect(args.host, args.port, cql_version='3.0.4')
except Exception as e:
print("CONNECTION ERROR: {0}".format(e.message))
sys.exit(1)
cursor = connection.cursor()
# execute commands
execute_commands(cursor, commands, args.verbose)
if args.verbose > 0:
print('\n----\n')
print("Done. Disconnecting.")
cursor.close()
connection.close()
示例#2文件:
load_cql.py项目:
alex/otter
def run(args):
"""
Generate CQL and/or load it into a cassandra instance/cluster.
"""
try:
generator = CQLGenerator(args.cql_dir, safe_only=args.ban_unsafe)
except Exception as e:
print e.message
sys.exit(1)
cql = generator.generate_cql(
keyspace_name=args.keyspace,
replication_factor=args.replication,
outfile=args.outfile)
if args.dry_run:
return
# filter out comments, to make debugging easier
cql = "\n".join(
[line for line in cql.split('\n')
if line.strip() and not line.strip().startswith('--')])
# no blank lines or pointless whitespace
commands = [x.strip() for x in cql.split(';') if x.strip()]
# connect
if args.verbose > 0:
print "Attempting to connect to {0}:{1}".format(args.host, args.port)
try:
connection = connect(args.host, args.port, cql_version='3')
except Exception as e:
print "CONNECTION ERROR: {0}".format(e.message)
sys.exit(1)
cursor = connection.cursor()
for command in commands:
try:
cursor.execute(command, {})
except ProgrammingError as pe:
# if somewhat verbose, then print out all errors.
# if less verbose, print out only non-already-existing errors
message = pe.message.lower()
significant_error = (
"already exist" not in message and
"existing keyspace" not in message)
if args.verbose > 1 or significant_error:
print '\n----\n'
print command
print "{0}".format(pe.message.strip())
if significant_error:
sys.exit(1)
else:
# extremely verbose - notify that command executed correctly.
if args.verbose > 2:
print '\n----\n'
print command
print "Ok."
if args.verbose > 0:
print '\n----\n'
print "Done. Disconnecting."
cursor.close()
connection.close()