Cabinet is a data storage service of the company. You need to add a shell client to view data and perform simple operations.
It is easier to write in python than you think. The Code is as follows:
[Python]
#! /Usr/bin/python-I
# Coding: UTF-8
Import sys
Import OS
Dirname = OS. path. dirname (sys. argv [0])
Sys. path. append (dirname + '/gen-py ')
Sys. path. append ('/usr/lib/python2.7/site-packages ')
From cabinet import CabinetStorage
From document. ttypes import Document
From thrift import Thrift
From thrift. transport import TSocket
From thrift. transport import TTransport
From thrift. protocol import TCompactProtocol
From optparse import OptionParser
# Avoid UnicodeDecodeError
Reload (sys)
Sys. setdefaultencoding ("UTF-8 ")
Try:
# Command line arguments.
Parser = OptionParser (usage = "usage: % prog [options]", version = "% prog 1.0 ")
Parser. add_option ("-c", "-- host", dest = "host", action = "store", default = "127.0.0.1", type = "string ", help = "Specify cabinet host. ")
Parser. add_option ("-p", "-- port", dest = "port", action = "store", default = 9090, type = "int ", help = "Specify cabinet port. ")
(Options, args) = parser. parse_args ()
# Connect to cabinet server.
Transport = TSocket. TSocket (options. host, options. port)
Transport = TTransport. TBufferedTransport (transport)
Protocol = TCompactProtocol. TCompactProtocol (transport)
Client = CabinetStorage. Client (protocol)
Transport. open ()
Failed t Exception as e:
Print e
OS. _ exit (1)
# Welcome info.
Print "Cabinet Shell V1.0, Copyright I-MD 2013, All rights reserved ."
Print "For usage, invoke help (). \ n"
# Main functions.
Def help ():
Print "Main functions :"
Print "get (db, id )"
Print "set (db, id, val )"
Print "delete (db, id )"
Print "unpackDocInfo (docstr )"
Print "packDocInfo (doc )"
Def get (db, id ):
Return client. Get (db, id)
Def set (db, id, val ):
Return client. Set (db, id, val)
Def delete (db, id ):
Return client. delete (db, id)
Def packDocInfo (doc ):
TransportOut = TTransport. TMemoryBuffer ()
ProtocolOut = TCompactProtocol. TCompactProtocol (transportOut)
Doc. write (protocolOut)
Return transportOut. getvalue ()
Def unpackDocInfo (docstr ):
TransportIn = TTransport. TMemoryBuffer (docstr)
ProtocolIn = TCompactProtocol. TCompactProtocol (transportIn)
Doc = Document ()
Doc. read (protocolIn)
Return doc
#! /Usr/bin/python-I
# Coding: UTF-8
Import sys
Import OS
Dirname = OS. path. dirname (sys. argv [0])
Sys. path. append (dirname + '/gen-py ')
Sys. path. append ('/usr/lib/python2.7/site-packages ')
From cabinet import CabinetStorage
From document. ttypes import Document
From thrift import Thrift
From thrift. transport import TSocket
From thrift. transport import TTransport
From thrift. protocol import TCompactProtocol
From optparse import OptionParser
# Avoid UnicodeDecodeError
Reload (sys)
Sys. setdefaultencoding ("UTF-8 ")
Try:
# Command line arguments.
Parser = OptionParser (usage = "usage: % prog [options]", version = "% prog 1.0 ")
Parser. add_option ("-c", "-- host", dest = "host", action = "store", default = "127.0.0.1", type = "string ", help = "Specify cabinet host. ")
Parser. add_option ("-p", "-- port", dest = "port", action = "store", default = 9090, type = "int ", help = "Specify cabinet port. ")
(Options, args) = parser. parse_args ()
# Connect to cabinet server.
Transport = TSocket. TSocket (options. host, options. port)
Transport = TTransport. TBufferedTransport (transport)
Protocol = TCompactProtocol. TCompactProtocol (transport)
Client = CabinetStorage. Client (protocol)
Transport. open ()
Failed t Exception as e:
Print e
OS. _ exit (1)
# Welcome info.
Print "Cabinet Shell V1.0, Copyright I-MD 2013, All rights reserved ."
Print "For usage, invoke help (). \ n"
# Main functions.
Def help ():
Print "Main functions :"
Print "get (db, id )"
Print "set (db, id, val )"
Print "delete (db, id )"
Print "unpackDocInfo (docstr )"
Print "packDocInfo (doc )"
Def get (db, id ):
Return client. Get (db, id)
Def set (db, id, val ):
Return client. Set (db, id, val)
Def delete (db, id ):
Return client. delete (db, id)
Def packDocInfo (doc ):
TransportOut = TTransport. TMemoryBuffer ()
ProtocolOut = TCompactProtocol. TCompactProtocol (transportOut)
Doc. write (protocolOut)
Return transportOut. getvalue ()
Def unpackDocInfo (docstr ):
TransportIn = TTransport. TMemoryBuffer (docstr)
ProtocolIn = TCompactProtocol. TCompactProtocol (transportIn)
Doc = Document ()
Doc. read (protocolIn)
Return doc