在HDFS上使用NIO进行mapreduce(mapreduce using NIO on HDFS)
我需要将一些文本放入地图缩小过程的Mappers (全部)的HDFS文件中。
文本/文件在reducers进程中用作查找,因此它不能在常规路径中传播( context.write() )
使用下面的代码段很慢,并且从不同的映射器激活时可能会产生文件锁定问题。
我很想使用ByteBuffer和文件锁(NIO)。 这可能在这个框架中吗? 此外,欢迎任何其他想法。
代码段:
Path fname = ... FileSystem fs = FileSystem.get(context.getconfiguration()); out = fs.create(fname); while (condition) out.write(...); out.flush(); out.close();
感谢您的任何想法/帮助。
拉兹
I'm need to put some text into an HDFS file from (all) the Mappers of a map reduce process.
The text / file is be used as a lookup in the reducers process so it cannot travel in the regular path (context.write())
Using the below snippet is both slow and may produce file lock issues when activated from the different mappers.
I would love to use ByteBuffer and file locks (NIO). is this possible in this framework ? Also, Any other ideas is welcome.
The code snippet:
Path fname = ... FileSystem fs = FileSystem.get(context.getconfiguration()); out = fs.create(fname); while (condition) out.write(...); out.flush(); out.close();
Thanks for any idea / help.
Raz
原文:https://stackoverflow.com/questions/27708753
最满意答案
phone
和org
的内容是完全不同的数据结构,并且您将无法完全反序列化为像您在示例中一样的同质格式。 最好的选择是至少部分反序列化为一个struct
:type data struct { Phone []map[string]string Org []map[string]map[string]string }
这至少会反序列化所有的数据,但它仍然有点混乱; 一片地图的地图并不是一个很好的数据结构。 这个问题并不清楚,但是如果任何一个领域都是固定的,那么您可能也希望将这些领域的类型编码,例如:
type data struct { Phone []map[string]string Org []struct{ Current map[string]string } }
然后您可以反序列化为这种类型并更容易使用它:
var person data json.Unmarshal([]byte(ii), &person) fmt.Printf("%v", person.Phone) fmt.Printf("%v", person.Org[0].Current)
工作场地示例: https : //play.golang.org/p/5W-7RzPimZj
请注意,我必须更正JSON中的错误,因
"org"
之前缺少逗号而无效。The contents of
phone
andorg
are completely different data structures, and you won't be able to cleanly deserialize both into a homogenous format like you've got in the example. The best option is to at least partially deserialize into astruct
:type data struct { Phone []map[string]string Org []map[string]map[string]string }
This will at least deserialize all of the data, but it's still a bit messy; a slice of maps of maps is not a great data structure to work with. It's not clear from the question, but if any of the fields are fixed, you might want to codify those in types as well, for example:
type data struct { Phone []map[string]string Org []struct{ Current map[string]string } }
You can then deserialize to this type and use it much more easily:
var person data json.Unmarshal([]byte(ii), &person) fmt.Printf("%v", person.Phone) fmt.Printf("%v", person.Org[0].Current)
Working playground example here: https://play.golang.org/p/5W-7RzPimZj
Note that I had to correct an error in the JSON, it is invalid due to a missing comma before
"org"
.
相关问答
更多-
如何解析Golang中嵌套的JSON对象的内部字段?(How do I parse an inner field in a nested JSON object in Golang?)[2021-06-20]
不幸的是,与encoding/xml不同, json包不提供访问嵌套值的方法。 你需要创建一个单独的Parents结构或者将类型赋值为map[string]string 。 例如: type Person struct { Name string Parents map[string]string } 您可以为母亲提供一个吸气剂,如下所示: func (p *Person) Mother() string { return p.Parents["mother"] } 这可能会或可 ... -
omitempty标记选项不适omitempty ,因为它是一个struct 。 结构有一个“零”值,但这是一个结构值,其中所有字段都有其零值。 这是一个“有效”值,因此不会被视为“空”。 但通过简单地将它改为一个指针: *time.Time ,它将起作用(对于json封送/解组处理, nil指针被视为“空”)。 所以在这种情况下无需编写自定义的Marshaler : type MyStruct struct { Timestamp *time.Time `json:",omitempty"` ...
-
通过漂亮的印刷,我认为你的意思是缩进,就像这样 { "data": 1234 } 而不是 {"data":1234} 最简单的方法是使用MarshalIndent ,这将允许您指定如何通过indent参数indent 。 因此, json.MarshalIndent(data, "", " ")将使用四个空格进行缩进打印。 By pretty-print, I assume you mean indented, like so { "data": 1234 } rather than ...
-
Golang JSON Unmarshal到字段,但不是编组到JSON响应中(Golang JSON Unmarshal to field, but NOT marshal into JSON response)[2023-08-28]
你在omitempty的正确轨道上,你只需将SecretField设置为""使其生效 package main import ( "fmt" "encoding/json" ) type MyStruct struct { GoodField string `json:"goodField"` SecretField string `json:"secret,omitempty"` } func main() { data := MyStruct{} ... -
您需要导出字段(使名称大写): type Metadata struct { Model string } type Texture struct { Url string Hash string Metadata *Metadata } 更新了游乐场示例 https://play.golang.org/p/d-d4SJbCpH You need to export your fields (make the names capitalized): ty ...
-
phone和org的内容是完全不同的数据结构,并且您将无法完全反序列化为像您在示例中一样的同质格式。 最好的选择是至少部分反序列化为一个struct : type data struct { Phone []map[string]string Org []map[string]map[string]string } 这至少会反序列化所有的数据,但它仍然有点混乱; 一片地图的地图并不是一个很好的数据结构。 这个问题并不清楚,但是如果任何一个领域都是固定的,那么您可能也希望将这些领域的类型编码 ...
-
添加声明 type Authenticator struct { Name string `json:"name"` Phone int64 `json:"phone"` } 并改变 Authenticators []struct { Name string `json:"name"` Phone int64 `json:"phone"` } `json:"authenticators"` 至 Authenticators []Authentica ...
-
Golang打印地图内部由json.NewDecoder生成的地图(Golang print map inside map generated by json.NewDecoder)[2023-08-26]
您需要将map[string]interface{}为更有用的东西。 在这种情况下, map[string]interface{}再次出现,因为city本身就是一个地图。 您无法将其转换为其他任何内容,因为coord也是地图,因此无法将city转换为map[string]string 。 https://play.golang.org/p/bGsYLnSAK4 You need to cast the interface{} of your map[string]interface{} to somethi ... -
gopkg.in/mgo.v2/bson软件包有一个名为UnmarshalJSON的功能,它可以完全按照您的需要进行操作。 data参数应该将JSON字符串保存为[]byte值。 func UnmarshalJSON(data []byte, value interface{}) error UnmarshalJSON解组一个JSON值,该值可能包含BSON扩展JSON规范中定义的非标准语法。 例: var bdoc interface{} err = bson.UnmarshalJSON([]byte ...
-
在这种情况下,您不是解组单个JSON对象,而是解释JSON对象的数组,这就是解析API响应时遇到问题的原因。 你看到这个错误告诉你f的实际类型。 另一个例子是有效的,因为它是一个单独的JSON对象,可以映射为map[string]interface{}看看这个: var f []interface{} err := json.Unmarshal(str, &f) if err != nil { fmt.Println(err) return } for _, v := range f { ...