How data is constructed in files created by teragen in Hadoop? - hadoop

I'm trying to understand the details of terasort. After using the Hadoop's teragen, I can successfully create the input data files.
Many online documents say that each row contains data in the following format:
<10 bytes key><10 bytes rowid><78 bytes filler>\r\n
Therefore, I downloaded the first part-file part-m-00000, and then use the following command to cut the rowid out of it:
cut -nb 11-20 part-m-00000 | od -c -w10 -Ad
However, here is what I got:
0000000 \0 021 0 0 0 0 0 0 0 0
0000010 \n 0 0 0 0 0 0 0 0 0
0000020 0 \n 021 0 0 0 0 0 0 0
0000030 0 0 \n 0 0 0 0 0 0 0
0000040 0 0 0 \n 021 0 0 0 0 0
0000050 0 0 0 0 \n 021 0 0 0 0
0000060 0 0 0 0 0 \n 0 0 0 0
0000070 0 0 0 0 0 0 \n 021 0 0
0000080 0 0 0 0 0 0 0 \n 0 0
0000090 0 0 0 0 0 0 0 0 \n 0
0000100 0 0 0 0 0 0 0 0 0 \n
Apparently, there is nothing like the increased row ids, that I expect in the result.
I wonder what did I do wrong?

I am not sure what online documents you're quoting, but the comment in TeraGen source code clearly states the format of generated records:
/**
* Generate the official GraySort input data set.
* The user specifies the number of rows and the output directory and this
* class runs a map/reduce program to generate the data.
* The format of the data is:
* <ul>
* <li>(10 bytes key) (constant 2 bytes) (32 bytes rowid)
* (constant 4 bytes) (48 bytes filler) (constant 4 bytes)
* <li>The rowid is the right justified row id as a hex number.
* </ul>
*
:
And, teragen produces rows that fully comply with the above format. In hex, they look like this (32-byte long rowids are marked by me with a "<<" :))
[hdfs#mygateway ~]# xxd part-m-00000 | head -40
0000000: 4a69 6d47 7261 7952 4950 0011 3030 3030 JimGrayRIP..0000 << rowid 0
0000010: 3030 3030 3030 3030 3030 3030 3030 3030 0000000000000000
0000020: 3030 3030 3030 3030 3030 3030 8899 aabb 000000000000....
0000030: 3232 3232 3030 3030 3232 3232 3030 3030 2222000022220000
0000040: 3232 3232 3030 3030 3232 3232 3030 3030 2222000022220000
0000050: 3232 3232 3030 3030 3030 3030 3131 3131 2222000000001111
0000060: ccdd eeff 95e0 e482 62b3 edfe 0447 0011 ........b....G..
0000070: 3030 3030 3030 3030 3030 3030 3030 3030 0000000000000000 << rowid 1
0000080: 3030 3030 3030 3030 3030 3030 3030 3031 0000000000000001
0000090: 8899 aabb 3939 3939 3434 3434 3838 3838 ....999944448888
00000a0: 3535 3535 4343 4343 3737 3737 3535 3535 5555CCCC77775555
00000b0: 3535 3535 4242 4242 3636 3636 3434 3434 5555BBBB66664444
00000c0: 3636 3636 ccdd eeff 4597 9353 dbed 5329 6666....E..S..S)
00000d0: 365c 0011 3030 3030 3030 3030 3030 3030 6\..000000000000 << rowid 2
00000e0: 3030 3030 3030 3030 3030 3030 3030 3030 0000000000000000
00000f0: 3030 3032 8899 aabb 4646 4646 3737 3737 0002....FFFF7777
0000100: 3939 3939 3535 3535 4646 4646 3131 3131 99995555FFFF1111
0000110: 3232 3232 3939 3939 3838 3838 3434 3434 2222999988884444
0000120: 4444 4444 4646 4646 ccdd eeff 882a 02c3 DDDDFFFF.....*..
0000130: 1536 2b60 765f 0011 3030 3030 3030 3030 .6+`v_..00000000 << etc. etc.
0000140: 3030 3030 3030 3030 3030 3030 3030 3030 0000000000000000
0000150: 3030 3030 3030 3033 8899 aabb 3131 3131 00000003....1111
0000160: 3030 3030 3030 3030 3030 3030 3636 3636 0000000000006666
0000170: 3838 3838 4242 4242 3333 3333 3333 3333 8888BBBB33333333
0000180: 4141 4141 3131 3131 4343 4343 ccdd eeff AAAA1111CCCC....
0000190: 5c90 ab38 ae52 8962 15d7 0011 3030 3030 \..8.R.b....0000
00001a0: 3030 3030 3030 3030 3030 3030 3030 3030 0000000000000000
00001b0: 3030 3030 3030 3030 3030 3034 8899 aabb 000000000004....
00001c0: 4141 4141 4242 4242 4242 4242 3232 3232 AAAABBBBBBBB2222
00001d0: 3434 3434 3434 3434 3939 3939 3535 3535 4444444499995555
00001e0: 4242 4242 3131 3131 3838 3838 4444 4444 BBBB11118888DDDD
00001f0: ccdd eeff 72dc 0ca5 1e33 3f32 4b7a 0011 ....r....3?2Kz..
0000200: 3030 3030 3030 3030 3030 3030 3030 3030 0000000000000000
0000210: 3030 3030 3030 3030 3030 3030 3030 3035 0000000000000005
0000220: 8899 aabb 3838 3838 3333 3333 4242 4242 ....88883333BBBB
0000230: 3838 3838 3838 3838 3434 3434 3737 3737 8888888844447777
0000240: 3232 3232 3737 3737 3939 3939 3030 3030 2222777799990000
0000250: 3232 3232 ccdd eeff 1043 1af6 a0d8 47b8 2222.....C....G.
0000260: c55f 0011 3030 3030 3030 3030 3030 3030 ._..000000000000
0000270: 3030 3030 3030 3030 3030 3030 3030 3030 0000000000000000
If you're interested, you can also inspect how the rows are generated (constants used, etc.) in GenSort.java.

Related

How to add path parameter in elasticsearch Java bulk Api

I'm using Java bulk Api,Elasticsearch 6.6.2, and expect to POST bulk request as
POST /<index>/_bulk,
but i got POST /_bulk instead (see packet captured below).
I know these 2 POST methods are the same, but there're a proxy between my app and ES, the path parameter is for the proxy for some reasons(I don't know why).
so, I wonder how to set the path parameter in Java ,so that request can be sent in POST /<index>/_bulk format.
Here's the code
RestHighLevelClient client;
/* some client init code ....*/
...
String INDEX = "api_track";
String DOC_TYPE = "_doc";
BulkProcessor.Builder builder = BulkProcessor.builder(
(request, bulkListener) ->
client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener),
listener);
builder.setBulkActions(500);
builder.setBulkSize(new ByteSizeValue(1L, ByteSizeUnit.MB));
builder.setConcurrentRequests(0);
builder.setFlushInterval(TimeValue.timeValueSeconds(5L));
builder.setBackoffPolicy(BackoffPolicy
.constantBackoff(TimeValue.timeValueSeconds(1L), 3));
builder.setGlobalIndex(INDEX);
builder.setGlobalType(DOC_TYPE);
bulkProcessor = builder.build();
IndexRequest request = new IndexRequest().index(INDEX).type(DOC_TYPE).source(maps);
bulkProcessor.add(request);
and the packet captured:
0x0000: 0200 0000 4500 0277 0000 4000 4006 12c9 ....E..w..#.#...
0x0010: 1e27 dee3 0ba3 1d0b e142 23f0 85d1 ad67 .'.......B#....g
0x0020: 1540 31d3 8018 0801 3eb0 0000 0101 080a .#1.....>.......
0x0030: 1500 c5e6 b6ba 7ff0 504f 5354 202f 5f62 ........POST./_b <-- POST format here
0x0040: 756c 6b3f 7469 6d65 6f75 743d 316d 2048 ulk?timeout=1m.H
0x0050: 5454 502f 312e 310d 0a43 6f6e 7465 6e74 TTP/1.1..Content
0x0060: 2d4c 656e 6774 683a 2033 3335 0d0a 436f -Length:.335..Co
0x0070: 6e74 656e 742d 5479 7065 3a20 6170 706c ntent-Type:.appl
0x0080: 6963 6174 696f 6e2f 6a73 6f6e 0d0a 486f ication/json..Ho
0x0090: 7374 3a20 7365 6172 6368 2e61 6c69 7061 st:.search.alipa
0x00a0: 792e 6e65 743a 3932 3030 0d0a 436f 6e6e y.net:9200..Conn
0x00b0: 6563 7469 6f6e 3a20 4b65 6570 2d41 6c69 ection:.Keep-Ali
0x00c0: 7665 0d0a 5573 6572 2d41 6765 6e74 3a20 ve..User-Agent:.
0x00d0: 4170 6163 6865 2d48 7474 7041 7379 6e63 Apache-HttpAsync
0x00e0: 436c 6965 6e74 2f34 2e31 2e34 2028 4a61 Client/4.1.4.(Ja
0x00f0: 7661 2f31 2e38 2e30 5f31 3531 290d 0a41 va/1.8.0_151)..A
0x0100: 7574 686f 7269 7a61 7469 6f6e 3a20 4261 uthorization:.Ba
0x0110: 7369 6320 6247 6c75 6132 4d36 527a 4e61 sic.bGlua2M6RzNa
0x0120: 5954 4e4e 5755 593d 0d0a 0d0a 7b22 696e YTNNWUY=....{"in
0x0130: 6465 7822 3a7b 225f 696e 6465 7822 3a22 dex":{"_index":"
0x0140: 6170 695f 7472 6163 6b22 2c22 5f74 7970 api_track","_typ
0x0150: 6522 3a22 5f64 6f63 227d 7d0a 7b22 6372 e":"_doc"}}.{"cr
0x0160: 6561 7465 6441 7422 3a22 3230 3230 2d30 eatedAt":"2020-0
0x0170: 322d 3132 5430 383a 3438 3a34 332e 3333 2-12T08:48:43.33
0x0180: 3822 2c22 686f 7374 4164 6472 223a 2231 8","hostAddr":"1
0x0190: 3237 2e30 2e30 2e31 222c 2274 656e 616e 27.0.0.1","tenan
0x01a0: 7449 6422 3a6e 756c 6c2c 2273 6572 7665 tId":null,"serve
0x01b0: 724e 616d 6522 3a6e 756c 6c2c 2270 6172 rName":null,"par
0x01c0: 616d 7322 3a22 2670 7269 7661 7465 5f74 ams":"&private_t
0x01d0: 6f6b 656e 3d2a 2a2a 266f 7264 6572 5f62 oken=***&order_b
0x01e0: 793d 7369 7a65 222c 2275 7365 724e 616d y=size","userNam
0x01f0: 6522 3a22 6d79 7363 6d22 2c22 7573 6572 e":"myscm","user
0x0200: 4964 223a 3330 3034 2c22 7061 7468 6e61 Id":3004,"pathna
0x0210: 6d65 223a 222f 6170 692f 7634 2f70 726f me":"/api/v4/pro
0x0220: 6a65 6374 732f 3339 3235 352f 6c61 6e67 jects/39255/lang
0x0230: 7561 6765 7322 2c22 7265 6d6f 7465 4164 uages","remoteAd
0x0240: 6472 223a 2233 302e 3339 2e32 3232 2e32 dr":"30.39.222.2
0x0250: 3237 222c 2275 7064 6174 6564 4174 223a 27","updatedAt":
0x0260: 2232 3032 302d 3032 2d31 3254 3038 3a34 "2020-02-12T08:4
0x0270: 383a 3433 2e33 3338 227d 0a 8:43.338"}.
It can be done by client.getLowLevelClient().performRequest(new Request(method, endpoint)), fill INDEX into parameter of method.

How to format WHOIS response

I need to implement a "whois" service, that will be listening for whois queries (whois -h ) and will respond with information from phpipam API. I am listening on port 43 with xinetd that starts script.
My problem is that when I send a response from the server, the server after first new line, resets connection. For testing purposes I am simply trying to send two echo lines and result is the same.
Script:
#!/bin/bash
echo "One"
echo "Two"
lsof:
# lsof -i | grep whois
xinetd 17696 root 6u IPv4 91809 0t0 TCP *:whois (LISTEN)
whois query (from client):
# whois -h 192.168.4.129 44.44.44.44
One
fgets: Connection reset by peer
tcpdump:
# tcpdump -n -i any -X port 43
16:34:01.162440 IP 192.168.4.128.49374 > 192.168.4.129.nicname: Flags [S], seq 1949234703, win 29200, options [mss 1460,sackOK,TS val 337480796 ecr 0,nop,wscale 7], length 0
0x0000: 4500 003c 7a53 4000 4006 3617 c0a8 0480 E..<zS#.#.6.....
0x0010: c0a8 0481 c0de 002b 742e f60f 0000 0000 .......+t.......
0x0020: a002 7210 8a80 0000 0204 05b4 0402 080a ..r.............
0x0030: 141d 8c5c 0000 0000 0103 0307 0000 0000 ...\............
0x0040: 0000 0000 0000 0000 0000 0000 ............
16:34:01.162916 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [S.], seq 3654394026, ack 1949234704, win 28960, options [mss 1460,sackOK,TS val 25631110 ecr 337480796,nop,wscale 7], length 0
0x0000: 4500 003c 0000 4000 4006 b06a c0a8 0481 E..<..#.#..j....
0x0010: c0a8 0480 002b c0de d9d1 a0aa 742e f610 .....+......t...
0x0020: a012 7120 eb31 0000 0204 05b4 0402 080a ..q..1..........
0x0030: 0187 1986 141d 8c5c 0103 0307 0000 0000 .......\........
0x0040: 0000 0000 0000 0000 0000 0000 ............
16:34:01.162946 IP 192.168.4.128.49374 > 192.168.4.129.nicname: Flags [.], ack 1, win 229, options [nop,nop,TS val 337480797 ecr 25631110], length 0
0x0000: 4500 0034 7a54 4000 4006 361e c0a8 0480 E..4zT#.#.6.....
0x0010: c0a8 0481 c0de 002b 742e f610 d9d1 a0ab .......+t.......
0x0020: 8010 00e5 8a78 0000 0101 080a 141d 8c5d .....x.........]
0x0030: 0187 1986 0000 0000 0000 0000 0000 0000 ................
0x0040: 0000 0000 ....
16:34:01.163348 IP 192.168.4.128.49374 > 192.168.4.129.nicname: Flags [P.], seq 1:14, ack 1, win 229, options [nop,nop,TS val 337480798 ecr 25631110], length 13
0x0000: 4500 0041 7a55 4000 4006 3610 c0a8 0480 E..AzU#.#.6.....
0x0010: c0a8 0481 c0de 002b 742e f610 d9d1 a0ab .......+t.......
0x0020: 8018 00e5 8a85 0000 0101 080a 141d 8c5e ...............^
0x0030: 0187 1986 3434 2e34 342e 3434 2e34 340d ....44.44.44.44.
0x0040: 0a00 0000 0000 0000 0000 0000 0000 0000 ................
0x0050: 00 .
16:34:01.164122 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [.], ack 14, win 227, options [nop,nop,TS val 25631110 ecr 337480798], length 0
0x0000: 4500 0034 b92d 4000 4006 f744 c0a8 0481 E..4.-#.#..D....
0x0010: c0a8 0480 002b c0de d9d1 a0ab 742e f61d .....+......t...
0x0020: 8010 00e3 8a2c 0000 0101 080a 0187 1986 .....,..........
0x0030: 141d 8c5e 0000 0000 0000 0000 0000 0000 ...^............
0x0040: 0000 0000 ....
16:34:01.165523 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [P.], seq 1:5, ack 14, win 227, options [nop,nop,TS val 25631110 ecr 337480798], length 4
0x0000: 4500 0038 b92e 4000 4006 f73f c0a8 0481 E..8..#.#..?....
0x0010: c0a8 0480 002b c0de d9d1 a0ab 742e f61d .....+......t...
0x0020: 8018 00e3 d5a7 0000 0101 080a 0187 1986 ................
0x0030: 141d 8c5e 4f6e 650a 0000 0000 0000 0000 ...^One.........
0x0040: 0000 0000 0000 0000 ........
16:34:01.165844 IP 192.168.4.128.49374 > 192.168.4.129.nicname: Flags [.], ack 5, win 229, options [nop,nop,TS val 337480800 ecr 25631110], length 0
0x0000: 4500 0034 7a56 4000 4006 361c c0a8 0480 E..4zV#.#.6.....
0x0010: c0a8 0481 c0de 002b 742e f61d d9d1 a0af .......+t.......
0x0020: 8010 00e5 8a78 0000 0101 080a 141d 8c60 .....x.........`
0x0030: 0187 1986 0000 0000 0000 0000 0000 0000 ................
0x0040: 0000 0000 ....
16:34:01.165914 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [R.], seq 5, ack 14, win 227, options [nop,nop,TS val 25631111 ecr 337480798], length 0
0x0000: 4500 0034 b92f 4000 4006 f742 c0a8 0481 E..4./#.#..B....
0x0010: c0a8 0480 002b c0de d9d1 a0af 742e f61d .....+......t...
0x0020: 8014 00e3 8a23 0000 0101 080a 0187 1987 .....#..........
0x0030: 141d 8c5e 0000 0000 0000 0000 0000 0000 ...^............
0x0040: 0000 0000 ....
16:34:01.166350 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [R], seq 3654394031, win 0, length 0
0x0000: 4500 0028 826a 4000 4006 2e14 c0a8 0481 E..(.j#.#.......
0x0010: c0a8 0480 002b c0de d9d1 a0af 0000 0000 .....+..........
0x0020: 5004 0000 ea03 0000 0000 0000 0000 0000 P...............
0x0030: 0000 0000 0000 0000 0000 0000 0000 ..............
If I try to connect to 43 with telnet, everything work fine:
# telnet 192.168.4.129 43
Trying 192.168.4.129...
Connected to 192.168.4.129.
Escape character is '^]'.
One
Two
Connection closed by foreign host.
UPDATE:
Loop like this works fine
while read line; do
echo "One"
echo "Two"
break
done
But still I can't understand why it is not working by just passing two echo lines, or even if I try to run a while loop with counter:
#!/bin/bash
COUNT=0
while [ $COUNT -lt 5 ]; do
echo The counter is $COUNT
let COUNT=COUNT+1
done
It stops after the first cycle:
# whois -h 192.168.4.129 44.44.44.44
The counter is 0
fgets: Connection reset by peer
Maybe you have any ideas, what is the reason/difference between these two loops?
My /etc/xinetd.d/whoistest:
service whois
{
port = 43
flags = REUSE
socket_type = stream
protocol = tcp
user = root
wait = no
type = UNLISTED
server = /path/to/shell/script
disable = no
log_type = SYSLOG authpriv
log_on_failure += USERID
}
Any support will be appreciated.
You need to make sure your xinetd is configured correctly. Your script also needs to handle input from whois client. This works:
#!/bin/bash
while read line; do
echo $line
echo "foo"
break
done
This reads your line, echoes it back and then something. Feel free to replace echo $line with echo whatever you want.
I had to configure xinetd like this for it to work:
service whois
{
port = 43
flags = REUSE
socket_type = stream
protocol = tcp
user = root
wait = no
type = UNLISTED
server = /tmp/w.sh
disable = no
}
Hope this helps. If your xinetd service is configured wrong, it just seems to drop the connection. If your server doesn't actually listen to input from client, you'll get connection reset by peer errors.
Hannu

spring cloud config server don't use profile

I use:
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR6</version>
in my application.properties I have put
spring.profiles.active= prod
spring.application.name= FunctionalControl
I can see on the log :
2016-10-20 17:58:17.900 INFO c.o.l.f.Application - The following profiles are active: prod
But if I use tcpdump I can see than despite does a GET /FunctionalControl/prod
on my spring.cloud.config server It does GET /FunctionalControl/default
0x0030: 15f1 ddb3 4745 5420 2f46 756e 6374 696f ....GET./Functio
0x0040: 6e61 6c43 6f6e 7472 6f6c 2f64 6566 6175 nalControl/defau
0x0050: 6c74 2048 5454 502f 312e 310d 0a41 6363 lt.HTTP/1.1..Acc
0x0060: 6570 743a 2061 7070 6c69 6361 7469 6f6e ept:.application
0x0070: 2f6a 736f 6e2c 2061 7070 6c69 6361 7469 /json,.applicati
0x0080: 6f6e 2f2a 2b6a 736f 6e0d 0a55 7365 722d on/*+json..User-
So I never manage to use FunctionalControl-prod.properties from my git repo :-(
I found myself the solution and share it with everybody.
The active profile for config server is taken in consideration only if it is put into bootstrap.properties (or .yaml) not in application.properties
file bootstrap.properties:
spring.profiles.active= prod
spring.application.name= FunctionalControl
I add this file in my project and then it works fine as you can see on this tcpdump
0x0000: 4500 00d3 0799 4000 4006 348a 7f00 0001 E.....#.#.4.....
0x0010: 7f00 0001 9f40 22b8 8800 ede0 32d2 4aa8 .....#".....2.J.
0x0020: 8018 0156 fec7 0000 0101 080a 16da ea48 ...V...........H
0x0030: 16da ea48 4745 5420 2f46 756e 6374 696f ...HGET./Functio
0x0040: 6e61 6c43 6f6e 7472 6f6c 2f70 726f 6420 nalControl/prod.
0x0050: 4854 5450 2f31 2e31 0d0a 4163 6365 7074 HTTP/1.1..Accept
0x0060: 3a20 6170 706c 6963 6174 696f 6e2f 6a73 :.application/js
0x0070: 6f6e 2c20 6170 706c 6963 6174 696f 6e2f on,.application/
0x0080: 2a2b 6a73 6f6e 0d0a 5573 6572 2d41 6765 *+json..User-Age
0x0090: 6e74 3a20 4a61 7661 2f31 2e38 2e30 5f34 nt:.Java/1.8.0_4
0x00a0: 350d 0a48 6f73 743a 206c 6f63 616c 686f 5..Host:.localho
0x00b0: 7374 3a38 3838 380d 0a43 6f6e 6e65 6374 st:8888..Connect
0x00c0: 696f 6e3a 206b 6565 702d 616c 6976 650d ion:.keep-alive.
You need to define the active profile for your config client application (in your case "FunctionalControl"). There are multiple ways to do that.
E.g. you can add the following to your client's bootstrap.properties file
spring.profiles.active=prod
or e.g. start you config client application via
mvn spring-boot:run -Dspring.profiles.active=prod
Either way, your client server application doesn't need any active profile. It will server whatever file is available in your git repo.

CasperJS "unable to load script"

I have the strangest issue while writing a new script for casperJS, which I use all the time.
When I try to launch the script, using "casperjs myScript.js, I get the following :
Unable to load script /var/www/scrapers/myScript.js; check file syntax
I checked my syntax and found nothing wrong, chmod +x the file, sudoed everything, still the same.
Even stranger, I copypasted the example code from the quickstart (http://docs.casperjs.org/en/latest/quickstart.html), saved , and same message...
All my other scripts in the same directory are working, what do I try now?
Re: what do I try now?
You can use xxd to look inside the files. Looking at the comments, you now have a working version and a non-working version, so can compare them side-by-side to understand what the problem was. (And if they are identical then you know you are looking for something at the file system level, not inside the files.)
(xxd comes with the vi package, and apparently is also available for Windows if you install vim.)
Here is sample output for a script with and without the UTF-8 BOM (which, incidentally, made no difference: both these scripts worked, at least with CasperJS 1.1 on Ubuntu):
0000000: efbb bf76 6172 2063 6173 7065 7220 3d20 ...var casper =
0000010: 7265 7175 6972 6528 2763 6173 7065 7227 require('casper'
0000020: 292e 6372 6561 7465 2829 3b0a 0a63 6173 ).create();..cas
0000030: 7065 722e 7374 6172 7428 2768 7474 703a per.start('http:
0000040: 2f2f 676f 6f67 6c65 2e63 6f6d 2f27 2c20 //google.com/',
0000050: 6675 6e63 7469 6f6e 2829 207b 0a20 2020 function() {.
0000060: 2074 6869 732e 6563 686f 2874 6869 732e this.echo(this.
0000070: 6765 7454 6974 6c65 2829 293b 0a7d 293b getTitle());.});
0000080: 0a0a 6361 7370 6572 2e72 756e 2829 3b0a ..casper.run();.
0000000: 7661 7220 6361 7370 6572 203d 2072 6571 var casper = req
0000010: 7569 7265 2827 6361 7370 6572 2729 2e63 uire('casper').c
0000020: 7265 6174 6528 293b 0a0a 6361 7370 6572 reate();..casper
0000030: 2e73 7461 7274 2827 6874 7470 3a2f 2f67 .start('http://g
0000040: 6f6f 676c 652e 636f 6d2f 272c 2066 756e oogle.com/', fun
0000050: 6374 696f 6e28 2920 7b0a 2020 2020 7468 ction() {. th
0000060: 6973 2e65 6368 6f28 7468 6973 2e67 6574 is.echo(this.get
0000070: 5469 746c 6528 2929 3b0a 7d29 3b0a 0a63 Title());.});..c
0000080: 6173 7065 722e 7275 6e28 293b 0a asper.run();.

How to find number of ways to color N*M Grid?

I am solving the code contest code and i am stuck on this problem
Calculate the number of ways to color an N * M grid using K colors.Adjacent squares in the grid should have different colors. Squares are considered adjacent if they share an edge.
Input Format
The first line contains an integer T denoting the number of test-cases.The next T lines contains integers N, M and K separated by a single space.
Sample Input
3
3 3 2
3 4 3
1 1 1
Sample Output
2
1122
1
I have attached the code in Java to solve your problem. There is not a simple formula because there are up to four squares that touch each square. This is a sample run of my program without it printing out each solution. Note that it matches the problems sample input/output.
Enter number of colors: 2
Enter number of rows: 3
Enter number of columns: 3
There are 2 different combinations of 2 colors.
Enter number of colors: 3
Enter number of rows: 4
Enter number of columns: 3
There are 1122 different combinations of 3 colors.
Enter number of colors: 1
Enter number of rows: 1
Enter number of columns: 1
There are 1 different combinations of 1 colors.
Here is the output for 3 colors, and 2 by 4 grid, printing each solution that it found. For this input, the answer is 162. For the source code, go straight to the bottom...
Enter number of colors: 3
Enter number of rows: 2
Enter number of columns: 4
1212
2121
1212
2123
1213
2121
1212
2131
1213
2131
1213
2132
1231
2123
1232
2121
1232
2123
1212
2321
1212
2323
1213
2321
1231
2312
1231
2313
1232
2313
1231
2323
1232
2321
1232
2323
1312
2121
1312
2123
1313
2121
1312
2131
1313
2131
1313
2132
1321
2132
1323
2131
1323
2132
1212
3121
1212
3123
1213
3121
1212
3131
1213
3131
1213
3132
1231
3123
1232
3121
1232
3123
1312
3121
1312
3123
1313
3121
1312
3131
1313
3131
1313
3132
1321
3132
1323
3131
1323
3132
1312
3231
1313
3231
1313
3232
1321
3212
1321
3213
1323
3212
1321
3232
1323
3231
1323
3232
2121
1212
2121
1213
2123
1212
2121
1232
2123
1231
2123
1232
2131
1212
2131
1213
2132
1213
2121
1312
2121
1313
2123
1312
2131
1312
2131
1313
2132
1313
2131
1323
2132
1321
2132
1323
2312
1231
2313
1231
2313
1232
2321
1212
2321
1213
2323
1212
2321
1232
2323
1231
2323
1232
2121
3212
2121
3213
2123
3212
2121
3232
2123
3231
2123
3232
2131
3212
2131
3213
2132
3213
2312
3121
2312
3123
2313
3121
2312
3131
2313
3131
2313
3132
2321
3132
2323
3131
2323
3132
2312
3231
2313
3231
2313
3232
2321
3212
2321
3213
2323
3212
2321
3232
2323
3231
2323
3232
3121
1212
3121
1213
3123
1212
3121
1232
3123
1231
3123
1232
3131
1212
3131
1213
3132
1213
3121
1312
3121
1313
3123
1312
3131
1312
3131
1313
3132
1313
3131
1323
3132
1321
3132
1323
3212
1321
3212
1323
3213
1321
3231
1312
3231
1313
3232
1313
3231
1323
3232
1321
3232
1323
3121
2312
3121
2313
3123
2312
3131
2312
3131
2313
3132
2313
3131
2323
3132
2321
3132
2323
3212
2121
3212
2123
3213
2121
3212
2131
3213
2131
3213
2132
3231
2123
3232
2121
3232
2123
3212
2321
3212
2323
3213
2321
3231
2312
3231
2313
3232
2313
3231
2323
3232
2321
3232
2323
There are 162 different combinations of 3 colors.
Here is the java code:
import java.util.*;
public class ColorChoices
{
int[][] grid;
int numberOfColors;
int numberOfRows;
int numberOfColumns;
int numberOfCombinations;
public static void main(String[] args)
{
ColorChoices solution = new ColorChoices();
solution.begin();
}
void begin()
{
numberOfCombinations = 0;
Scanner consoleInput = new Scanner(System.in);
System.out.print("Enter number of colors: ");
numberOfColors = consoleInput.nextInt();
System.out.print("Enter number of rows: ");
numberOfRows = consoleInput.nextInt();
System.out.print("Enter number of columns: ");
numberOfColumns = consoleInput.nextInt();
grid = new int[numberOfRows][numberOfColumns];
solve(0, 0);
System.out.println("There are " + numberOfCombinations + " different combinations of " + numberOfColors + " colors.");
}
void solve(int r, int c)
{
for(int i = 1; i <= numberOfColors; i++)
{
if(valid(r, c, i))
{
grid[r][c] = i;
if(r == numberOfRows - 1 && c == numberOfColumns - 1)
{
printBoard();
numberOfCombinations++;
}
else if(r == numberOfRows - 1) solve(0, c + 1);
else solve(r + 1, c);
}
}
grid[r][c] = 0;
}
boolean valid(int r, int c, int n)
{
return(leftOK(r, c, n) && rightOK(r, c, n) && topOK(r, c, n) && bottomOK(r, c, n));
}
boolean leftOK(int r, int c, int n)
{
if(c == 0) return true;
if(grid[r][c - 1] != n) return true;
return false;
}
boolean rightOK(int r, int c, int n)
{
if(c == numberOfColumns - 1) return true;
if(grid[r][c + 1] != n) return true;
return false;
}
boolean topOK(int r, int c, int n)
{
if(r == 0) return true;
if(grid[r - 1][c] != n) return true;
return false;
}
boolean bottomOK(int r, int c, int n)
{
if(r == numberOfRows - 1) return true;
if(grid[r + 1][c] != n) return true;
return false;
}
void printBoard()
{
for(int r = 0; r < numberOfRows; r++)
{
for(int c = 0; c < numberOfColumns; c++)
{
System.out.print(grid[r][c]);
}
System.out.println();
}
System.out.println();
}
}

Resources