Stu_Classbasic_infoclass_infonameagesexcidcname1001Tom25male3-105计算机导论1002Arie23male3-245操作系统1003Jim17female6-166数字电路1004Lily21female3-245操作系统1、使用API在HBase中创建Stu_Class表并在Hbaseshell中展示所有的表。2、使用API将数据插入
- 使用API在HBase中创建Stu_Class表,并在HBase shell中展示所有的表。
import happybase
connection = happybase.Connection('localhost')
connection.create_table(
'Stu_Class',
{
'info': dict(max_versions=1),
}
)
# 在HBase shell中展示所有的表
list
- 使用API将数据插入到表中
table = connection.table('Stu_Class')
data = {
'name': b'Tom',
'age': b'25',
'sex': b'male',
'cid': b'3-105',
'cname': b'计算机导论'
}
table.put(b'1001', data)
data = {
'name': b'Arie',
'age': b'23',
'sex': b'male',
'cid': b'3-245',
'cname': b'操作系统'
}
table.put(b'1002', data)
data = {
'name': b'Jim',
'age': b'17',
'sex': b'female',
'cid': b'6-166',
'cname': b'数字电路'
}
table.put(b'1003', data)
data = {
'name': b'Lily',
'age': b'21',
'sex': b'female',
'cid': b'3-245',
'cname': b'操作系统'
}
table.put(b'1004', data)
- 使用Shell命令查询表中所有数据
scan 'Stu_Class'
- 使用Shell将1003行的cname改为网络技术,并查询1003行修改后的数据
put 'Stu_Class', '1003', 'info:cname', '网络技术'
get 'Stu_Class', '1003'
- 使用API查询选修课程为3-245的学生信息
from typing import List
def get_students_by_course_id(course_id: bytes) -> List[dict]:
table = connection.table('Stu_Class')
students = []
for key, data in table.scan():
if data[b'cid'] == course_id:
student = {
'id': key.decode(),
'name': data[b'name'].decode(),
'age': int(data[b'age'].decode()),
'sex': data[b'sex'].decode(),
'cid': data[b'cid'].decode(),
'cname': data[b'cname'].decode()
}
students.append(student)
return students
get_students_by_course_id(b'3-245')
``
原文地址: https://www.cveoy.top/t/topic/gBin 著作权归作者所有。请勿转载和采集!