Go言語取得Redisデータをprotobuf形式に変換Json形式でファイルとして格納
3268 ワード
私の最初のgoプログラム package main
import (
"fmt"
"github.com/go-redis/redis"
"github.com/golang/protobuf/proto"
"github.com/user/appcpi/game/protobuf/game_data"
"os"
"log"
//"unsafe"
"encoding/json"
)
func checkFile(filename string) {
_, err := os.Stat(filename)
if err == nil {
os.Remove(filename)
}
}
func createFileAndWrite(filename string, data []byte) {
checkFile(filename)
file, err := os.Create(filename)
if err != nil {
log.Fatal("create %s err:%s
", filename, err)
}
file.Write(data)
/*if err != nil {
log.Fatal("write len(data)=%d err:%s
", data, err)
}*/
file.Close()
}
func parseGameProto(data string) []byte{
game_data := &APPCPI_GAME.GameData{}
err := proto.Unmarshal([]byte(data), game_data)
if err != nil {
log.Fatal("GameData Unmarshal err: %s
", err)
}
result, err := json.MarshalIndent(game_data, "", " ")
if err != nil {
log.Fatal("GameData arshalIndent err: %s
", err)
}
return result
}
func parseTicketProto(data string) []byte{
ticket := &APPCPI_GAME.TicketInfo{}
err := proto.Unmarshal([]byte(data), ticket)
if err != nil {
log.Fatal("ticket Unmarshal err: %s
", err)
}
result, err := json.MarshalIndent(ticket, "", " ")
if err != nil {
log.Fatal("ticket arshalIndent err: %s
", err)
}
return result
}
func getRecord(client *redis.Client, id string) {
data, err := client.HGetAll(id).Result()
if err != nil {
log.Fatal("HGetAll(%s) err: %s
", id, err)
}
for k,v := range(data) {
fmt.Printf("k:%s len(v):%d
", k, len(v))
if k == "game" {
createFileAndWrite(id + "_game.txt", parseGameProto(v))
} else if k == "ticket" {
createFileAndWrite(id + "_ticket.txt", parseTicketProto(v))
}
}
}
func main() {
argv := os.Args[1:]
if len(argv) < 1 {
fmt.Printf("Usage:%s [user_id] {IP} {PORT}
", os.Args[0])
os.Exit(0)
}
var Addr string
if len(argv) >= 3 {
Addr += argv[1]
Addr += ":"
Addr += argv[2]
} else {
Addr = "127.0.0.1:7003"
}
client := redis.NewClient(&redis.Options{
Addr: Addr,
Password: "", // no password set
DB: 0, // use default DB
})
//fmt.Printf("type:%s
", unsafe.Sizeof(client))
_, err := client.Ping().Result()
if err != nil {
log.Fatal("redis ping err:%s
", err)
}
//fmt.Println(pong, err)
getRecord(client, argv[0])
}
説明する
package main
import (
"fmt"
"github.com/go-redis/redis"
"github.com/golang/protobuf/proto"
"github.com/user/appcpi/game/protobuf/game_data"
"os"
"log"
//"unsafe"
"encoding/json"
)
func checkFile(filename string) {
_, err := os.Stat(filename)
if err == nil {
os.Remove(filename)
}
}
func createFileAndWrite(filename string, data []byte) {
checkFile(filename)
file, err := os.Create(filename)
if err != nil {
log.Fatal("create %s err:%s
", filename, err)
}
file.Write(data)
/*if err != nil {
log.Fatal("write len(data)=%d err:%s
", data, err)
}*/
file.Close()
}
func parseGameProto(data string) []byte{
game_data := &APPCPI_GAME.GameData{}
err := proto.Unmarshal([]byte(data), game_data)
if err != nil {
log.Fatal("GameData Unmarshal err: %s
", err)
}
result, err := json.MarshalIndent(game_data, "", " ")
if err != nil {
log.Fatal("GameData arshalIndent err: %s
", err)
}
return result
}
func parseTicketProto(data string) []byte{
ticket := &APPCPI_GAME.TicketInfo{}
err := proto.Unmarshal([]byte(data), ticket)
if err != nil {
log.Fatal("ticket Unmarshal err: %s
", err)
}
result, err := json.MarshalIndent(ticket, "", " ")
if err != nil {
log.Fatal("ticket arshalIndent err: %s
", err)
}
return result
}
func getRecord(client *redis.Client, id string) {
data, err := client.HGetAll(id).Result()
if err != nil {
log.Fatal("HGetAll(%s) err: %s
", id, err)
}
for k,v := range(data) {
fmt.Printf("k:%s len(v):%d
", k, len(v))
if k == "game" {
createFileAndWrite(id + "_game.txt", parseGameProto(v))
} else if k == "ticket" {
createFileAndWrite(id + "_ticket.txt", parseTicketProto(v))
}
}
}
func main() {
argv := os.Args[1:]
if len(argv) < 1 {
fmt.Printf("Usage:%s [user_id] {IP} {PORT}
", os.Args[0])
os.Exit(0)
}
var Addr string
if len(argv) >= 3 {
Addr += argv[1]
Addr += ":"
Addr += argv[2]
} else {
Addr = "127.0.0.1:7003"
}
client := redis.NewClient(&redis.Options{
Addr: Addr,
Password: "", // no password set
DB: 0, // use default DB
})
//fmt.Printf("type:%s
", unsafe.Sizeof(client))
_, err := client.Ping().Result()
if err != nil {
log.Fatal("redis ping err:%s
", err)
}
//fmt.Println(pong, err)
getRecord(client, argv[0])
}