Finology 大数据金融

通过大数据以量化金融

从关系型数据库迁移数据到Elasticsearch时,总要处理很多关联数据,如何进行数据建模,下面给出了四种方案可供大家参考。

其中最简单的一种就是数据冗余扁平化,这个不做过多讲解。

应用层联接有点类型关系型数据库的子查询。第一次查询的结果作为第二次查询的条件。

1
2
3
4
5
6
7
8
9
10
11
12
13
PUT /my_index/user/1 
{
"name": "John Smith",
"email": "john@smith.com",
"dob": "1970/10/24"
}

PUT /my_index/blogpost/2
{
"title": "Relationships",
"body": "It's complicated...",
"user": 1
}

blogpost 通过用户的 id 链接到用户。

通过用户的 ID 1 可以很容易的找到博客帖子。

1
2
3
4
5
6
7
8
9
10
GET /my_index/blogpost/_search
{
"query": {
"filtered": {
"filter": {
"term": { "user": 1 }
}
}
}
}

为了找到用户叫做 John 的博客帖子,我们需要运行两次查询。先查询名字包含 John 的所有用户的 id 集合,再像上面一样根据 id 查询 blogpost。

执行第一个查询得到的结果将填充到 terms 过滤器中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GET /my_index/user/_search
{
"query": {
"match": {
"name": "John"
}
}
}

GET /my_index/blogpost/_search
{
"query": {
"filtered": {
"filter": {
"terms": { "user": [1, 3, 7] }
}
}
}
}

总结:应用层联接的主要优点是可以对数据进行标准化处理。缺点就是需要2次查询,有时间消耗。
如果说叫 John 的用户有很多,比如百万以上,那查询是非常没有效率的。
这种方法适合于 user 只有少量文档的情况,并且最好它们很少改变,这将允许应用程序对结果进行缓存,避免经常运行第一次查询。

eg. 搜索用户名称和博客标题,展示用户及其最相关的博客列表。

需要按用户名称进行分组,根据score进行排序选TOPN。

https://www.elastic.co/guide/cn/elasticsearch/guide/current/top-hits.html

eg. 文件目录的搜索,可以参考如下链接。

https://www.elastic.co/guide/cn/elasticsearch/guide/current/denormalization-concurrency.html

eg. 并发问题

https://www.elastic.co/guide/cn/elasticsearch/guide/current/concurrency-solutions.html

Windows10环境,某天突然就没法使用 git, svn了,IntelliJ IDEA下面的git, svn也没法正常使用。

使用git desktop时,提示cannot publish unborn HEAD

使用 cmd 时,也会提示信息 ANOMALY: use of REX.w is meaningless (default operand size is 64)

导致这个事情的原因是Win10的自动更新和某监控软件产生了冲突。

使用如下语句可以查看到这个软件的连接。

netstat -ano | findstr 8237

我们可以通过修改注册表的方式解决上述问题。

在运行中输入regedit打开注册表,然后打开到目录计算机\HKEY_LOCAL_MACHINE\SOFTWARE\TEC\Ocular.3\agent\config 下新建 字符串值
hookapi_filterproc_external,值为cmd.exe;powershell.exe;git.exe;idea64.exe

重启软件,一切恢复正常。

生产者消费者模型的特点:

保证生产者不会在缓冲区满的时候继续向缓冲区放入数据,而消费者也不会在缓冲区空的时候,消耗数据。

当缓冲区满的时候,生产者会进入休眠状态,当下次消费者开始消耗缓冲区的数据时,生产者才会被唤醒,开始往缓冲区中添加数据;当缓冲区空的时候,消费者也会进入休眠状态,直到生产者往缓冲区中添加数据时才会被唤醒。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;


public class ProducerAndConsumerDemo {

public static void main(String[] args) {

Store store = new Store(10);

new Thread(new Producer(store), "producer").start();
new Thread(new Consumer(store), "consumer1").start();
new Thread(new Consumer(store), "consumer2").start();
}
}


@Slf4j
class Store {

private Integer capacity;
private Integer count = 0;

public Store(Integer capacity) {
this.capacity = capacity;
}

public Boolean isFull() {
return this.count >= this.capacity;
}

public Boolean isEmpty() {
return this.count <= 0;
}

synchronized void produce() throws InterruptedException {

if (this.isFull()) {

log.info("仓库满了,停止生产");
wait();

} else if (this.isEmpty()) {

log.info("仓库空了, 放入第" + count + "号位置,并通知消费者");
this.count++;
TimeUnit.SECONDS.sleep(1);
notifyAll();

} else {

log.info("放入第" + count + "号位置");
this.count++;
TimeUnit.SECONDS.sleep(1);

}

}

synchronized void consume() throws InterruptedException {

if (this.isEmpty()) {

log.info("仓库空了,停止消费");
wait();

} else if (this.isFull()) {

this.count--;
log.info("仓库满了,取出第" + count + "号位置, 并通知生产者");
TimeUnit.SECONDS.sleep(2);
notifyAll();

} else {

this.count--;
log.info("取出第" + count + "号位置");
TimeUnit.SECONDS.sleep(2);

}
}
}

class Producer implements Runnable {

private Store store;

public Producer(Store store) {
this.store = store;
}

@Override
public void run() {

while (true) {
try {
store.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class Consumer implements Runnable {

private Store store;

public Consumer(Store store) {
this.store = store;
}

@Override
public void run() {
while (true) {
try {
store.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
0%