/
bytekeg.go
124 lines (91 loc) · 2.15 KB
/
bytekeg.go
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
124
package main
import (
r "github.com/dancannon/gorethink"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"time"
)
func CheckError(err error) {
if err != nil {
log.Println(err)
}
}
type TimeConfig struct {
ConnectTimeout time.Duration
ReadWriteTimeout time.Duration
}
func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, cTimeout)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(rwTimeout))
return conn, nil
}
}
func FetchURL(theurl string) string {
var client *http.Client
if proxy := os.Getenv("http_proxy"); proxy != `` {
proxyUrl, err := url.Parse(proxy)
CheckError(err)
transport := http.Transport{
Dial: TimeoutDialer(5*time.Second, 5*time.Second), // connect, read/write
Proxy: http.ProxyURL(proxyUrl),
}
client = &http.Client{Transport: &transport}
} else {
client = &http.Client{}
}
req, err := http.NewRequest(`GET`, theurl, nil)
CheckError(err)
resp, err := client.Do(req)
CheckError(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
CheckError(err)
return string(body)
}
type datum struct {
count int
data []string
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
var response []interface{}
var session *r.Session
session, err := r.Connect(map[string]interface{}{
"address": "localhost:28015",
"database": "test",
"maxIdle": 10,
"idleTimeout": time.Second * 10,
})
if err != nil {
panic(err)
}
r.Db("test").TableCreate("test").Exec(session)
objects := []interface{}{}
for i := 3000; i < 3001; i++ {
www := FetchURL("http://www.cnn.com")
row := map[string]interface{}{"id": i, "g1": 6771, "www": www}
objects = append(objects, row)
}
log.Println(objects)
query := r.Db("test").Table("Table1").Insert(objects)
_, err = query.Run(session)
if err != nil {
panic(err)
}
query = r.Db("test").Table("Table1").OrderBy("id")
rows, err := query.Run(session)
if err != nil {
panic(err)
}
err = rows.ScanAll(&response)
log.Println(response)
}