HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Parsing data coming from a URL

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
parsingfromdatacomingurl

Problem

I need to parse the data coming from a URL:

haschanged=true
version=1
timestamp=1389562122310
DATACENTER=/pr/hello/plc
    TotalNumberOfServers:4
    primary:{0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2}
    secondary:{0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0}
    hosttomachine:{3=plcdbx1115.plc.domain.com, 2=plcdbx1114.plc.domain.com, 1=plcdbx1113.plc.domain.com, 4=plcdbx1116.plc.domain.com}
DATACENTER=/pr/hello/pty
    TotalNumberOfServers:2
    primary:{0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2, 14=1}
    secondary:{0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0, 14=0}
    hosttomachine:{1=ptydbx1145.pty.domain.com, 4=ptydbx1148.pty.domain.com}
DATACENTER=/pr/hello/vgs
    TotalNumberOfServers:0
    primary:{}
    secondary:{}
    hosttomachine:{}


After parsing the data I need to store all datacenter data in a Map like this:

ConcurrentHashMap> primaryData


For example, the Key of primaryData is /pr/hello/plc and value is:

{0=1, 1=2, 2=1, 3=2, 4=1, 5=2, 6=1, 7=2, 8=1, 9=2, 10=1, 11=2, 12=1, 13=2}


which is for primary.

Similarly another Map for secondary for each datacenter:

ConcurrentHashMap> secondaryData


For example, the Key of secondaryData is /pr/hello/plc and value is:

{0=0, 1=0, 2=0, 3=1, 4=0, 5=0, 6=0, 7=1, 8=0, 9=0, 10=0, 11=1, 12=0, 13=0}


which is for secondary.

And lastly, one more map for hosttomachine mapping for each datacenter:

ConcurrentHashMap> hostMachineMapping -


For example, the key of hostMachineMapping is /pr/hello/plc and value is:

{3=plcdbx1115.plc.domain.com, 2=plcdbx1114.plc.domain.com, 1=plcdbx1113.plc.domain.com, 4=plcdbx1116.plc.domain.com}


which is for hosttomachine.

And all the above map will have data for its datacenter as I have three datacenter in the above example. So each each map will have three data. And also I will parse the

Solution

Regex to the rescue

-
Regex for key: (?

-
Regex for primary values:
(?

-
Regex for secondary values: (?

-
Regex for hostMachine Mapping:
(?

-
Split on "," for primary, secondary, and hostMachine

Use these as your patterns then go through each match. Each iteration should give you key with matching values.

Context

StackExchange Code Review Q#39231, answer score: 2

Revisions (0)

No revisions yet.