返回首页

gbase数据、南大通用产品文档:GBase8aBlob Uri 大数据字段的存取

更新日期:2024年09月11日

支持GBase UP 的Blob Uri 字段的存取,使用时注意以下几点要求:

必须使用Prepare

必须使用Parameters

大文件必须使用FileStream 作为Parameters 的参数

Select 时必须将Blob Uri 字段放在所有字段最后

Select 时DataReader 必须使用CommandBehavior.SequentialAccess
流式读取模式

Select 时先执行GetBlobLength()获取Blob
Uri 字段值的长度,
再循
环执行GetBlob 获取
使用代码示例如下:
string connstr =
"server=192.168.8.29;database=song;uid=gbase;pwd=gbase20110531;pooling=false;
Ignore Prepare=false;";//连接参数中必须指定Ignore Prepare=false

FileStream fsin;
string filepath = @"d:\1.bmp";
string tablename = "test1";
fsin = new FileStream(filepath,FileMode.Open,FileAccess.Read);
long fsinlength = fsin.Length;
string sql = "drop table if exists " + tablename;
using (GBaseConnection conn = new GBaseConnection(connstr))
{
conn.Open();



GBase 8a 程序员手册ADO.NET 篇
南大通用数据技术股份有限公司

- 337 -
using (GBaseCommand cmd = new GBaseCommand(sql,conn))
{
cmd.ExecuteNonQuery();
cmd.CommandText = "create table " + tablename + " (a int, b blob uri, c
varchar(30))";
cmd.ExecuteNonQuery();

cmd.CommandText = "insert into " + tablename + " values(@a,@b,@c)";
cmd.CommandTimeout = 20000;
cmd.Parameters.AddWithValue("@a", 1);
cmd.Parameters.AddWithValue("@b", fsin); //将FileStream作为参数传入
cmd.Parameters.AddWithValue("@c", "song");
cmd.Prepare();//必须先Prepare
cmd.ExecuteNonQuery();
}
fsin.Close();

FileStream fsout;
string outpath = @"d:\outfile.bmp";
if (File.Exists(outpath))
{
File.Delete(outpath);
}
fsout = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write);

sql = "select a,c,b from " + tablename; //select时将blob uri字段放在最后
using (GBaseCommand cmd = new GBaseCommand(sql,conn))
{
cmd.Prepare();//开启prepare
cmd.CommandTimeout = 20000;//timeout设置大,防止在读取较大数据
时超时退出


GBase 8a 程序员手册ADO.NET 篇


- 338 -

南大通用数据技术股份有限公司
using (GBaseDataReader reader =
cmd.ExecuteReader(CommandBehavior.SequentialAccess)) //必须设置为流式读取
{
while (reader.Read())
{
Assert.AreEqual(1, reader.GetValue(0));
Assert.AreEqual("song", reader.GetValue(1));
long len = 0;
len = reader.GetBlobLength(2);//读取blob uri时,先获取列值的
长度 GetBlobLength
if (len == 0)
{
Console.WriteLine("blob uri is null");
}
Assert.AreEqual(len, fsinlength);//判断获取的长度等于存入时
的长度

byte[] result = null;
long alreadyRead = 0;//已经读取的长度
while (alreadyRead < len)//循环获取值
{
result = reader.GetBlob(2, alreadyRead); //读满,一个数据
包最大16M,内存够用 GetBlob
fsout.Write(result, 0, result.Length);
alreadyRead += result.Length;
}

fsout.Close();
}
}
}
}





BENCHMARK(count,expr)
函数说明
BENCHMARK()函数用于将表达式expr重复运行count次。
它可以用于计时GBase
8a MPP Cluster 处理表达式的时间,结果通常为0。在gccli 客户端使用它时,它
将返回查询执行所需的时间。
示例
将“hello”重复运行1000000 次。
gbase> SELECT BENCHMARK(1000000,'hello') FROM dual;
+----------------------------+
| BENCHMARK(1000000,'hello') |
+----------------------------+
|
0 |
+----------------------------+
1 row in set
说明
报告的时间是客户端操作的时间,不是服务器端的CPU 时间。计算
时间是应当执行BENCHMARK()多次,
并注意参考服务器的负载来解释结果。

GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
844

参数说明:控制优化器使用的排序步骤。
完全消除明确的排序是不可能的,但是关闭这
个变量可以让优化器在存在其他方法的时候优先选择其他方法。
该参数属于USERSET 类型参数,请参考表15-1 中对应设置方法进行设置。
取值范围:布尔型

on 表示使用。

off 表示不使用。
默认值:on