Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to insert my custom data #30

Closed
intfish123 opened this issue Mar 31, 2022 · 3 comments
Closed

How to insert my custom data #30

intfish123 opened this issue Mar 31, 2022 · 3 comments

Comments

@intfish123
Copy link

intfish123 commented Mar 31, 2022

I rewrote my code like this, But it still doesn't work, you can try it:

writer, err := mmdbwriter.New(
	mmdbwriter.Options{
		DatabaseType: "GeoLite2-City",
			RecordSize: 24,
			IPVersion: 4,
			IncludeReservedNetworks: true,
			Languages: []string{"zh-CN"},
	},
)
if err != nil {
	return err
}

nameId := 0
for i:=0; i<len(customData); i++ {
	record := mmdbtype.Map{}
	record["continent"] = mmdbtype.Map{
		"geoname_id": mmdbtype.Uint64(nameId),
		"code": mmdbtype.String(""),
		"names":mmdbtype.Map{"zh-CN": mmdbtype.String("")},
	}
	nameId++

	record["country"] = mmdbtype.Map{
		"geoname_id": mmdbtype.Uint64(nameId),
		"iso_code": mmdbtype.String(""),
		"names":mmdbtype.Map{"zh-CN": mmdbtype.String(customData[i].Country)},
	}
	nameId++

	record["subdivisions"] = mmdbtype.Slice{
		mmdbtype.Map{
			"geoname_id": mmdbtype.Uint64(nameId),
			"iso_code": mmdbtype.String(""),
			"names":mmdbtype.Map{"zh-CN": mmdbtype.String(customData[i].Province)},
	  },
	}
	nameId++

	record["city"] = mmdbtype.Map{
		"geoname_id": mmdbtype.Uint64(nameId),
		"names":mmdbtype.Map{"zh-CN": mmdbtype.String(customData[i].City)},
	}
	nameId++

	record["county"] = mmdbtype.Map{
		"geoname_id": mmdbtype.Uint64(nameId),
		"names":mmdbtype.Map{"zh-CN": mmdbtype.String(customData[i].County)},
	}
	nameId++

	record["location"] = mmdbtype.Map{
		"accuracy_radius": mmdbtype.Uint16(0),
		"latitude": mmdbtype.Float64(0),
		"longitude": mmdbtype.Float64(0),
		"metro_code": mmdbtype.Uint64(0),
		"time_zone": mmdbtype.String(""),
	}
	nameId++

	record["postal"] = mmdbtype.Map{
		"code": mmdbtype.String(""),
	}
	nameId++

	record["registered_country"] = mmdbtype.Map{
		"geoname_id": mmdbtype.Uint64(nameId),
		"is_in_european_union": mmdbtype.Bool(false),
		"iso_code": mmdbtype.String(""),
		"names": mmdbtype.Map{"zh-CN": mmdbtype.String("")},
	}
	nameId++

	record["represented_country"] = mmdbtype.Map{
		"geoname_id": mmdbtype.Uint64(nameId),
		"is_in_european_union": mmdbtype.Bool(false),
		"iso_code": mmdbtype.String(""),
		"type": mmdbtype.String(""),
		"names": mmdbtype.Map{"zh-CN": mmdbtype.String("")},
	}
	nameId++

	tmpIp := net.ParseIP(customData[i].ipVal)
	writer.Insert(&net.IPNet{IP: tmpIp, Mask: tmpIp.DefaultMask()}, record)
}
fh, err := os.Create("out/out.mmdb")
if err != nil {
	log.Fatal(err)
}
_, err = dbWriter.WriteTo(fh)
if err != nil {
	log.Fatal(err)
}
@intfish123
Copy link
Author

I read the db use this lib: github.com/oschwald/geoip2-golang v1.7.0

@oschwald
Copy link
Member

Your code is not complete as is and I am not able to run it. At a glance, I don't see anything obviously wrong. I would suggest comparing the structure of records in the database you generated with a MaxMind one using mmdblookup from libmaxminddb. I am closing this as I don't see any evidence there is a problem with mmdbwriter.

@intfish123
Copy link
Author

Your code is not complete as is and I am not able to run it. At a glance, I don't see anything obviously wrong. I would suggest comparing the structure of records in the database you generated with a MaxMind one using mmdblookup from libmaxminddb. I am closing this as I don't see any evidence there is a problem with mmdbwriter.

Yep, you are right, I have found what's the my problem, &net.IPNet{IP: tmpIp, Mask: tmpIp.DefaultMask()} is not correct for *IPNet. Thanks you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants