Пример #1
0
        static void Main4(string[] args)
        {
            using (SQLiteConnection conn
                       = new SQLiteConnection(@"Data Source=G:\其他资料\技术资料\各种海量数据\VeryCD\verycd.sqlite3.db"))
            {
                conn.Open();
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from verycd";
                    using (var reader = cmd.ExecuteReader())
                    {
                        ElasticConnection client = new ElasticConnection("localhost", 9200);
                        var serializer           = new JsonNetSerializer();
                        while (reader.Read())
                        {
                            long   verycdid  = reader.GetInt64(reader.GetOrdinal("verycdid"));
                            string title     = reader.GetString(reader.GetOrdinal("title"));
                            string status    = reader.GetString(reader.GetOrdinal("status"));
                            string brief     = reader.GetString(reader.GetOrdinal("brief"));
                            string pubtime   = reader.GetString(reader.GetOrdinal("pubtime"));
                            string updtime   = reader.GetString(reader.GetOrdinal("updtime"));
                            string category1 = reader.GetString(reader.GetOrdinal("category1"));
                            string category2 = reader.GetString(reader.GetOrdinal("category2"));
                            string ed2k      = reader.GetString(reader.GetOrdinal("ed2k"));
                            string content   = reader.GetString(reader.GetOrdinal("content"));
                            string related   = reader.GetString(reader.GetOrdinal("related"));

                            VerycdItem item = new VerycdItem();
                            item.verycdid  = verycdid;
                            item.title     = title;
                            item.status    = status;
                            item.brief     = brief;
                            item.pubtime   = pubtime;
                            item.updtime   = updtime;
                            item.category1 = category1;
                            item.category2 = category2;
                            item.ed2k      = ed2k;
                            item.content   = content;
                            item.related   = related;

                            Console.WriteLine("当前读取到id=" + verycdid);
                            IndexCommand indexCmd = new IndexCommand("verycd", "items", verycdid.ToString());
                            //Put()第二个参数是要插入的数据
                            OperationResult result      = client.Put(indexCmd, serializer.Serialize(item));
                            var             indexResult = serializer.ToIndexResult(result.Result);
                            if (indexResult.created)
                            {
                                Console.WriteLine("创建了");
                            }
                            else
                            {
                                Console.WriteLine("没创建" + indexResult.error);
                            }
                        }
                    }
                }
            }



            Console.ReadKey();
        }
Пример #2
0
        /// <summary>
        /// 把sqllite中的数据存入elasticSearch中
        /// </summary>
        /// <param name="args"></param>
        static void Main3(string[] args)
        {
            //安装sqlLite的ado.net sdk:Install-Package System.Data.SQLite.Core
            //创建SQLite数据库连接(直接连接文件地址)
            using (SQLiteConnection conn
                       = new SQLiteConnection(@"Data Source=C:\Users\Administrator\Desktop\掌上租项目\电驴数据库(全部电影)\verycd.sqlite3.db"))
            {
                //打开数据库连接
                conn.Open();
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from verycd";
                    //读取数据
                    using (var reader = cmd.ExecuteReader())
                    {
                        //连接elasticSearch
                        ElasticConnection client = new ElasticConnection("localhost", 9200);
                        var serializer           = new JsonNetSerializer();
                        while (reader.Read())
                        {
                            //将读取的数据赋值
                            long   verycdid  = reader.GetInt64(reader.GetOrdinal("verycdid"));
                            string title     = reader.GetString(reader.GetOrdinal("title"));
                            string status    = reader.GetString(reader.GetOrdinal("status"));
                            string brief     = reader.GetString(reader.GetOrdinal("brief"));
                            string pubtime   = reader.GetString(reader.GetOrdinal("pubtime"));
                            string updtime   = reader.GetString(reader.GetOrdinal("updtime"));
                            string category1 = reader.GetString(reader.GetOrdinal("category1"));
                            string category2 = reader.GetString(reader.GetOrdinal("category2"));
                            string ed2k      = reader.GetString(reader.GetOrdinal("ed2k"));
                            string content   = reader.GetString(reader.GetOrdinal("content"));
                            string related   = reader.GetString(reader.GetOrdinal("related"));

                            //创建一个对象,把数据库每条数据给一个对象
                            VerycdItem item = new VerycdItem();
                            item.verycdid  = verycdid;
                            item.title     = title;
                            item.status    = status;
                            item.brief     = brief;
                            item.pubtime   = pubtime;
                            item.updtime   = updtime;
                            item.category1 = category1;
                            item.category2 = category2;
                            item.ed2k      = ed2k;
                            item.content   = content;
                            item.related   = related;

                            Console.WriteLine("当前读取到id=" + verycdid);
                            //创建一个elasticSearch(数据库,表名,主键)
                            IndexCommand indexCmd = new IndexCommand("verycd", "items", verycdid.ToString());
                            //Put()第二个参数是要插入的数据(把数据序列化存入elasticSearch)
                            OperationResult result      = client.Put(indexCmd, serializer.Serialize(item));
                            var             indexResult = serializer.ToIndexResult(result.Result);
                            if (indexResult.created)
                            {
                                Console.WriteLine("创建了");
                            }
                            else
                            {
                                Console.WriteLine("没创建" + indexResult.error);
                            }
                        }
                    }
                }
            }



            Console.ReadKey();
        }