首页 > 解决方案 > Apache Ignite,C#/.NET:BinaryObject 警告 - Execpted 和 Actual ValueType 不相等,无法查询条目

问题描述

我试图将 BinaryObjects 存储在缓存中,特别是按照这个示例。但是,当缓存被填充时,我无法对条目进行 SQL 查询。

当我得到 的输出时>>> BinaryObject.cs - Person: Cache Loaded. Size: 10,我也收到以下警告:

[12:45:33,939][WARNING][main][GridQueryProcessor] Key-value pair is not inserted into any SQL table [cacheName=default, expValType=BinaryObject.Student, actualValType=Student]
[12:45:33,940][WARNING][main][GridQueryProcessor]   ^-- Value type(s) are specified via CacheConfiguration.indexedTypes or CacheConfiguration.queryEntities
[12:45:33,947][WARNING][main][GridQueryProcessor]   ^-- Make sure that same type(s) used when adding Object or BinaryObject to cache
[12:45:33,953][WARNING][main][GridQueryProcessor]   ^-- Otherwise, entries will be stored in cache, but not appear as SQL Table rows

这绝对是我在 SQL 查询中看不到输出的原因:

>>> All person names:

我有以下代码:

程序.cs

namespace BinaryObject
{
    class Program
    {
        static void Main(string[] args)
        {
            var cfg = new IgniteConfiguration
            {
                // TCPDiscoverySpi Configuration
            };

            using (IIgnite ignite = Ignition.Start(cfg))
            {
                // Establish database connection; query database
                DBConnection dbc = new DBConnection();
                OracleConnection con = dbc.EstablishConnection();
                OracleCommand cmd = dbc.CreateCommand(con, "SELECT * FROM STUDENT WHERE ROWNUM <= 10");
                OracleDataReader reader = dbc.DatabaseReader(cmd);

                var cache0 = ignite.GetOrCreateCache<object, object>(new CacheConfiguration
                {
                    Name = "default",
                    Backups = 1,
                    QueryEntities = new[]
                    {
                        new QueryEntity(typeof(int), typeof(Student))
                    }
                });

                var cache = cache0.WithKeepBinary<int, IBinaryObject>();
                
                cache.Clear();

                IBinary binary = cache.Ignite.GetBinary();

                while (reader.Read())
                {
                    Student.SetStudentObject(reader, binary, cache);
                }

                Console.WriteLine();
                Console.WriteLine(">>> Binary Object - Student: Cache Loaded. Size: " + cache.GetSize() );
                Console.WriteLine();
                SqlQueryExample(cache);
            }
        }

        private static void SqlQueryExample(ICache<int, IBinaryObject> cache)
        {
            var qry = cache.Query(new SqlFieldsQuery("select FirstName from Student"));

            Console.WriteLine();
            Console.WriteLine(">>> All student names:");

            foreach (var row in qry)
                Console.WriteLine(">>>     " + row[0]);
        }
    }
}

学生.cs

namespace BinaryObject
{
    class Student
    {
        [QuerySqlField(IsIndexed = true)]
        public int StudentId { get; set; }
        [QuerySqlField]
        public string FirstName { get; set; }
        [QuerySqlField]
        public string LastName { get; set; }
        [QuerySqlField]
        public string Major { get; set; }

        public void SetStudentObject(OracleDataReader reader)
        {
            cache[reader.IsDBNull(0) ? 0 : reader.GetInt32(0)] = binary.GetBuilder("Student")
                .SetField("Student", reader.IsDBNull(0) ? 0 : reader.GetInt32(0))
                .SetField("FirstName", reader.IsDBNull(5) ? "" : reader.GetString(5))
                .SetField("LastName", reader.IsDBNull(2) ? "" : reader.GetString(2))
                .SetField("Major", reader.IsDBNull(1) ? "" : reader.GetString(1))
                .Build();
        }
    }
}

标签: c#ignite

解决方案


查看错误消息:expValType=BinaryObject.Student, actualValType=Student。创建二进制对象时必须使用完整的类型名称。

替换binary.GetBuilder("Student")binary.GetBuilder("BinaryObject.Student")


推荐阅读