python操作Kudu


具體步驟如下:
import kudu
from kudu.client import Partitioning
from datetime import datetime

# 連接到kudu主服務器
client = kudu.connect(host='kudu.master', port=7051)

# 為新表定義架構
builder = kudu.schema_builder()
builder.add_column('key').type(kudu.int64).nullable(False).primary_key()
builder.add_column('ts_val', type_=kudu.unixtime_micros, nullable=False, compression='lz4')
schema = builder.build()

# 定義分區模式
partitioning = Partitioning().add_hash_partitions(column_names=['key'], num_buckets=3)

# 創建新表
client.create_table('python-example', schema, partitioning)

# 打開表
table = client.table('python-example')

# 創建一個新會話用於操作表
session = client.new_session()

# 往表插入數據
op = table.new_insert({'key': 1, 'ts_val': datetime.utcnow()})
session.apply(op)

# 更新插入,即有則更新,無則插入
op = table.new_upsert({'key': 2, 'ts_val': "2016-01-01T00:00:00.000000"})
session.apply(op)

# 更新行
op = table.new_update({'key': 1, 'ts_val': ("2017-01-01", "%Y-%m-%d")})
session.apply(op)

# 刪除數據
op = table.new_delete({'key': 2})
session.apply(op)

# 刷新寫入操作,如果發生異常,則捕獲異常並打印.
try:
session.flush()
except kudu.KuduBadStatus as e:
print(session.get_pending_errors())

# 創建一個掃描,並增加一個python-example表的斷言
scanner = table.scanner()
scanner.add_predicate(table['ts_val'] == datetime(2017, 1, 1))

# 打開掃描儀並讀取所有元組
# 注: 這不適用於大掃描
result = scanner.open().read_all_tuples()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM