読者です 読者をやめる 読者になる 読者になる

Cassandra+PythonでLongTypeのColumnを操作する

python Cassandra

PythonからLongTypeのColumnを操作する際は以下のように書く。

# -*- coding:utf-8 -*-
from thrift import Thrift
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated
from cassandra import Cassandra
from cassandra.ttypes import *
import time
import struct

socket = TSocket.TSocket('localhost', 9160)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
client = Cassandra.Client(protocol)
socket.open()

keyscpace = 'Keyspace'
key = 'key'
column_path = ColumnPath(column_family='LongCF', column=struct.pack('!q', 10)) 
value = 'hoge'
timestamp = time.time() * 1e6
consistency = ConsistencyLevel.ONE

client.insert(keyscpace, key, column_path , value, timestamp, consistency)

LongTypeの時は struct.pack('!q', 10) みたく変換したらおk。
ちなみにcassandra-cliではLongTypeとかUUIDTypeのColumnは操作できんので注意。