Skynet

---------- ---------- 我的新 blog : liukaiyi.cublog.cn ---------- ----------

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  112 Posts :: 1 Stories :: 49 Comments :: 0 Trackbacks
虽然 mysql,oracle 和  Berkeley DB,sqlite3 等数据库已经很好
 但是当我初略学习下 数据挖掘方面的一些知识发现,关系数据库远远不够来存储,查询 etl 后的数据

比如:我希望原始日志数据进行某一字段的排序,是不是很简单 。
  有人说  - 数据导入数据库 load into table ... , select order by 。之
  还有人说 - linux sort -n...

恩!很好,下面我们对大小为 1TB 的数据开始进行这个简单的操作   -- 傻眼了 !!
   关于挖掘 - TB 级别的数量在我目前学习挖掘不到半年,就遇到过3-4次之多

解决办法:
对于这个问题 - 我现在希望能有个 大的链表 - (大到内存装不下)
  链表中的struct 结构为 :
   >> 排序属性文件归属
   >> 排序属性整条数据在文件中的 起始位置 - 结束位置
   >> 在排序中的排位 ( 链表结构,只记入比自己小的 属性在此链表的位置  )


比如 :
  1. 文件1内容 =>
说明:
完整数据描述 : 此数据在文件中的 起始位置(当然是通过程序取得的,这为了方便我标出)
..c.  0 
- 22
..a.  
23 - 55
..b.  
56- 76
..d.  
77 - 130
..f.  
131 - 220
..e.  
221 - 243

  2. 数据结构预开空间 100 byte
  3. 文件存储在描述 : # 链表排序我就不介绍了,数据结构的最基本技能,修改数据结构中的比自己小的指向
      我这就给出结果
{ /tmp/文件1, 0-22 ,  300 }   #说明 c : 在链表位置 0
{ /tmp/文件1, 23-55 , 200 }       # a : 100
{ /tmp/文件1, 56-76 , 0 }     # b : 200
{ /tmp/文件1, 77-130 , 500 }  # d : 300
{ /tmp/文件1, 131-220 ,  } # f : 400
{ /tmp/文件1, 221-243 , 400 } # e : 500

4. 倒叙输出 由小到到
     假设预存最小 为  200 链表位置
     找出 使用 open /tmp/文件1 
       并使用 seek 文件游标 定位  23-55 取出  ..a...
       根据 链表中 200 到 seek 56 76 取出 ..b...
       等等

当然 上面
  数据结构你可以使用 双向链表, btree , 红黑 , 斐波那契。。。( 数据结构终于感觉有用了,不枉费我考的软证啊!)


通过说明,我这 给大家提供个 可能需要的 技术细节 (py),不足之处 欢迎拍砖!!

1. 二进制文件 结构化 写,修改
#指定修改 190 byte 处的 内容
import os
from struct import *
fd 
= os.open( "pack1.txt", os.O_RDWR|os.O_CREAT )

ss 
= pack('ii11s'34'google')
os.lseek(fs, len(ss)
*10, 0) 
os.write(fs,ss) 
os.fsync(fs)

#os.close( fs )



2. seek 指定位置结构化读取


from struct import *
file_object 
= open('pack1.txt''rb')

def ts(si,ss=len(ss)):
    file_object.seek(si
*ss)
    chunk 
= file_object.read(ss)
    a,b,c
=unpack('ii11s', chunk )
    
print a,b,c

ts(10)
#输出 
3 4 google





1. 其他语言的 使用
struct 结构定义 ,在 python 中 使用  struct 包,这样序列出来的数据到文件中其他语言也可以使用
 参考: http://www.pythonid.com/bbs/archiver/?tid-285.html
pack1.py
from struct import *

# i 为 int(4)  11s 为预留 11 位置 的 string
# 此数据类型 为 19 byte ss 
= pack('ii11s'12'hello world')

= open("pack1.txt""wb")
f.write(ss)
f.close()


上面的代码往C的结构中写入数据,结构包括两个整型和一个字符串。
pack1.c
#include <stdio.h>
#
include <string.h>

struct AA
{
    int a;
    int b;
    char    c[
64];
};

int main()
{
    struct AA   aa;
    FILE    
*fp;
    int     size, readsize;
      
    memset(
&aa, 0, sizeof(struct AA));
   
    fp 
= fopen("pack1.txt""rb");
    
if (NULL == fp) {
        printf(
"open file error!"n");
        
return 0;
    }
   
    readsize 
= sizeof(struct AA);
    printf(
"readsize: %d"n", readsize);
  
    size 
= fread(&aa, 1, readsize, fp);   
    printf(
"read: %d"n", size);
    printf(
"a=%d, b=%d, c=%s"n", aa.a, aa.b, aa.c);
   
    fclose(fp);
   
    
return 0;
}

结果输出:
C:"Documents and Settings"lky"桌面"dataStructure>a
readsize: 72
read: 57
a=1, b=2, c=hello word



   
最后罗嗦下:
  能用数据结构了,很多东西都可以根据自己逻辑定制 存储很方便 。 不再受 关系数据库 , key 数据库 或 mapreduce 的限制
  
参考:
http://docs.python.org/library/struct.html#module-struct    #官方struct 包 说明
http://blog.csdn.net/JGood/archive/2009/06/22/4290158.aspx  # 使用 struct  的前辈留下的
http://www.tutorialspoint.com/python/os_lseek.htm #一个小demo
Python天天美味(17) - open读写文件










整理 www.blogjava.net/Good-Game
posted on 2009-11-04 15:16 刘凯毅 阅读(2095) 评论(0)  编辑  收藏 所属分类: python数据挖掘

只有注册用户登录后才能发表评论。


网站导航: