从Cassandra数据库表中探索和导入数据
本示例展示了如何探索Apache™Cassandra®数据库的结构,并使用Cassandra数据库连接与Apache Cassandra数据库c++接口将数据从Cassandra数据库表导入到MATLAB®。Cassandra数据库根据分区键存储数据库表。分区键影响如何在数据库中筛选数据。
在本例中,Cassandra数据库包含employees_by_job
数据库表中包含员工数据和job_id
分区键。
创建Cassandra数据库连接
使用配置的数据源创建Cassandra数据库连接CassandraDataSource
以及一个空白的用户名和密码。的apacheCassandra
函数返回康涅狄格州
作为一个连接
对象。
数据源=“CassandraDataSource”;用户名="";密码="";conn = apacheCassandra(数据源,用户名,密码);
探索Cassandra数据库结构
使用Cassandra数据库连接查看Cassandra数据库中的键空间。返回的字符串数组包含键空间。一个键空间包含一个或多个数据库表,并定义数据库如何复制表中的数据。
conn.Keyspaces
ans =6×1的字符串"employeedata" "system" "system_auth" "system_distributed" "system_schema" "system_traces"
中返回Cassandra数据库表的名称employeedata
用于。t
中包含数据库表的名称的字符串数组employeedata
用于。
用于=“employeedata”;T = tablenames(conn,keyspace)
t =3×1的字符串"employees_by_id" "employees_by_job" "employees_by_name"
控件中Cassandra数据库列的名称employees_by_job
数据库表中。
表名=“employees_by_job”;Cols = columninfo(conn,keyspace,tablename);
控件中显示Cassandra数据库列的前几个名称employees_by_job
数据库表中。
头(峡路)
ans =8×4表数据类型名称PartitionKey ClusteringColumn ________________ ________ ____________ ________________ " job_id”“文本”真正的“”“hire_date”“日期“false”DESC”“employee_id”“int“false”ASC”“commission_pct”“双“false”“department_id”“int“false”“电子邮件”、“文本“false”“first_name”“文本“false”“last_name文本“false””
关口
是一个包含以下变量的表:
的名字
- Cassandra数据库列名称数据类型
—Cassandra数据库列的CQL (Query Language)数据类型PartitionKey
—分区键指示灯ClusteringColumn
-聚类列指示器
的值PartitionKey
Variable指示数据库列是否为分区键。列job_id
(作业标识符)是这个数据库表中的一个分区键。
从Cassandra数据库导入数据
从employees_by_job
数据库表转换成MATLAB。这个数据库有关于店员的数据,所以使用分区键值SH_CLERK
.
keyValue =“SH_CLERK”;结果= partitionRead(conn,keyspace,tablename,...keyValue);
显示返回的雇员数据的前几行。
(结果)
ans =表8×13job_id hire_date employee_id commission_pct department_id邮件first_name last_name manager_id办公室performance_ratings phone_number工资建筑房间 __________ ___________ ___________ ______________ _____________ __________ __________ __________ __________ ________________ ___________________ ______________ ______ " SH_CLERK“03 - 183年2月- 2008年南50”GGEONI”“吉拉德”“Geoni西方国家“120”“121”{[2]}650.507.9879“2800”SH_CLERK“13 - 199年1月- 2008年南50”DGRANT”“道格拉斯授予“124”西”139 {3×1 int32} "650.507.9844" 2600 "SH_CLERK" 19- 12 -2007 191 NaN 50 "RPERKINS" "Randall" "Perkins" 122 "West" 255 {[2]} "650.505.4876" 2500 "SH_CLERK" 21- june -2007 182 NaN 50 " msullivan " "Martha" "Sullivan" 120 "East" 112 {2×1 int32} "650.507.9878" 2500 "SH_CLERK" 21- june -2007 198 NaN 50 "DOCONNEL" "Donald" OConnell" 124 "East" 125 {3×1 int32} "650.507.9833" 2600 "SH_CLERK" 17- 3 -2007 195 NaN 50 "VJONES" "Vance" "Jones" 123 "West" 236 {5×1 int32} "650.501.4876" 2800 "SH_CLERK" 07- 2 -2007 187南50 "ACABRIO" "Anthony" "Cabrio" 121 "西" 345 {3×1 int32} "650.509.4876" 3000 "SH_CLERK" 11-七月-2006 190南50 "TGATES" "Timothy" "Gates" 122 "东" 297 {3×1 int32} "650.505.3876" 2900
结果
是一个包含以下变量的表:
job_id
-工作标识符hire_date
-租用日期employee_id
-员工标识commission_pct
-佣金百分比department_id
-部门标识电子邮件
-电子邮件地址first_name
-名字last_name
-姓氏manager_id
—管理员标识符办公室
-办公室位置(包含建筑和房间两个变量的表格)performance_ratings
-性能评级phone_number
-电话号码工资
- - - - - -工资
关闭Cassandra数据库连接
关上(康涅狄格州)
另请参阅
apacheCassandra
|表名
|columninfo
|partitionRead
|关闭