當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Tuple.SerializeTuple方法代碼示例

本文整理匯總了Golang中worker.Tuple.SerializeTuple方法的典型用法代碼示例。如果您正苦於以下問題:Golang Tuple.SerializeTuple方法的具體用法?Golang Tuple.SerializeTuple怎麽用?Golang Tuple.SerializeTuple使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在worker.Tuple的用法示例。


在下文中一共展示了Tuple.SerializeTuple方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: main

func main() {

	file, err := os.Create("output.txt")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	count := make(map[string]int)

	worker.ReadTupleStream(os.Stdin, func(tuple worker.Tuple, index int) {
		switch index {
		case 0:
			num, _ := strconv.Atoi(tuple.Slice[1])
			count[tuple.Slice[0]] += num
		case 1:
			num, _ := strconv.Atoi(tuple.Slice[1])
			count[tuple.Slice[0]] += num
		case 2:
			num, _ := strconv.Atoi(tuple.Slice[1])
			count[tuple.Slice[0]] -= num
		}
	})

	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	for key, value := range count {
		file.Write([]byte(key + ", " + strconv.Itoa(value) + "\n"))
		outTuple := worker.Tuple{[]string{key, strconv.Itoa(value)}}
		stdout.Write(outTuple.SerializeTuple(0))
		stdout.Write([]byte{'\n'})
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:33,代碼來源:output.go

示例2: main

func main() {

	// read from standard input to get the input tuples
	inputTuples := make([]worker.Tuple, 0)
	worker.ReadTupleStream(os.Stdin, func(tuple worker.Tuple, index int) {
		inputTuples = append(inputTuples, tuple)
	})

	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	counts := make(map[string]int)
	for _, tuple := range inputTuples {
		word := tuple.Slice[0]

		if count, ok := counts[word]; ok {
			counts[word] = count + 1
		} else {
			counts[word] = 1
		}
	}

	for word, count := range counts {
		outTuple := worker.Tuple{[]string{word, strconv.Itoa(count)}}
		stdout.Write(outTuple.SerializeTuple(0))
		stdout.Write([]byte{'\n'})
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:27,代碼來源:wordcount_reduce.go

示例3: main

// go run input.go start_time duration number_of_log random_seed
// go run input.go now 60 2000 5
func main() {
	d, _ := strconv.Atoi(os.Args[2])
	duration := time.Second * time.Duration(d)

	startTime := time.Now() // os.Args[1]

	num, _ := strconv.Atoi(os.Args[3])

	seed, _ := strconv.ParseInt(os.Args[4], 10, 64)

	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	r := rand.New(rand.NewSource(seed))

	step := duration / time.Duration(num)

	for st := startTime; st.Before(startTime.Add(duration)); st = st.Add(step) {
		s := []string{"[" + st.String() + "]"}

		if r.Float32() < 0.1 {
			s = append(s, "error")
		} else {
			s = append(s, "pass")
		}

		tuple := worker.Tuple{s}
		stdout.Write(tuple.SerializeTuple(0))
		stdout.Write([]byte{'\n'})
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:32,代碼來源:input.go

示例4: main

func main() {

	// read from standard input to get the input tuples
	newTuples := make([]worker.Tuple, 0)
	oldTuples := make([]worker.Tuple, 0)
	totalTuples := make([]worker.Tuple, 0)

	worker.ReadTupleStream(os.Stdin, func(tuple worker.Tuple, index int) {
		switch index {
		case 0:
			newTuples = append(newTuples, tuple)
		case 1:
			oldTuples = append(oldTuples, tuple)
		case 2:
			totalTuples = append(totalTuples, tuple)
		}
	})

	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	counts := make(map[string]int)
	for _, tuple := range totalTuples {
		word := tuple.Slice[0]
		prev, _ := strconv.Atoi(tuple.Slice[1])

		if count, ok := counts[word]; ok {
			counts[word] = count + prev
		} else {
			counts[word] = prev
		}
	}

	for _, tuple := range newTuples {
		word := tuple.Slice[0]

		if count, ok := counts[word]; ok {
			counts[word] = count + 1
		} else {
			counts[word] = 1
		}
	}

	for _, tuple := range oldTuples {
		word := tuple.Slice[0]

		if count, ok := counts[word]; ok {
			counts[word] = count - 1
		}
	}

	for word, count := range counts {
		outTuple := worker.Tuple{[]string{word, strconv.Itoa(count)}}

		stdout.Write(outTuple.SerializeTuple(2))
		stdout.Write([]byte{'\n'})
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:57,代碼來源:combine.go

示例5: main

func main() {
	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	t := time.Now()
	strings := []string{"Zack", "Jeff", "Vedha", "Vikas", "Zack"}

	for i := 0; i < 1000; i++ {
		for _, str := range strings {
			tuple := worker.Tuple{[]string{str, t.String()}}
			stdout.Write(tuple.SerializeTuple(0))
			stdout.Write([]byte{'\n'})
		}
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:14,代碼來源:mock_log_reader.go

示例6: main

func main() {

	count := make(map[string]int)

	worker.ReadTupleStream(os.Stdin, func(tuple worker.Tuple, index int) {
		count[tuple.Slice[0]] += 1
	})

	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	for key, value := range count {
		outTuple := worker.Tuple{[]string{key, strconv.Itoa(value)}}
		stdout.Write(outTuple.SerializeTuple(0))
		stdout.Write([]byte{'\n'})
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:16,代碼來源:reduce.go

示例7: main

func main() {
	gopath := os.Getenv("GOPATH")
	path := path.Join(gopath, "/src/demo/wordcount_go/input_file.txt")

	file, err := os.Open(path)
	if err != nil {
		log.Panic(err)
	}
	defer file.Close()

	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		tuple := worker.Tuple{[]string{scanner.Text()}}

		stdout.Write(tuple.SerializeTuple(0))
		stdout.Write([]byte{'\n'})
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:20,代碼來源:wordcount_input.go

示例8: main

func main() {

	// read input from standard input and create tuples
	inputTuples := make([]worker.Tuple, 0)
	worker.ReadTupleStream(os.Stdin, func(tuple worker.Tuple, index int) {
		inputTuples = append(inputTuples, tuple)
	})

	// get standard output
	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	// iterate over input tuples
	for _, tuple := range inputTuples {
		words := strings.Fields(tuple.Slice[0])
		// iterate over words
		for _, word := range words {
			// emit each word in a new tuple
			outTuple := worker.Tuple{[]string{strings.ToLower(word), "1"}}
			stdout.Write(outTuple.SerializeTuple(0))
			stdout.Write([]byte{'\n'})
		}
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:23,代碼來源:wordcount_map.go

示例9: main

func main() {

	// read from standard input to get the input tuples
	inputTuples := make([]worker.Tuple, 0)
	worker.ReadTupleStream(os.Stdin, func(tuple worker.Tuple, index int) {
		inputTuples = append(inputTuples, tuple)
	})

	stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout")

	for _, tuple := range inputTuples {
		words := strings.Fields(tuple.Slice[1])

		for _, word := range words {
			word = strings.ToLower(word)
			if word == "error" {
				outTuple := worker.Tuple{[]string{word, "1"}}
				stdout.Write(outTuple.SerializeTuple(0))
				stdout.Write([]byte{'\n'})
			}
		}
	}
}
開發者ID:jtwarren,項目名稱:Hurricane,代碼行數:23,代碼來源:map.go


注:本文中的worker.Tuple.SerializeTuple方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。