下面的代码演示了如何利用Linq结合AspNetPager控件实现分页功能,以及如果利用Linq
to Xml将当前页数据保存为Xml

pig中,limit可以取样少部分数据,但有很多问题,比如数据不能少于10条,否则返回全部。

必发bf88 1 using (NorthWindDataContext db = new NorthWindDataContext())
必发bf88 2必发bf88 3            必发bf88 4{               
必发bf88 5                int Page = Utils.GetPageIndex();//取得当前页码
必发bf88 6必发bf88 7                var s = from c in db.T_Tests orderby c.F_AutoId select new 必发bf88 8{ c.F_ID,F_AutoID = c.F_AutoId.ToString().PadLeft(8,’0′)};                             
var p = s.Skip((Page – 1) * this.AspNetPager1.PageSize).Take(this.AspNetPager1.PageSize);//取得当前页数据(注:先跳过(Page-1)*PageSize条记录后,再取PageSize条记录)
必发bf88 9                this.GridView3.DataSource = p; 
必发bf88 10                this.GridView3.DataBind();
必发bf88 11
必发bf88 12                this.AspNetPager1.RecordCount = s.Count();//设置分页控件的总记录数
必发bf88 13                this.AspNetPager1.CurrentPageIndex = Page;//设置分页控件的当前页
必发bf88 14
必发bf88 15
必发bf88 16                //将当前数据保存为xml
必发bf88 17                XDocument doc = new XDocument(new XElement(
必发bf88 18                                                            “T_Test”, from d in p                                                         
必发bf88 19                                                                        select 
必发bf88 20                                                                        (
必发bf88 21                                                                            new XElement
必发bf88 22                                                                            (
必发bf88 23                                                                                “data”,
必发bf88 24                                                                                new XAttribute(“F_ID”, d.F_ID),
必发bf88 25                                                                                new XAttribute(“F_AutoID”, d.F_AutoID)
必发bf88 26                                                                            )
必发bf88 27                                                                        )
必发bf88 28                                                           )
必发bf88 29                                               );
必发bf88 30                doc.Save(“c:\\demo.xml”); 
必发bf88 31
必发bf88 32                db.Dispose();//及时释放资源
必发bf88 33            }

今天又遇到另一个问题:

保存的Demo.Xml内容如下:

group后的数据,limit无效:也就是group后的数据,不能用limit,估计是含group结构不行(没验证)

必发bf88 34<?xml version=”1.0″ encoding=”utf-8″?>
必发bf88 35<T_Test>
必发bf88 36  <data F_ID=”9552e4dc-9a0a-414e-ab80-b0252f88411e” F_AutoID=”00000014″ />
必发bf88 37  <data F_ID=”2d135009-d24f-46ca-92ba-6d7b706f4025″ F_AutoID=”00000015″ />
必发bf88 38  <data F_ID=”2e725756-da8c-4ede-ba13-00aa91fbd8e2″ F_AutoID=”00000016″ />
必发bf88 39  <data F_ID=”d6a1b0aa-40e8-4cf8-a3f2-c1c28576d6f3″ F_AutoID=”00000017″ />
必发bf88 40  <data F_ID=”fed11493-6349-419e-a83b-c42f6d318735″ F_AutoID=”00000018″ />
必发bf88 41  <data F_ID=”c1eedd2b-2d92-46e3-8aab-f4180e106c6f” F_AutoID=”00000019″ />
必发bf88 42  <data F_ID=”762a57b5-8ca6-4d8c-9569-bbd62d4873a6″ F_AutoID=”00000020″ />
必发bf88 43  <data F_ID=”a468b3b0-1392-4bf5-be84-b2e1a90c53de” F_AutoID=”00000021″ />
必发bf88 44  <data F_ID=”d3526eff-23d4-41a4-b6b2-d4b1f5bf6085″ F_AutoID=”00000022″ />
必发bf88 45  <data F_ID=”6e0687ea-07be-4a83-a50f-4d3700868d15″ F_AutoID=”00000023″ />
必发bf88 46  <data F_ID=”f343f96f-8d2b-4ef5-a431-82df7f607d6e” F_AutoID=”00000024″ />
必发bf88 47  <data F_ID=”34d7d945-599c-4986-bdec-2ee7bf338e0f” F_AutoID=”00000025″ />
必发bf88 48  <data F_ID=”2c569843-ecfd-4f53-a59b-5e28083ed4e3″ F_AutoID=”00000026″ />
必发bf88 49</T_Test>

相比而言sample比较好,我试了一下是可以的,对group后的数据也能有作用

测试代码如下:

origin_cleaned_data = LOAD ‘$cleanedLog’ as 省略;

STORE  origin_cleaned_data INTO
‘/user/wizad/tmp/origin_cleaned_data’ USING PigStorage(‘,’);
DESCRIBE origin_cleaned_data;

test_data = foreach origin_cleaned_data generate $0,$1,$2,$3, $4;
ts_limit = limit origin_cleaned_data 100;–可以看到返回100条数据
STORE ts_limit INTO ‘/user/wizad/tmp/my’ USING PigStorage(‘,’);

g_log = group test_data by ($2,$4);
DESCRIBE g_log;

alldata = limit g_log 10;

dump alldata;–返回了全部数据,limit 无效

 

返回的group结构如下

origin_cleaned_data:
{

wizad_ad_id: chararray,
guid: chararray,
Android_id: chararray,

imei: chararray,

app_category_id: chararray

}

g_log: {
group: (android_id: chararray,app_category_id: chararray),
test_data: {
wizad_ad_id: chararray,
guid: chararray,
android_id: chararray,
imei: chararray,

app_category_id: chararray
}
}

 

 

 

 

 

 

 

 

对sample的测试:

test_data = foreach origin_cleaned_data generate
wizad_ad_id,guid,android_id,imei,app_category_id;
g_log = group test_data by ($2,$4);
DESCRIBE g_log;

myts = sample g_log 0.0001;
STORE myts INTO ‘/user/wizad/tmp/my’ USING PigStorage(‘,’);

dump myts;
describe myts;

 

 

结果如下:

g_log: {
group: (android_id: chararray,app_category_id: chararray),
test_data: {wizad_ad_id: chararray,guid: chararray,android_id:
chararray,imei: chararray,app_category_id: chararray}
}

myts: {
group: (android_id: chararray,app_category_id: chararray),
test_data: {wizad_ad_id: chararray,guid: chararray,android_id:
chararray,imei: chararray,app_category_id: chararray}
}

((-1,),{(74,9051235c-a391-4dae-ab22-f93d24a12636,-1,-1,),(75,053e9f48-03bf-4b39-9455-ff412a725a3c,-1,-1,),(75,e77c7cd1-c2ae-4fdb-bc07-992235b97b58,-1,-1,),(74,f2752e0b-b5e6-4310-849a-12c5dd10a0c5,-1,-1,),(74,a9072546-3cd9-4215-9d0c-b0632d201505,-1,-1,),(74,68b5a933-f452-4850-8b93-59acd60b0e9a,-1,-1,),(74,8c4d6d1c-644c-4287-9a86-3117bfaf430b,-1,-1,),(74,a4efdcb5-9a2e-42c4-8c8f-d32c0a22e765,-1,-1,),(74,49d6e13e-4080-4739-b0dc-ad3f89e4bdf9,-1,-1,),(74,7982b4b1-d925-4404-91c6-e59d96f2a074,-1,-1,),(74,051d62e1-634e-4a22-abe6-d2653c3165fb,-1,-1,),(74,2e7350b4-450a-4124-9e5b-ed083dcef902,-1,-1,),(74,5a7cf340-4f66-4b43-862e-49acd8beeea3,-1,-1,),(74,e6abe1d5-3070-4e2a-9e86-358b21f0cd6b,-1,-1,),(74,5d6f3f9a-d194-4ccd-b465-25703c8047e0,-1,-1,),(74,76aa0b57-c68c-42f6-ba4b-bbd5a386eb1a,-1,-1,),(74,915ec619-52dd-4bd1-b8d5-2479c9a5467a,-1,-1,),(74,433521f3-e677-4159-80bc-f918ba5bd49c,-1,-1,),(74,51e0b6f3-26a2-4531-9a0f-337f25a4c171,-1,-1,),(74,dfe58439-29d6-4a05-84d1-0dd0ee033dc7,-1,-1,),(74,f46d55c8-5f5f-4f50-a9ef-75d3f25ea21c,-1,-1,),(74,2623d054-dbe1-433e-97c6-5d568d49442f,-1,-1,),(74,86bc62e7-7e96-415a-a5bc-2d8e1483e6db,-1,-1,),(74,68a5a8b7-4b4c-49ce-b323-3baebd9f814f,-1,-1,),(74,5d58bc0e-14a9-45ec-bc2c-9d06780b2d9b,-1,-1,),(74,d896d166-327e-4187-a4c6-4c2fd58876a5,-1,-1,),(74,e214a7f9-2c2c-4875-85ac-011b431296f3,-1,-1,),(74,3743bc6a-09af-4d44-a058-9caf7b2374dd,-1,-1,),(74,777c5eb8-0b12-46fb-968f-9e9679794abf,-1,-1,),(74,550e9982-f62d-4bba-bce9-5fb98ea28e22,-1,-1,),(74,68e96b35-62a4-4160-aa56-b9f7aed4da52,-1,-1,),(74,1b5dc09b-f03c-4fa3-b290-7388afacbea7,-1,-1,),(74,44e4afc0-e439-4853-a004-9467d363dd0a,-1,-1,),(74,05f01d54-2c5c-44c1-a24b-213f1b0a184d,-1,-1,),(74,169f9282-0437-43af-98c4-e078fc360533,-1,-1,),(74,2bb741f9-170f-4fa4-955f-370605f8261d,-1,-1,),(74,9bc1a4dc-0324-4007-8be0-93a4b3286b49,-1,-1,),(74,2e820c47-bce6-45df-a1e6-3751f76b11f5,-1,-1,),(74,e2b26638-c2bb-4375-8498-bc94bf590b18,-1,-1,),(74,a02e372d-6773-4282-9925-44def1d6c564,-1,-1,),(74,b7da49ce-5ba3-41d4-ba18-8eade05c9745,-1,-1,),(74,b44017a5-b5c9-45ba-8903-6a6de996772b,-1,-1,),(74,da0e84c3-78f3-4b19-a695-c00d8f114299,-1,-1,),(74,f935cf93-8867-4496-8315-bd4045b1b6b9,-1,-1,),(74,5827b614-03cc-47a5-bc40-5c6952488617,-1,-1,),(74,8771188e-cc86-43b8-bcc1-ad632dfd56cb,-1,-1,),(74,2619bcf9-bed6-45de-a25e-003cd73a9ffc,-1,-1,),(74,1dd06a98-cf68-4130-9f0f-d2dcf1809085,-1,-1,),(74,b3f2c66a-6988-4ede-8f11-66e34764e93c,-1,-1,),(74,b1ab5c0b-5e1c-4e0f-99eb-07dc7722b7a4,-1,-1,),(74,c0698e6f-6e2e-4456-8bc0-c438276f0b36,-1,-1,),(74,feb44a38-ac42-4fb4-a7f1-30d087709a97,-1,-1,),(74,0387bcf5-04e2-49f9-af3c-65bb6875b09d,-1,-1,),(74,f3711f23-2a34-49af-89f8-130f299eb17d,-1,-1,),(74,85f13431-5be6-4d72-ad97-9873b2c6c2e7,-1,-1,),(74,21ca723c-ec2b-4242-8108-b95436f10e3e,-1,-1,),(74,fec1932a-b0e4-4bf0-b504-8ed8f3c159e7,-1,-1,),(74,d74374ec-8cf4-4c4a-b598-9631f6972cbb,-1,-1,),(74,6780962a-bf75-4c4c-a557-94a7de5a3e36,-1,-1,),(74,14517915-ee3f-4d34-943f-d6f1813afdef,-1,-1,),(74,c5547aca-3b8b-4108-93ba-bf365c106cdd,-1,-1,),(74,e9a986c1-6868-4f7f-baf6-69d8c302583e,-1,-1,),(74,9c1341cf-45b8-48c6-b699-33b1a4215c66,-1,-1,),(74,f16e6222-a84b-4758-ae71-0613c8f34b29,-1,-1,),(74,47cc25ef-05bc-47f4-a32b-3cddaf0ac22b,-1,-1,),(74,d5c1b6b0-38c3-464b-8cb9-70ced875be5f,-1,-1,),(74,6a4f782a-1f5c-45c0-bb3a-4df25c436be3,-1,-1,),(74,23bb2f0c-d629-479d-800e-b86fc3d6e45c,-1,-1,)})
((a50a17bde79ac018,),{(74,863010025134441,a50a17bde79ac018,863010025134441,)})
((a51779f736cd3f54,),{(74,862949029595753,a51779f736cd3f54,862949029595753,)})
((c7ae5867-3b77-4987-b082-ed3867b5c384,),{(74,353627055387065,c7ae5867-3b77-4987-b082-ed3867b5c384,353627055387065,)})

Pig 的安装与测试
http://www.linuxidc.com/Linux/2014-07/104039.htm

Pig安装与配置教程
http://www.linuxidc.com/Linux/2013-04/82785.htm

Pig 安装部署及MapReduce模式下测试
http://www.linuxidc.com/Linux/2013-04/82786.htm

Pig安装及本地模式测试,体验
http://www.linuxidc.com/Linux/2013-04/82783.htm

Pig的安装配置与基本使用
http://www.linuxidc.com/Linux/2013-02/79928.htm

Hadoop Pig进阶语法
http://www.linuxidc.com/Linux/2013-02/79462.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2014-10/108766.htm

必发bf88 50

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注