本文整理匯總了Golang中github.com/henrylee2cn/pholcus/common/util.JsonString函數的典型用法代碼示例。如果您正苦於以下問題:Golang JsonString函數的具體用法?Golang JsonString怎麽用?Golang JsonString使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了JsonString函數的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Push
func (p *KafkaSender) Push(data map[string]interface{}) error {
val := util.JsonString(data)
_, _, err := producer.SendMessage(&sarama.ProducerMessage{
Topic: p.topic,
Value: sarama.StringEncoder(val),
})
return err
}
示例2: init
func init() {
Output["mysql"] = func(self *Collector, dataIndex int) {
db, ok := mysql.MysqlPool.GetOne().(*mysql.MysqlSrc)
if !ok || db == nil {
logs.Log.Error("鏈接Mysql數據庫超時,無法輸出!")
return
}
defer mysql.MysqlPool.Free(db)
var mysqls = make(map[string]*mysql.MyTable)
var namespace = util.FileNameReplace(self.namespace())
for _, datacell := range self.DockerQueue.Dockers[dataIndex] {
subNamespace := util.FileNameReplace(self.subNamespace(datacell))
var tName = namespace
if subNamespace != "" {
tName += "__" + subNamespace
}
if _, ok := mysqls[subNamespace]; !ok {
mysqls[subNamespace] = mysql.New(db.DB)
mysqls[subNamespace].SetTableName(tName)
for _, title := range self.MustGetRule(datacell["RuleName"].(string)).ItemFields {
mysqls[subNamespace].AddColumn(title + ` MEDIUMTEXT`)
}
mysqls[subNamespace].
AddColumn(`Url VARCHAR(255)`, `ParentUrl VARCHAR(255)`, `DownloadTime VARCHAR(50)`).
Create()
}
for _, title := range self.MustGetRule(datacell["RuleName"].(string)).ItemFields {
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
mysqls[subNamespace].AddRow(v)
} else {
mysqls[subNamespace].AddRow(util.JsonString(vd[title]))
}
}
err := mysqls[subNamespace].
AddRow(datacell["Url"].(string), datacell["ParentUrl"].(string), datacell["DownloadTime"].(string)).
Update()
util.CheckErr(err)
}
}
}
示例3: init
func init() {
Output["mysql"] = func(self *Collector, dataIndex int) {
db := mysql.MysqlPool.GetOne().(*mysql.MysqlFish)
defer mysql.MysqlPool.Free(db)
var newMysql = new(mysql.MyTable)
for Name, Rule := range self.GetRules() {
//跳過不輸出的數據
if len(Rule.GetOutFeild()) == 0 {
continue
}
newMysql.SetTableName("`" + tabName(self, Name) + "`")
for _, title := range Rule.GetOutFeild() {
newMysql.AddColumn(title)
}
newMysql.AddColumn("當前連接", "上級鏈接", "下載時間").
Create(db.DB)
num := 0 //小計
for _, datacell := range self.DockerQueue.Dockers[dataIndex] {
if datacell["RuleName"].(string) == Name {
for _, title := range Rule.GetOutFeild() {
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
newMysql.AddRow(v)
} else {
newMysql.AddRow(util.JsonString(vd[title]))
}
}
newMysql.AddRow(datacell["Url"].(string), datacell["ParentUrl"].(string), datacell["DownloadTime"].(string)).
Update(db.DB)
num++
}
}
newMysql = new(mysql.MyTable)
}
}
}
示例4: init
func init() {
Output["mysql"] = func(self *Collector, dataIndex int) {
db, ok := mysql.MysqlPool.GetOne().(*mysql.MysqlSrc)
if !ok || db == nil {
logs.Log.Error("鏈接Mysql數據庫超時,無法輸出!")
return
}
defer mysql.MysqlPool.Free(db)
var mysqls = make(map[string]*mysql.MyTable)
var namespace = util.FileNameReplace(self.namespace())
for _, datacell := range self.DockerQueue.Dockers[dataIndex] {
subNamespace := util.FileNameReplace(self.subNamespace(datacell))
if _, ok := mysqls[subNamespace]; !ok {
mysqls[subNamespace] = mysql.New(db.DB)
mysqls[subNamespace].SetTableName("`" + namespace + "__" + subNamespace + "`")
for _, title := range self.GetRule(datacell["RuleName"].(string)).GetOutFeild() {
mysqls[subNamespace].AddColumn(title)
}
mysqls[subNamespace].
AddColumn("Url", "ParentUrl", "DownloadTime").
Create()
}
for _, title := range self.GetRule(datacell["RuleName"].(string)).GetOutFeild() {
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
mysqls[subNamespace].AddRow(v)
} else {
mysqls[subNamespace].AddRow(util.JsonString(vd[title]))
}
}
mysqls[subNamespace].
AddRow(datacell["Url"].(string), datacell["ParentUrl"].(string), datacell["DownloadTime"].(string)).
Update()
}
}
}
示例5: init
/************************ excel 輸出 ***************************/
func init() {
Output["excel"] = func(self *Collector, dataIndex int) {
defer func() {
if err := recover(); err != nil {
Log.Println(err)
}
}()
var file *xlsx.File
var sheet *xlsx.Sheet
var row *xlsx.Row
var cell *xlsx.Cell
var err error
folder1 := "result/data"
folder2 := folder1 + "/" + self.startTime.Format("2006年01月02日 15時04分05秒")
filename := folder2 + "/" + util.FileNameReplace(self.Spider.GetName()+"_"+self.Spider.GetKeyword()+" "+fmt.Sprintf("%v", self.sum[0])+"-"+fmt.Sprintf("%v", self.sum[1])) + ".xlsx"
// 創建文件
file = xlsx.NewFile()
// 添加分類數據工作表
for Name, Rule := range self.GetRules() {
// 跳過不輸出的數據
if len(Rule.GetOutFeild()) == 0 {
continue
}
// 添加工作表
sheet = file.AddSheet(util.ExcelSheetNameReplace(Name))
// 寫入表頭
row = sheet.AddRow()
for _, title := range Rule.GetOutFeild() {
cell = row.AddCell()
cell.Value = title
}
cell = row.AddCell()
cell.Value = "當前鏈接"
cell = row.AddCell()
cell.Value = "上級鏈接"
cell = row.AddCell()
cell.Value = "下載時間"
num := 0 //小計
for _, datacell := range self.DockerQueue.Dockers[dataIndex] {
if datacell["RuleName"].(string) == Name {
row = sheet.AddRow()
for _, title := range Rule.GetOutFeild() {
cell = row.AddCell()
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
cell.Value = v
} else {
cell.Value = util.JsonString(vd[title])
}
}
cell = row.AddCell()
cell.Value = datacell["Url"].(string)
cell = row.AddCell()
cell.Value = datacell["ParentUrl"].(string)
cell = row.AddCell()
cell.Value = datacell["DownloadTime"].(string)
num++
}
}
// Log.Printf("[任務:%v | 關鍵詞:%v | 小類:%v] 輸出 %v 條數據!!!\n", self.Spider.GetName(), self.Spider.GetKeyword(), Name, num)
}
// 創建/打開目錄
f2, err := os.Stat(folder2)
if err != nil || !f2.IsDir() {
if err := os.MkdirAll(folder2, 0777); err != nil {
Log.Printf("Error: %v\n", err)
}
}
// 保存文件
err = file.Save(filename)
if err != nil {
Log.Println(err)
}
}
}
示例6: init
func init() {
var (
mysqlTable = map[string]*mysql.MyTable{}
mysqlTableLock sync.RWMutex
)
var getMysqlTable = func(name string) (*mysql.MyTable, bool) {
mysqlTableLock.RLock()
defer mysqlTableLock.RUnlock()
tab, ok := mysqlTable[name]
if ok {
return tab.Clone(), true
}
return nil, false
}
var setMysqlTable = func(name string, tab *mysql.MyTable) {
mysqlTableLock.Lock()
mysqlTable[name] = tab
mysqlTableLock.Unlock()
}
DataOutput["mysql"] = func(self *Collector) error {
_, err := mysql.DB()
if err != nil {
return fmt.Errorf("Mysql數據庫鏈接失敗: %v", err)
}
var (
mysqls = make(map[string]*mysql.MyTable)
namespace = util.FileNameReplace(self.namespace())
)
for _, datacell := range self.dataDocker {
subNamespace := util.FileNameReplace(self.subNamespace(datacell))
tName := joinNamespaces(namespace, subNamespace)
table, ok := mysqls[tName]
if !ok {
table, ok = getMysqlTable(tName)
if ok {
mysqls[tName] = table
} else {
table = mysql.New()
table.SetTableName(tName)
for _, title := range self.MustGetRule(datacell["RuleName"].(string)).ItemFields {
table.AddColumn(title + ` MEDIUMTEXT`)
}
if self.Spider.OutDefaultField() {
table.AddColumn(`Url VARCHAR(255)`, `ParentUrl VARCHAR(255)`, `DownloadTime VARCHAR(50)`)
}
if err := table.Create(); err != nil {
logs.Log.Error("%v", err)
continue
} else {
setMysqlTable(tName, table)
mysqls[tName] = table
}
}
}
data := []string{}
for _, title := range self.MustGetRule(datacell["RuleName"].(string)).ItemFields {
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
data = append(data, v)
} else {
data = append(data, util.JsonString(vd[title]))
}
}
if self.Spider.OutDefaultField() {
data = append(data, datacell["Url"].(string), datacell["ParentUrl"].(string), datacell["DownloadTime"].(string))
}
table.AutoInsert(data)
}
for _, tab := range mysqls {
util.CheckErr(tab.FlushInsert())
}
mysqls = nil
return nil
}
}
示例7: init
/************************ CSV 輸出 ***************************/
func init() {
Output["csv"] = func(self *Collector, dataIndex int) {
defer func() {
if err := recover(); err != nil {
logs.Log.Error("%v", err)
}
}()
var namespace = util.FileNameReplace(self.namespace())
var sheets = make(map[string]*csv.Writer)
for _, datacell := range self.DockerQueue.Dockers[dataIndex] {
var subNamespace = util.FileNameReplace(self.subNamespace(datacell))
if _, ok := sheets[subNamespace]; !ok {
folder := config.COMM_PATH.TEXT + "/" + cache.StartTime.Format("2006年01月02日 15時04分05秒") + "/" + namespace + "__" + subNamespace
filename := fmt.Sprintf("%v/%v-%v.csv", folder, self.sum[0], self.sum[1])
// 創建/打開目錄
f, err := os.Stat(folder)
if err != nil || !f.IsDir() {
if err := os.MkdirAll(folder, 0777); err != nil {
logs.Log.Error("Error: %v\n", err)
}
}
// 按數據分類創建文件
file, err := os.Create(filename)
if err != nil {
logs.Log.Error("%v", err)
continue
}
file.WriteString("\xEF\xBB\xBF") // 寫入UTF-8 BOM
sheets[subNamespace] = csv.NewWriter(file)
th := self.MustGetRule(datacell["RuleName"].(string)).ItemFields
th = append(th, "當前鏈接", "上級鏈接", "下載時間")
sheets[subNamespace].Write(th)
defer func(file *os.File) {
// 發送緩存數據流
sheets[subNamespace].Flush()
// 關閉文件
file.Close()
}(file)
}
row := []string{}
for _, title := range self.MustGetRule(datacell["RuleName"].(string)).ItemFields {
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
row = append(row, v)
} else {
row = append(row, util.JsonString(vd[title]))
}
}
row = append(row, datacell["Url"].(string))
row = append(row, datacell["ParentUrl"].(string))
row = append(row, datacell["DownloadTime"].(string))
sheets[subNamespace].Write(row)
}
}
}
示例8: Serialization
// 請求的序列化
func (self *Request) Serialization() string {
return util.JsonString(self)
}
示例9: init
/************************ CSV 輸出 ***************************/
func init() {
Output["csv"] = func(self *Collector, dataIndex int) {
defer func() {
if err := recover(); err != nil {
Log.Println(err)
}
}()
folder1 := "result/data"
folder2 := folder1 + "/" + self.startTime.Format("2006年01月02日 15時04分05秒")
filenameBase := folder2 + "/" + util.FileNameReplace(self.Spider.GetName()+"_"+self.Spider.GetKeyword()+" "+fmt.Sprintf("%v", self.sum[0])+"-"+fmt.Sprintf("%v", self.sum[1]))
// 創建/打開目錄
f2, err := os.Stat(folder2)
if err != nil || !f2.IsDir() {
if err := os.MkdirAll(folder2, 0777); err != nil {
Log.Printf("Error: %v\n", err)
}
}
// 按數據分類創建文件
for Name, Rule := range self.GetRules() {
// 跳過不輸出的數據
if len(Rule.GetOutFeild()) == 0 {
continue
}
file, err := os.Create(filenameBase + " (" + util.FileNameReplace(Name) + ").csv")
if err != nil {
Log.Println(err)
continue
}
file.WriteString("\xEF\xBB\xBF") // 寫入UTF-8 BOM
w := csv.NewWriter(file)
th := Rule.GetOutFeild()
th = append(th, []string{"當前鏈接", "上級鏈接", "下載時間"}...)
w.Write(th)
num := 0 //小計
for _, datacell := range self.DockerQueue.Dockers[dataIndex] {
if datacell["RuleName"].(string) == Name {
row := []string{}
for _, title := range Rule.GetOutFeild() {
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
row = append(row, v)
} else {
row = append(row, util.JsonString(vd[title]))
}
}
row = append(row, datacell["Url"].(string))
row = append(row, datacell["ParentUrl"].(string))
row = append(row, datacell["DownloadTime"].(string))
w.Write(row)
num++
}
}
// 發送緩存數據流
w.Flush()
// 關閉文件
file.Close()
// 輸出報告
// Log.Printf("[任務:%v | 關鍵詞:%v | 小類:%v] 輸出 %v 條數據!!!\n", self.Spider.GetName(), self.Spider.GetKeyword(), Name, num)
}
}
}
示例10: init
func init() {
defer func() {
// 獲取輸出方式列表
for out, _ := range Output {
OutputLib = append(OutputLib, out)
}
util.StringsSort(OutputLib)
}()
/************************ excel 輸出 ***************************/
Output["excel"] = func(self *Collector, dataIndex int) {
defer func() {
if err := recover(); err != nil {
Log.Println(err)
}
}()
var file *xlsx.File
var sheet *xlsx.Sheet
var row *xlsx.Row
var cell *xlsx.Cell
var err error
folder1 := "result/data"
folder2 := folder1 + "/" + self.startTime.Format("2006年01月02日 15時04分05秒")
filename := folder2 + "/" + util.FileNameReplace(self.Spider.GetName()+"_"+self.Spider.GetKeyword()+" "+fmt.Sprintf("%v", self.sum[0])+"-"+fmt.Sprintf("%v", self.sum[1])) + ".xlsx"
// 創建文件
file = xlsx.NewFile()
// 添加分類數據工作表
for Name, Rule := range self.GetRules() {
// 跳過不輸出的數據
if len(Rule.GetOutFeild()) == 0 {
continue
}
// 添加工作表
sheet = file.AddSheet(util.ExcelSheetNameReplace(Name))
// 寫入表頭
row = sheet.AddRow()
for _, title := range Rule.GetOutFeild() {
cell = row.AddCell()
cell.Value = title
}
cell = row.AddCell()
cell.Value = "當前鏈接"
cell = row.AddCell()
cell.Value = "上級鏈接"
cell = row.AddCell()
cell.Value = "下載時間"
num := 0 //小計
for _, datacell := range self.DockerQueue.Dockers[dataIndex] {
if datacell["RuleName"].(string) == Name {
row = sheet.AddRow()
for _, title := range Rule.GetOutFeild() {
cell = row.AddCell()
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
cell.Value = v
} else {
cell.Value = util.JsonString(vd[title])
}
}
cell = row.AddCell()
cell.Value = datacell["Url"].(string)
cell = row.AddCell()
cell.Value = datacell["ParentUrl"].(string)
cell = row.AddCell()
cell.Value = datacell["DownloadTime"].(string)
num++
}
}
// Log.Printf("[任務:%v | 關鍵詞:%v | 小類:%v] 輸出 %v 條數據!!!\n", self.Spider.GetName(), self.Spider.GetKeyword(), Name, num)
}
// 創建/打開目錄
f2, err := os.Stat(folder2)
if err != nil || !f2.IsDir() {
if err := os.MkdirAll(folder2, 0777); err != nil {
Log.Printf("Error: %v\n", err)
}
}
// 保存文件
err = file.Save(filename)
if err != nil {
Log.Println(err)
}
}
/************************ CSV 輸出 ***************************/
Output["csv"] = func(self *Collector, dataIndex int) {
defer func() {
if err := recover(); err != nil {
Log.Println(err)
//.........這裏部分代碼省略.........
示例11: init
/************************ excel 輸出 ***************************/
func init() {
Output["excel"] = func(self *Collector, dataIndex int) (err error) {
defer func() {
if p := recover(); p != nil {
err = fmt.Errorf("%v", p)
}
}()
var (
file *xlsx.File
row *xlsx.Row
cell *xlsx.Cell
sheets = make(map[string]*xlsx.Sheet)
)
// 創建文件
file = xlsx.NewFile()
// 添加分類數據工作表
for _, datacell := range self.DockerQueue.Dockers[dataIndex] {
var subNamespace = util.FileNameReplace(self.subNamespace(datacell))
if _, ok := sheets[subNamespace]; !ok {
// 添加工作表
sheet, err := file.AddSheet(subNamespace)
if err != nil {
logs.Log.Error("%v", err)
continue
}
sheets[subNamespace] = sheet
// 寫入表頭
row = sheets[subNamespace].AddRow()
for _, title := range self.MustGetRule(datacell["RuleName"].(string)).ItemFields {
row.AddCell().Value = title
}
if self.Spider.OutDefaultField() {
row.AddCell().Value = "當前鏈接"
row.AddCell().Value = "上級鏈接"
row.AddCell().Value = "下載時間"
}
}
row = sheets[subNamespace].AddRow()
for _, title := range self.MustGetRule(datacell["RuleName"].(string)).ItemFields {
cell = row.AddCell()
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
cell.Value = v
} else {
cell.Value = util.JsonString(vd[title])
}
}
if self.Spider.OutDefaultField() {
row.AddCell().Value = datacell["Url"].(string)
row.AddCell().Value = datacell["ParentUrl"].(string)
row.AddCell().Value = datacell["DownloadTime"].(string)
}
}
folder := config.TEXT_DIR + "/" + cache.StartTime.Format("2006年01月02日 15時04分05秒")
filename := fmt.Sprintf("%v/%v__%v-%v.xlsx", folder, util.FileNameReplace(self.namespace()), self.sum[0], self.sum[1])
// 創建/打開目錄
f2, err := os.Stat(folder)
if err != nil || !f2.IsDir() {
if err := os.MkdirAll(folder, 0777); err != nil {
logs.Log.Error("Error: %v\n", err)
}
}
// 保存文件
err = file.Save(filename)
return
}
}
示例12: init
/************************ Kafka 輸出 ***************************/
func init() {
var (
kafkaSenders = map[string]*kafka.KafkaSender{}
kafkaSenderLock sync.RWMutex
)
var getKafkaSender = func(name string) (*kafka.KafkaSender, bool) {
kafkaSenderLock.RLock()
tab, ok := kafkaSenders[name]
kafkaSenderLock.RUnlock()
return tab, ok
}
var setKafkaSender = func(name string, tab *kafka.KafkaSender) {
kafkaSenderLock.Lock()
kafkaSenders[name] = tab
kafkaSenderLock.Unlock()
}
DataOutput["kafka"] = func(self *Collector) error {
_, err := kafka.GetProducer()
if err != nil {
return fmt.Errorf("kafka producer失敗: %v", err)
}
var (
kafkas = make(map[string]*kafka.KafkaSender)
namespace = util.FileNameReplace(self.namespace())
)
for _, datacell := range self.dataDocker {
subNamespace := util.FileNameReplace(self.subNamespace(datacell))
topicName := joinNamespaces(namespace, subNamespace)
sender, ok := kafkas[topicName]
if !ok {
sender, ok = getKafkaSender(topicName)
if ok {
kafkas[topicName] = sender
} else {
sender = kafka.New()
sender.SetTopic(topicName)
setKafkaSender(topicName, sender)
kafkas[topicName] = sender
}
}
data := make(map[string]interface{})
for _, title := range self.MustGetRule(datacell["RuleName"].(string)).ItemFields {
vd := datacell["Data"].(map[string]interface{})
if v, ok := vd[title].(string); ok || vd[title] == nil {
data[title] = v
} else {
data[title] = util.JsonString(vd[title])
}
}
if self.Spider.OutDefaultField() {
data["url"] = datacell["Url"].(string)
data["parent_url"] = datacell["ParentUrl"].(string)
data["download_time"] = datacell["DownloadTime"].(string)
}
err := sender.Push(data)
util.CheckErr(err)
}
kafkas = nil
return nil
}
}