Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Hadoop learning-serialization and deserialization of person class objects-day07

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The code for the person class:

Import java.io.DataInput

Import java.io.DataOutput

Import java.io.IOException

Import org.apache.hadoop.io.BooleanWritable

Import org.apache.hadoop.io.IntWritable

Import org.apache.hadoop.io.Text

Import org.apache.hadoop.io.Writable

Public class Person implements Writable {

/ / equivalent to String type

Private Text name

Public Text getName () {

Return name

}

Public void setName (Text name) {

This.name = name

}

Public IntWritable getAge () {

Return age

}

Public void setAge (IntWritable age) {

This.age = age

}

Public BooleanWritable getMale () {

Return male

}

Public void setMale (BooleanWritable male) {

This.male = male

}

Private IntWritable age

Private BooleanWritable male

/ / Serialization

Public void write (DataOutput out) throws IOException {

Name.write (out)

Age.write (out)

Male.write (out)

}

/ / deserialization

Public void readFields (DataInput in) throws IOException {

Name = new Text ()

Age=new IntWritable ()

Male=new BooleanWritable ()

Name.readFields (in)

Age.readFields (in)

Male.readFields (in)

}

}

The code for the test class:

Import java.io.ByteArrayInputStream

Import java.io.ByteArrayOutputStream

Import java.io.DataInputStream

Import java.io.DataOutputStream

Import org.apache.hadoop.io.BooleanWritable

Import org.apache.hadoop.io.IntWritable

Import org.apache.hadoop.io.Text

Import org.junit.Test

Public class TestPerson {

@ Test

Public void testSeria () throws Exception {

/ / new person

Person p = new Person ()

P.setName (new Text ("tomas"))

P.setAge (new IntWritable (12))

P.setMale (new BooleanWritable (false))

/ / Serialization

ByteArrayOutputStream baos = new ByteArrayOutputStream ()

DataOutputStream dataOut = new DataOutputStream (baos)

P.write (dataOut)

DataOut.close ()

/ / deserialization

Person newPerson = newPerson ()

NewPerson.readFields (new DataInputStream (new ByteArrayInputStream (baos.toByteArray ()

System.out.println (newPerson.getName ())

System.out.println (newPerson.getAge () .get ())

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report