sparksql中如何实现对Sequoiadb数组类型字段的查询

本文介绍了如何在Sequoiadb数据库中使用Spark SQL查询数组数据。通过具体示例,展示了创建集合、数据表及如何利用数组中的特定信息进行查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sequoiadb数据库是国产的企业级分布式数据库,Sequoiadb本身是key-value格式的nosql数据库,上层使用spark做SQL解析层,本文介绍如何使用sparksql查询Sequoiadb数组。

下面举一个具体的例子来说明:

1. 在SDB中创建集合,里面包含数据对象

db.foo.createCL("array1", {ShardingKey:{_id:1}, ShardingType:"hash", AutoSplit:true})

db.foo.array1.insert({id:1, empList: [{name:"Tom", age:30}, {name:"Jack", age:40}]})

db.foo.array1.insert({id:2, empList: [{name:"Nacy", age:25}, {name:"Wendy", age:35}]})

db.foo.createCL("array2", {ShardingKey:{_id:1}, ShardingType:"hash", AutoSplit:true})

db.foo.array2.insert({id:3, empList: [{name:"Tom", age:30}, {name:"Jack", age:40}]})

db.foo.array2.insert({id:4, empList: [{name:"Nacy", age:25}, {name:"Wendy", age:35}]})

 

2. 在spark-sql中创建对应的数据表:

CREATE table sdb_array1 ( id int, empList array<struct<name:string, age:int>>) using com.sequoiadb.spark OPTIONS ( host 'sdbserver1:11810', collectionspace 'foo', collection 'array1');

CREATE table sdb_array2 ( id int, empList array<struct<name:string, age:int>>) using com.sequoiadb.spark OPTIONS ( host 'sdbserver1:11810', collectionspace 'foo', collection 'array2');

select * from sdb_array1;

select * from sdb_array2;

注意:

基本的模式是 array<TYPE> 和 struct<COLUMN:TYPE, COLUMN:TYPE, ...>,上面的用法是两者的组合。

 

Hive和Spark数据类型的说明,请参考下面的文档:

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

https://spark.apache.org/docs/1.2.0/sql-programming-guide.html

 

3. 以数组中的特定信息作为查询条件:

select * from sdb_array1 where empList[0].name='Tom';

select * from sdb_array2 where empList[1].name='Wendy';

select * from sdb_array1 where empList[0].age=30;

select * from sdb_array2 where empList[1].age=35;

select * from sdb_array1 where empList[0].name='Tom' union all select * from sdb_array2 where empList[1].age=35;



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值