How to fix the bit rate of ffmpeg - java

I am building simple java application to broadcast the webcam to the server listening on
localhost:8082
with following java program
private String frameRate = "60";
private String bitRate = "100k";
private String param = " -s 640x480 -f video4linux2 -i /dev/video0 -f mpeg1video -r "+frameRate+" -b:v "+bitRate+" http://localhost:8082/raj/640/480/";
private String command =" ffmpeg ";
//to execute
proc = rt.exec(command +" "+param);
proc.waitFor();
Following is the output from starting point to 5 sec.
frame= 27 fps= 0 q=5.4 size= 88kB time=0.43 bitrate=1655.4kbits/s dup=18 drop=0
frame= 59 fps= 55 q=11.6 size= 146kB time=0.97 bitrate=1238.3kbits/s dup=45 drop=0
frame= 94 fps= 60 q=26.1 size= 207kB time=1.55 bitrate=1095.6kbits/s dup=69 drop=0
frame= 125 fps= 60 q=14.3 size= 235kB time=2.07 bitrate= 929.9kbits/s dup=83 drop=0
frame= 156 fps= 60 q=16.1 size= 276kB time=2.58 bitrate= 875.5kbits/s dup=97 drop=0
frame= 187 fps= 60 q=15.4 size= 306kB time=3.10 bitrate= 807.9kbits/s dup=111 drop=0
frame= 218 fps= 60 q=22.1 size= 343kB time=3.62 bitrate= 777.8kbits/s dup=125 drop=0
frame= 249 fps= 60 q=24.8 size= 379kB time=4.13 bitrate= 751.7kbits/s dup=139 drop=0
frame= 280 fps= 60 q=31.0 size= 402kB time=4.65 bitrate= 708.8kbits/s dup=153 drop=0
frame= 311 fps= 60 q=31.0 size= 436kB time=5.17 bitrate= 690.7kbits/s dup=167 drop=0
frame= 342 fps= 60 q=31.0 size= 458kB time=5.68 bitrate= 659.5kbits/s dup=181 drop=0
Initially, bit rate is ok, but it is in decreasing order. Is this due to java program or ffmped. How to fix the bit rate?

If the image has low motion, ( like a security camera ) the encoder may not need any more than 700kbits to encode the image. If you look at the q value, it is at 31 (31 is the max) so the quantizer is encoding at the highest quality possible and still can hit the target bit rate. You can increase the resolution to give the encoder more work to go, or you can add padding. Or you can just enjoy the bandwidth savings :)

Related

How to make 10x10 printf table display consistently in java?

I'm trying to display a 10x10 table of an ArrayList in console using printf.
like this:
930 396 466 242 315 254 217 820 287 216
595 220 13 494 186 645 309 902 560 56
797 980 201 301 479 694 509 778 702 360
253 995 647 725 327 774 861 420 37 753
948 107 935 867 399 818 73 427 485 70
575 385 174 400 940 296 76 569 362 732
21 197 948 421 852 954 640 528 119 659
96 55 475 5 903 940 299 45 432 79
352 363 698 873 130 704 89 245 45 288
646 378 967 179 94 607 261 710 504 20
The code works sometimes, however it will randomly make a new line or fail to make one.
public static void main(String[] args) {
// This creates an ArrayList of 100 random integers between the values from 1 to 1000
int listSize = 100;
ArrayList<Integer> list = createList(listSize);
displayList(list);
}
private static void displayTable(ArrayList<Integer> list) {
System.out.println();
for (Integer num : list) {
if (list.indexOf(num) % 10 == 0) {
System.out.printf("%n%5d", num);
} else {
System.out.printf("%5d", num);
}
}
System.out.println();
}
Here are two examples of outputs I've gotten:
969 500 12 256 945 105 402 868 213 658
909 144 165 217 828 628 395 682 816 199
769 220 218 958 97 237 36 92 220 712
332 640 547 893 210 926 868 486 914 307
740 962 109 745 347 896 74 922 686 593
26 964 677 321 889 690 956 892 720 915
631 90 824 338 887 822 49 529 521 841
504 946 302 253 175 107 765 225 6 101
747 841 143 642 533 662 143 528 733 209
377 366 928 511 404
26 296 946 597 717
673 257 970 480 595 936 1000 490 937 45
156 619 722 237 448 611 266 603 84 421
719 8 341 720 284 170 885 740 587 686
182 111 533 268 455 804 494 14 161 38
612
612 235 758 366 607 354 591 914 791
277 426 318 204 692 851 727 654 696 7
504 801 213 368 834 928 141 951 714 340
190 325 129 930 923 654 997 903 569 867
956 736 712 586 560 770 497 875 854 618
998 281 953 747 828 212 844 314 494 367
As #akuzminykh said, you have an issue with how you deal with your random number index in your array.
To pe precise, this line:
if (list.indexOf(num) % 10 == 0) {
Will return the index of the FIRST num in your array, so if your num corresponds to a previous breakline index (in your case, %10), you will have one new line out of nowhere!
Try using a hand made index with your for loop. Or a counter.
Look at your example:
969 500 12 256 945 105 402 868 213 658
909 144 165 217 828 628 395 682 816 199
769 220 218 958 97 237 36 92 220 712
332 640 547 893 210 926 868 486 914 307
740 962 109 745 347 896 74 922 686 593
26 964 677 321 889 690 956 892 720 915 <--- Starting with 26
631 90 824 338 887 822 49 529 521 841
504 946 302 253 175 107 765 225 6 101
747 841 143 642 533 662 143 528 733 209
377 366 928 511 404
26 296 946 597 717 <--- Starting with 26

What is the parseBase64Binary function in Python?

In Java, I use:
String str = “%EF!c&WrDwCCTe<fX$,#8L<YTs?G5d>F])ub.63G=Xn<cdef2R{47JQexxN”;
byte[] result = DatatypeConverter.parseBase64Binary(str);
for(byte i : result){
System.out.print(i);
System.out.print(" ");
}
to decode str.
Output:
16 87 22 -84 60 2 9 55 -97 95 -62 -40 78 -63 -71 116 91 -101 -21 113 94 119 29 121 -3 -111 -29 -78 80 123 28 77
Now I need to decode str with Base64 in python but I don't know which lib and function should I choose.
I've tried base64.b64decode but its result is different from that in Java.
str = '%EF!c&WrDwCCTe<fX$,#8L<YTs?G5d>F])ub.63G=Xn<cdef2R{47JQexxN'
result = base64.b64decode(str)
print(result)
for i in range(0, len(decode_secret)):
print(decode_secret[i], end=" ")
Output:
b'\x10W\x16\xac<\x02\t7\x9f_\xc2\xd8N\xc1\xb9t[\x9b\xebq'
16 87 22 172 60 2 9 55 159 95 194 216 78 193 185 116 91 155 235 113

HashMap deleting itself inside a HashMap

The goal of my project is to create a League of teams, and have rosters of each teams. I'm supposed to use 2 different maps. One map is for holding the Team name as a string, with it's object (Team). The other map is for holding the roster with the number and the object (Player). Whenever I run this code, and try to test whether the roster is printing out the right numbers, I have a problem. The roster seems to clear itself whole. I know that I have put a Roster.clear() method in, but that is to reset the roster to put players from other teams in. If you need the team class, I will put it in at the bottom. Thank you SO much if anyone is able to answer my question.
import java.util.*;
public class League{
public League(Scanner s){
Map<String,Team> League = new HashMap<String,Team>();
Map<Integer,Player> Roster = new HashMap<Integer, Player>();
Team t;
Player p;
String team,
name;
Boolean throwsRightHanded;
int number,
position,
plateApp,
walks,
strikeouts,
hits,
inningsPitched,
earnedRuns,
atBats,
runsBattedIn,
homeRuns,
hitByPitch;
team = s.next();
while (s.hasNext()){
if (!s.hasNext("-1")){
number = s.nextInt();
name = s.next();
position = s.nextInt();
if (s.next() == "t"){
throwsRightHanded = true;
}
else{
throwsRightHanded = false;
}
plateApp = s.nextInt();
walks = s.nextInt();
strikeouts = s.nextInt();
hits = s.nextInt();
if (position == 1){
//Pitcher
inningsPitched = s.nextInt();
earnedRuns = s.nextInt();
p = new Pitcher(inningsPitched, earnedRuns, number, name, position,
throwsRightHanded, plateApp, walks, strikeouts, hits);
}
else{
atBats = s.nextInt();
runsBattedIn = s.nextInt();
homeRuns = s.nextInt();
hitByPitch = s.nextInt();
p = new PositionPlayer(atBats, runsBattedIn, homeRuns, hitByPitch, number, name, position,
throwsRightHanded, plateApp, walks, strikeouts, hits);
}
Roster.put(number, p);
}
else{
t = new Team(team, Roster);
League.put(team, t);
Roster.clear();
s.next();
if (s.hasNext())
team = s.next();
}
}
Iterator<Team> it = League.values().iterator();
Team teamIterator;
while (it.hasNext()){
teamIterator = it.next();
System.out.println(teamIterator);
Iterator<Player> itt = teamIterator.getRoster().values().iterator();
while (itt.hasNext()){
System.out.println(itt.next());
}
}
}
The important part is below:
else{
t = new Team(team, Roster);
League.put(team, t);
Roster.clear();
s.next();
if (s.hasNext())
team = s.next();
}
}
Iterator<Team> it = League.values().iterator();
Team teamIterator;
while (it.hasNext()){
teamIterator = it.next();
System.out.println(teamIterator);
Iterator<Player> itt = teamIterator.getRoster().values().iterator();
while (itt.hasNext()){
System.out.println(itt.next());
}
}
}
Team Class:
public class Team{
private Map<Integer,Player> roster;
private String t;
public Team(String tname, Map<Integer,Player> floatroster){
setRoster(floatroster);
t = tname;
}
public String lookupPlayer(int n){
if (!roster.containsKey(n)){
System.out.println("No player with number " + n + " is on the roster for the " + t);
return null;
}
else{
return roster.values().toString();
}
}
public String toString(){
return t;
}
public void setRoster(Map<Integer,Player> floatroster){
roster = floatroster;
}
public Map<Integer, Player> getRoster(){
return roster;
}
}
File I'm reading from:
RedSox
65 Jonathan 1 t 416 23 80 111
259 32
68 Matt 1 t 399 28 56 90
241 37
32 Craig 1 f 383 29 55 121
245 35
11 Clay 1 t 418 32 63 105
241 37
46 Ryan 1 t 408 24 59 97
228 35
51 Edwin 1 f 392 32 64 82
232 36
37 Heath 1 t 419 31 53 109
233 38
62 Rich 1 f 411 33 36 77
263 36
61 Brian 1 f 397 25 59 107
240 32
56 Joe 1 t 390 32 57 89
201 36
59 Tommy 1 f 408 26 69 104
284 35
64 Jean 1 t 421 44 37 94
263 33
70 Roman 1 t 428 31 46 91
252 35
20 Wade 1 f 386 36 49 73
256 38
41 Alex 1 t 418 28 61 70
240 36
60 Henry 1 f 401 26 74 115
255 37
22 Rick 1 t 420 20 65 88
226 36
66 Noe 1 t 408 27 61 80
225 36
52 Eduardo 1 f 365 34 67 100
217 34
28 Robbie 1 f 382 32 55 96
221 34
36 Junichi 1 t 401 23 60 88
267 39
19 Koji 1 t 364 28 47 113
236 35
67 Brandon 1 t 392 25 66 122
210 37
10 Ryan 2 t 592 44 82 210
406 95 29 5
23 Blake 2 t 577 29 99 121
432 80 16 3
7 Christian 2 t 599 34 117 196
416 95 31 6
47 Travis 3 t 669 58 78 201
451 136 34 1
48 Pablo 5 t 628 45 74 139
368 96 1 0
13 Hanley 3 t 575 37 94 148
388 80 23 0
15 Dustin 4 t 635 36 105 227
304 113 28 1
26 Brock 4 t 1443 33 83 422
454 274 50 2
5 Allen 3 t 654 40 108 125
383 60 2 1
2 Xander 6 t 602 48 95 137
420 64 14 3
50 Mookie 8 t 584 35 84 199
323 110 31 7
25 Jackie 7 t 608 53 92 161
528 69 1 5
38 Rusney 9 t 626 47 69 194
422 135 31 0
34 David 3 f 567 63 99 108
399 55 1 6
-1
Yankees
38 Andrew 1 t 388 22 67 119
213 37
68 Dellin 1 t 364 34 65 86
265 32
26 Chris 1 f 397 19 74 118
232 35
65 Caleb 1 t 424 33 64 88
279 36
30 Nathan 1 t 381 35 59 85
250 37
74 Nick 1 t 417 33 80 92
235 33
33 Chris 1 t 414 25 52 85
238 38
48 Andrew 1 f 386 28 66 110
242 35
55 Brian 1 t 419 25 48 76
273 34
47 Ivan 1 t 445 24 51 108
256 35
67 James 1 f 427 26 51 120
217 37
57 Branden 1 t 416 22 73 106
212 37
35 Michael 1 t 398 28 61 109
283 35
50 Nick 1 t 426 24 62 95
231 37
45 Chasen 1 f 399 18 60 74
243 35
52 CC 1 f 397 23 49 108
234 35
40 Luis 1 t 405 20 64 131
240 38
19 Masahiro 1 t 420 31 26 89
195 33
43 Adam 1 t 416 28 54 98
240 33
41 Justin 1 f 365 26 49 104
251 35
34 Brian 2 t 637 41 79 147
365 66 1 1
66 John 2 t 584 58 81 150
434 92 21 1
53 Austin 2 t 593 47 98 122
423 62 17 3
73 Gary 2 t 709 43 66 194
407 152 35 2
31 Greg 3 t 672 66 78 157
299 60 1 5
14 Stephen 4 t 607 44 53 158
390 99 16 5
18 Didi 6 t 613 33 55 217
479 126 29 3
12 Chase 5 t 553 58 97 146
332 83 2 5
63 Jose 4 t 683 49 124 124
380 71 12 2
64 Robert 4 t 662 43 96 144
395 65 6 2
17 Brendan 4 t 658 55 61 162
382 96 28 2
29 Dustin 7 t 587 63 82 156
461 119 22 1
36 Carlos 9 t 545 50 94 129
373 90 4 4
22 Jacoby 8 f 657 42 43 142
473 51 1 3
11 Brett 7 f 551 32 120 145
406 131 26 1
72 Slade 8 f 543 50 97 132
399 70 23 2
70 Rico 9 t 646 22 83 259
433 175 40 6
25 Chris 9 t 576 34 103 153
432 74 6 3
-1
BlueJays
56 Mark 1 f 440 21 63 123
208 33
27 Brett 1 f 405 31 63 117
218 36
50 Steve 1 t 438 30 74 106
251 38
43 RA 1 t 384 32 72 88
254 38
25 Marco 1 t 438 27 67 104
243 34
35 Jeff 1 f 409 32 63 100
228 32
32 LaTroy 1 t 402 21 66 57
261 36
31 Liam 1 t 380 25 74 100
252 34
36 Drew 1 t 375 26 78 83
251 36
64 Chad 1 t 421 27 63 97
253 31
62 Aaron 1 f 403 33 64 96
277 35
57 Mark 1 t 376 24 59 113
234 34
54 Roberto 1 t 423 21 58 121
247 34
14 David 1 f 432 26 54 119
201 38
41 Aaron 1 t 410 18 60 107
259 36
8 AJ 2 t 610 44 115 151
400 54 12 2
55 Russell 2 t 646 49 79 145
300 79 9 2
30 Dioner 2 t 686 38 71 111
434 76 21 4
15 Chris 3 t 606 47 77 126
440 53 11 3
20 Josh 5 t 605 63 100 150
446 48 8 1
17 Ryan 4 t 610 21 89 135
420 89 23 2
9 Cliff 4 t 531 73 94 169
442 47 14 4
13 Justin 3 f 651 40 85 158
380 82 5 3
2 Troy 6 t 658 38 87 127
399 57 16 2
19 Jose 9 t 712 44 115 239
429 145 28 1
3 Ezequiel 7 f 589 57 104 136
351 75 18 2
11 Kevin 8 t 671 41 81 158
380 155 31 1
45 Dalton 8 t 638 36 104 246
343 115 30 2
7 Ben 7 t 621 37 98 149
382 86 5 4
-1
Orioles
35 Brad 1 t 392 35 69 116
233 32
53 Zach 1 f 451 31 45 78
239 34
16 Wei-yin 1 f 359 25 59 108
237 39
71 Oliver 1 t 426 21 62 106
241 37
61 Jason 1 t 385 22 56 103
242 33
39 Kevin 1 t 383 33 59 104
229 38
60 Mychal 1 t 397 26 65 118
261 36
50 Miguel 1 t 411 32 64 83
236 36
31 Ubaldo 1 t 371 35 69 81
227 35
52 Steve 1 t 393 32 64 97
257 39
17 Brian 1 f 425 28 62 113
227 36
66 T.J. 1 f 386 30 66 91
244 35
56 Darren 1 t 395 29 75 97
265 35
65 Chaz 1 t 382 18 46 75
232 34
57 Jorge 1 t 406 20 58 93
242 33
30 Chris 1 t 394 38 67 89
226 33
63 Tyler 1 t 420 27 69 88
278 35
59 Mike 1 t 358 30 64 124
287 37
45 Steve 2 t 667 55 130 141
452 57 1 3
36 Caleb 2 t 545 48 81 165
403 47 10 0
32 Matt 2 t 625 55 97 119
453 46 0 2
19 Chris 3 t 636 40 88 95
438 54 0 2
3 Ryan 4 t 722 35 85 166
436 87 28 2
2 J.J. 6 t 668 52 99 164
417 136 32 1
15 Paul 6 t 734 33 99 141
524 78 1 1
13 Manny 5 t 665 39 63 137
420 45 10 3
6 Jonathan 4 t 744 44 70 146
402 57 17 4
34 Christian 3 t 617 40 109 128
447 58 19 3
12 Dariel 9 t 555 56 99 170
422 111 32 2
10 Adam 8 t 621 40 95 166
436 119 25 3
48 Junior 7 t 689 49 123 142
425 107 11 3
9 David 7 f 605 44 89 146
409 36 10 0
18 Gerardo 9 f 686 34 80 206
435 116 36 3
28 Steve 7 t 624 70 72 159
457 108 20 5
14 Nolan 7 t 682 55 91 140
369 62 6 5
-1
Rays
35 Matt 1 t 395 23 57 83
208 34
22 Chris 1 t 371 21 60 84
234 37
59 Andrew 1 t 420 27 60 105
250 30
26 Brad 1 t 376 28 66 110
259 37
31 Xavier 1 f 423 31 59 92
224 37
37 Alex 1 t 409 21 56 111
272 37
54 Steven 1 t 412 31 79 90
244 38
47 Brandon 1 t 407 22 69 102
226 36
51 Nathan 1 f 409 20 62 88
265 36
57 Jake 1 f 373 27 78 101
223 36
55 Matt 1 t 375 32 61 104
247 35
23 Jake 1 t 392 25 56 82
219 37
30 Erasmo 1 t 434 34 26 102
270 35
34 C.J. 1 f 408 25 69 87
235 35
45 Enny 1 f 391 26 63 106
211 35
33 Drew 1 f 385 28 75 98
226 39
49 Kirby 1 t 359 22 59 129
237 39
40 J.P. 2 t 612 39 114 163
393 99 28 2
46 Luke 2 t 593 56 124 191
371 201 38 0
44 Rene 2 t 670 39 94 158
438 62 1 3
1 Tim 4 t 630 44 98 157
390 155 29 2
13 Asdrubal 6 t 680 46 100 127
455 43 7 4
11 Logan 4 t 547 46 107 142
391 100 0 3
2 Nick 4 t 1065 47 76 214
496 157 32 4
21 James 3 t 638 56 97 217
397 103 29 1
3 Evan 5 t 624 61 102 140
442 63 16 3
36 Richie 3 t 658 36 81 143
419 63 0 2
5 Brandon 1 t 410 30 66 80
254 39
39 Kevin 1 t 414 34 63 84
238 35
27 Mikie 1 t 388 24 65 89
238 33
7 Daniel 1 t 384 23 61 89
259 35
24 Grady 1 t 371 29 68 106
224 38
20 Steven 1 t 406 16 54 103
247 35
-1
Since your input always ends each roster's data with a "-1", your current code is calling Roster.clear() -- clearing the Roster object right after it had been filled with data. That is one reason why your team rosters are always empty.
Also, you are re-using the same Roster object for every team. Every team needs its own roster.
To fix both of these issues, replace the Roster.clear() call with:
Roster = new HashMap<Integer, Player>();
That will ensure you have a new roster map for each roster, and that you never empty out a roster.

Full content is not written into a file

I've the below set of data in my text file.
10
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3
542
100
230 863 916 585 981 404 316 785 88 12 70 435 384 778 887 755 740 337 86 92 325 422 815 650 920 125 277 336 221 847 168 23 677 61 400 136 874 363 394 199 863 997 794 587 124 321 212 957 764 173 314 422 927 783 930 282 306 506 44 926 691 568 68 730 933 737 531 180 414 751 28 546 60 371 493 370 527 387 43 541 13 457 328 227 652 365 430 803 59 858 538 427 583 368 375 173 809 896 370 789
789
65
591 955 829 805 312 83 764 841 12 744 104 773 627 306 731 539 349 811 662 341 465 300 491 423 569 405 508 802 500 747 689 506 129 325 918 606 918 370 623 905 321 670 879 607 140 543 997 530 356 446 444 184 787 199 614 685 778 929 819 612 737 344 471 645 726
101
5
722 600 905 54 47
35
51
210 582 622 337 626 580 994 299 386 274 591 921 733 851 770 300 380 225 223 861 851 525 206 714 985 82 641 270 5 777 899 820 995 397 43 973 191 885 156 9 568 256 659 673 85 26 631 293 151 143 423
890
62
286 461 830 216 539 44 989 749 340 51 505 178 50 305 341 292 415 40 239 950 404 965 29 972 536 922 700 501 730 430 630 293 557 542 598 795 28 344 128 461 368 683 903 744 430 648 290 135 437 336 152 698 570 3 827 901 796 682 391 693 161 145
163
90
22 391 140 874 75 339 439 638 158 519 570 484 607 538 459 758 608 784 26 792 389 418 682 206 232 432 537 492 232 219 3 517 460 271 946 418 741 31 874 840 700 58 686 952 293 848 55 82 623 850 619 380 359 479 48 863 813 797 463 683 22 285 522 60 472 948 234 971 517 494 218 857 261 115 238 290 158 326 795 978 364 116 730 581 174 405 575 315 101 99
295
17
678 227 764 37 956 982 118 212 177 597 519 968 866 121 771 343 561
here the first number gives the number of test cases, here it is 10, and the number after it is the sum and the next line is the size of array followed by the array elements(numbers). here i need to sum the array numbers and match with the sum that is given. and print the position of the numbers that match the sum.
In the above case the results should be.
2, 3
1, 4
4, 5
29, 46
11, 56
4, 5
40, 46
16, 35
55, 74
7, 9
I've been trying the below code to write the output into a file, And also just to check i'm printing it even to the console. But here only the last case result is getting updated into file. please let me know where am i going wrong.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class FromFile {
public static void main(String args[]) throws Exception {
Scanner s = new Scanner(new File("D:/A1.txt"));
int testCaseCount = Integer.parseInt(s.next());
for (int i = 0; i < testCaseCount; i++){
int Avail = Integer.parseInt(s.next());
int size=Integer.parseInt(s.next());
ArrayList<Integer> list = new ArrayList<Integer>();
for(int j=0;j<size;j++)
{
list.add(s.nextInt());
}
for(int k=0;k<list.size()-1;k++){
for(int j=k+1; j<=list.size()-1;j++){
int sum=list.get(k)+list.get(j);
if(sum==Avail){
System.out.println((k+1)+", "+(j+1));
File file=new File("D:/A2.txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter fw=new FileWriter(file.getAbsoluteFile());
BufferedWriter bw=new BufferedWriter(fw);
bw.write("Case:"+i+"-"+(k+1)+", "+(j+1)+" Available is "+Avail+" Values are "+list.get(k)+","+ list.get(j));
bw.close();
// System.out.println("Done");
}
}
}
}
s.close();
}
}
the line printed in the file is
Case:9-7, 9 Available is 295 Values are 118,177
it is only the last one; open the file only once not for each element.Opening a file with a FileWriter by default empties out the file
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class FromFile {
public static void main(String args[]) throws Exception {
Scanner s = new Scanner(new File("D:/A1.txt"));
int testCaseCount = Integer.parseInt(s.next());
for (int i = 0; i < testCaseCount; i++){
int Avail = Integer.parseInt(s.next());
int size=Integer.parseInt(s.next());
ArrayList<Integer> list = new ArrayList<Integer>();
for(int j=0;j<size;j++)
{
list.add(s.nextInt());
}
FileWriter fw=new FileWriter(file.getAbsoluteFile());
BufferedWriter bw=new BufferedWriter(fw);
for(int k=0;k<list.size()-1;k++){
for(int j=k+1; j<=list.size()-1;j++){
int sum=list.get(k)+list.get(j);
if(sum==Avail){
System.out.println((k+1)+", "+(j+1));
File file=new File("D:/A2.txt");
if(!file.exists()){
file.createNewFile();
}
bw.write("Case:"+i+"-"+(k+1)+", "+(j+1)+" Available is "+Avail+" Values are "+list.get(k)+","+ list.get(j));
}
}
}
bw.close();
}
s.close();
}
}

Lucene Index Size

I have data like
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 22 23 24 25 26 28 30 36 37 39 40 41 46 48 49 51 52 53 54 55 56 58 60 66 67 68 71 72 74 77 78 85 89 90 91 108 109 110 116 117 118 120 121 123 137 138 145 146 147 148 154 157 159 162 165 166 168 175 179 181 198 201 203 212 215 216 223 231 233 254 266 270 274 323 327 329 331 347 352 355 360 363 370 411 415 434 438 442 444 445 462 470 471 477 486 495 499 503 524 525 536 542 595 603 608 636 644 646 647 670 692 694 698 762 763 798 809 822 970 981 987 992 1040 1057 1066 1079 1089 1111 1233 1244 1302 1315 1327 1333 1336 1387 1411 1412 1432 1458 1486 1498 1509 1572 1573 1574 1607 1625 1784 1808 1824 1909 1933 1938 1940 2011 2077 2081 2093 2286 2289 2395 2427 2467 2911 2944 2962 2975 3121 3170 3172 3197 3236 3267 3334 3699 3731 3905 3945 3982 3999 4008 4161 4234 4235 4296 4374 4457 4494 4526 4717 4720 4723 4820 4875 5352 5423 5472 5728 5799 5813 5821 6032 6230 6244 6278 6859 6868 7186 7280 7401 8734 8832 8885 8886 8925 9363 9510 9517 9592 9707 9802 10002 11097 11192 11715 11716 11836 11945 11996 12025 12482 12703 12706 12887 13122 13372 13482 13577 14150 14161 14169 14461 14626 16057 16268 16415 17183 17398 17440 17464 18097 18690 18731 18834 20576 20603 21558 21839 22202 26201 26497 26654 26658 26776 28088 28531 28551 28775 29122 29407.
This is one line of data many are there like that, stored in "training.txt". I am index it using following lucene indexing code
public class training
{
public static void main(String args[]) throws IOException, ParseException
{
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
// IndexWriter w = new IndexWriter(FSDirectory.open(new File("../search/index")), analyzer, true, new IndexWriter.MaxFieldLength(1000000));
IndexWriter w = new IndexWriter(FSDirectory.open(new File("index")), analyzer, true, new IndexWriter.MaxFieldLength(2139999999));
File file = new File("training.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
File file1 = new File("fileName.txt");
FileInputStream fis1 = null;
BufferedInputStream bis1 = null;
DataInputStream dis1 = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
fis1 = new FileInputStream(file1);
// Here BufferedInputStream is added for fast reading.
bis1 = new BufferedInputStream(fis1);
dis1 = new DataInputStream(bis1);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0 && dis1.available() != 0 ) {
String tempImg=dis1.readLine();
String temp=dis.readLine();
addDoc(w,tempImg,temp);
// System.out.println(temp);
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
w.optimize();
w.close();
}
private static void addDoc(IndexWriter w, String value1,String value2) throws IOException
{
Document doc = new Document();
doc.add(new Field("fileId", value1, Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("visualId", value2, Field.Store.YES, Field.Index.ANALYZED));
w.addDocument(doc);
}
}
There is another file "fileName.txt", for file name. My "training.txt" is of size 127.1 MB & index folder is getting created of size 217.2 MB. I believe it should get reduced.
My Search Code :
public class search
{
public static void main(String args[]) throws IOException, ParseException
{
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
String fname = "test.txt";
File file = new File(fname);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
Writer fos = null;
File outputFile = new File("outList.txt");
fos = new BufferedWriter(new FileWriter(outputFile));
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0)
{
Query q = new QueryParser(Version.LUCENE_CURRENT, "visualId", analyzer).parse(dis.readLine());
//3.search
int hitsPerPage = 200;
IndexSearcher searcher = new IndexSearcher(IndexReader.open(FSDirectory.open(new File("index")), true));
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
long startTime = System.currentTimeMillis();
searcher.search(q, collector);
long endTime = System.currentTimeMillis();
ScoreDoc[] hits = collector.topDocs().scoreDocs;
for(int i=0;i<hits.length;++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
String text = d.get("fileId");
fos.write(text);
fos.write("\n");
}
searcher.close();
}
// dispose all the resources after using them.
fis.close();
fos.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//out.close();
}
}
My "test.txt" is having content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 55 56 57 58 59 60 61 63 64 65 66 67 69 70 72 73 76 77 78 80 82 83 85 86 88 89 90 91 92 93 94 95 97 99 100 102 105 106 107 108 109 110 111 112 114 115 116 117 118 119 120 121 122 124 126 127 128 129 130 132 133 135 136 137 138 141 142 143 144 145 147 148 151 153 154 155 156 157 160 164 165 167 168 169 170 172 173 174 175 176 178 179 180 181 182 183 184 190 191 194 195 199 200 202 206 208 211 215 216 217 220 228 231 234 239 246 248 250 254 259 264 266 267 268 270 271 272 275 276 278 281 284 285 292 296 297 300 306 307 314 316 317 320 321 322 323 325 326 327 330 331 333 336 343 345 348 349 350 351 353 354 355 357 358 360 361 362 364 365 367 371 372 379 381 384 385 386 388 391 396 398 399 404 405 406 407 409 412 415 423 424 427 428 429 431 432 434 435 436 442 443 444 453 458 461 462 466 468 472 479 493 494 495 496 500 501 502 503 504 506 507 508 509 510 515 518 519 521 526 528 533 535 537 538 540 544 545 547 549 551 569 570 574 582 583 586 597 599 601 605 607 618 623 624 632 644 645 649 651 661 683 694 701 702 718 737 738 739 743 751 762 776 777 778 792 797 800 803 809 811 812 813 817 825 828 833 843 853 854 875 889 892 900 918 919 922 941 949 951 961 963 964 965 966 967 969 975 976 977 979 980 990 992 993 1000 1007 1008 1009 1029 1036 1045 1047 1051 1052 1053 1058 1059 1061 1062 1064 1065 1066 1070 1072 1075 1081 1082 1083 1086 1093 1094 1101 1114 1116 1117 1136 1143 1152 1154 1158 1159 1165 1172 1188 1194 1198 1212 1216 1218 1220 1227 1236 1245 1269 1272 1280 1283 1284 1285 1287 1293 1295 1296 1303 1305 1307 1327 1329 1332 1358 1373 1374 1375 1384 1385 1386 1397 1404 1415 1416 1436 1437 1478 1481 1482 1485 1487 1489 1501 1503 1505 1506 1508 1511 1517 1518 1520 1521 1522 1524 1525 1527 1529 1545 1555 1556 1564 1577 1579 1583 1599 1606 1610 1611 1612 1615 1620 1632 1636 1640 1648 1654 1706 1711 1721 1746 1750 1758 1792 1796 1802 1814 1820 1853 1869 1872 1897 1931 1932 1935 1946 1953 1982 2049 2082 2104 2107 2155 2211 2213 2216 2228 2253 2286 2329 2330 2332 2334 2377 2390 2399 2408 2427 2428 2433 2435 2440 2452 2475 2484 2498 2529 2559 2563 2626 2666 2675 2699 2754 2758 2765 2822 2847 2852 2882 2889 2893 2895 2898 2902 2906 2908 2925 2929 2932 2936 2939 2940 2971 2977 2980 2999 3022 3023 3024 3028 3086 3107 3134 3136 3140 3152 3156 3160 3174 3176 3182 3186 3192 3195 3197 3209 3216 3225 3242 3247 3249 3259 3279 3283 3303 3341 3349 3350 3352 3407 3429 3455 3462 3475 3476 3495 3515 3564 3581 3595 3637 3648 3653 3660 3681 3707 3735 3807 3817 3839 3850 3852 3856 3860 3878 3884 3889 3909 3916 3920 3980 3988 3997 4075 4120 4122 4123 4125 4152 4156 4157 4159 4191 4211 4244 4248 4307 4310 4434 4444 4446 4455 4462 4466 4503 4509 4516 4517 4525 4532 4551 4554 4559 4563 4564 4565 4573 4576 4581 4586 4634 4666 4669 4691 4730 4738 4748 4796 4817 4829 4832 4837 4846 4859 4896 4909 4919 4943 4962 5119 5132 5162 5237 5251 5275 5376 5387 5407 5441 5461 5559 5606 5608 5616 5692 5792 5797 5806 5837 5858 5947 6146 6245 6313 6320 6466 6632 6640 6648 6683 6759 6859 6987 6988 6989 6995 7003 7131 7171 7197 7223 7225 7280 7283 7299 7304 7320 7355 7357 7424 7451 7493 7586 7678 7690 7878 7997 8024 8096 8261 8275 8294 8465 8542 8556 8646 8667 8679 8685 8695 8707 8718 8724 8774 8786 8795 8808 8817 8819 8913 8932 8941 8996 9065 9069 9071 9085 9258 9321 9403 9408 9420 9456 9468 9481 9523 9528 9546 9559 9575 9584 9590 9592 9626 9648 9675 9727 9740 9742 9747 9776 9778 9836 9850 9909 10022 10046 10049 10056 10222 10288 10366 10385 10425 10429 10485 10546 10691 10744 10786 10912 10945 10958 10980 11043 11120 11205 11420 11451 11518 11551 11557 11568 11580 11633 11635 11652 11667 11728 11749 11760 11940 11963 11990 12225 12360 12367 12370 12375 12455 12468 12472 12476 12573 12632 12633 12731 12732 12745 12921 12922 12931 13303 13331 13332 13338 13364 13366 13386 13397 13510 13528 13548 13551 13575 13597 13654 13662 13676 13688 13689 13690 13693 13694 13720 13728 13743 13757 13901 13999 14007 14074 14190 14214 14245 14389 14452 14487 14496 14511 14538 14578 14689 14726 14756 14829 14887 15357 15395 15485 15710 15754 15824 16128 16161 16220 16323 16384 16678 16819 16825 16848 17075 17375 17391 17417 17511 17575 17841 18439 18734 18940 18961 19399 19896 19920 19945 20050 20276 20578 20960 20964 20967 20986 21009 21393 21513 21591 21670 21676 21839 21849 21898 21911 21960 22066 22072 22271 22354 22480 22759 23033 23070 23635 23990 24073 24287 24784 24824 24882 25395 25625 25668 25938 26002 26036 26054 26056 26085 26122 26153 26173 26321 26358 26385 26423 26450 26456 26739 26796 26823 26987 27196 27206 27214 27255 27773 27962 28209 28225 28260 28369 28405 28443 28568 28585 28637 28676 28724 28753 28770 28775 28877 28944 29026 29180 29221 29225 29240 29327 29333 29507
Thanks,
Ravi.
When you are adding Field.Store.YES to a Lucene field it is stored as well as indexed. The result would be that your index becomes larger than expected.

Categories