Save log data in HashMap

-1

What I want to do is save each request of an acceslog in a hashmap , but the saving is not being done. Please someone see the fault ???

JAVA:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;


public class BuscarPatrones {

    static String inici = "GET / HTTP/1.1";
    static String fi = "HelloWorldExample";
/**
* @param args
*/
public static void main(String[] args) {

    File f = new File( "C:/Users/Marc/Desktop/access-logs.txt" );
    BufferedReader entrada;
     HashMap<String, ArrayList<String>> Patrones = 
                new HashMap<String, ArrayList<String>>();
    ArrayList<String>patron=new ArrayList<String>();
    ArrayList<String>p=new ArrayList<String>();
    String linea="";
    int cont=0;

    try {
        entrada = new BufferedReader( new FileReader( f ) );

        while(entrada.ready()){
            linea = entrada.readLine();
            linea = linea.substring(linea.indexOf("+0100]") + 8);


            if(!linea.contains(inici)){
                patron.add(linea);
            }else{

                cont++;
                crearpatrones(Patrones,patron,cont);    
                patron.clear();
            }


}}catch (IOException e) {
e.printStackTrace();
}


    /*
     * --------------------Resultado general del programa----------------
     * 
     * for (Entry<String, ArrayList<String>> entry : Patrones.entrySet()) {
          String key = entry.getKey();
          ArrayList<String> value = entry.getValue();
          System.out.println(key);
          System.out.println(value);

    }*/



}

private static void crearpatrones(HashMap<String, ArrayList<String>> patrones, ArrayList<String> patron2,int contador) {
    // TODO Auto-generated method stub

    String petición="Petición";
    petición= petición+contador;

    //--------------Aqui accede con la info correcta---------------------

    //System.out.println(petición);
    //System.out.println(patron2);

    patrones.put(petición,patron2);

return; 

}
}

ACCESLOG:

https://mega.nz/#!mU9l3QaS!JZw7wQEqagjATFrfYgy8xkcnVzmV2nDhyt7o_6jqyaw

127.0.0.1 - - [17/Dec/2016:19:30:30 +0100] "GET / HTTP/1.1" 200 11485 
127.0.0.1 - - [17/Dec/2016:19:30:31 +0100] "GET /favicon.1 HTTP/1.1" 200 21630 
127.0.0.1 - - [17/Dec/2016:19:30:32 +0100] "GET /tomcat.css HTTP/1.1" 200 5926 
127.0.0.1 - - [17/Dec/2016:19:30:33 +0100] "GET /tomcat.png HTTP/1.1" 200 5103 
127.0.0.1 - - [17/Dec/2016:19:30:33 +0100] "GET /asf-logo.png HTTP/1.1" 200 17811 
127.0.0.1 - - [17/Dec/2016:19:30:33 +0100] "GET /bg-upper.png HTTP/1.1" 200 3103 
127.0.0.1 - - [17/Dec/2016:19:30:33 +0100] "GET /bg-nav.png HTTP/1.1" 200 1401 
127.0.0.1 - - [17/Dec/2016:19:30:33 +0100] "GET /bg-nav.png HTTP/1.1" 200 1401 
127.0.0.1 - - [17/Dec/2016:19:30:33 +0100] "-" 400 - 
127.0.0.1 - - [17/Dec/2016:19:30:37 +0100] "GET /bg-middle.png HTTP/1.1" 200 1918 
127.0.0.1 - - [17/Dec/2016:19:30:37 +0100] "GET /bg-button.png HTTP/1.1" 200 713 
127.0.0.1 - - [17/Dec/2016:19:31:03 +0100] "GET /examples HTTP/1.1" 302 - 
127.0.0.1 - - [17/Dec/2016:19:31:03 +0100] "GET /examples/ HTTP/1.1" 200 1156 
127.0.0.1 - - [17/Dec/2016:19:31:05 +0100] "GET /examples/servlets HTTP/1.1" 302 - 
127.0.0.1 - - [17/Dec/2016:19:31:06 +0100] "GET /examples/servlets/ HTTP/1.1" 200 6596 
127.0.0.1 - - [17/Dec/2016:19:31:09 +0100] "GET /examples/servlets/images/return.gif HTTP/1.1" 200 1231 
127.0.0.1 - - [17/Dec/2016:19:31:10 +0100] "GET /examples/servlets/images/code.gif HTTP/1.1" 200 292 
127.0.0.1 - - [17/Dec/2016:19:31:10 +0100] "GET /examples/servlets/images/execute.gif HTTP/1.1" 200 1242 
127.0.0.1 - - [17/Dec/2016:19:31:18 +0100] "GET /examples/servlets/servlet/HelloWorldExample HTTP/1.1" 200 400 
127.0.0.1 - - [17/Dec/2016:19:31:59 +0100] "GET / HTTP/1.1" 200 11458 
127.0.0.1 - - [17/Dec/2016:19:31:59 +0100] "GET /tomcat.css HTTP/1.1" 200 5926 
127.0.0.1 - - [17/Dec/2016:19:32:00 +0100] "GET /favicon.2 HTTP/1.1" 200 21630 
127.0.0.1 - - [17/Dec/2016:19:32:00 +0100] "GET /tomcat.png HTTP/1.1" 200 5103 
127.0.0.1 - - [17/Dec/2016:19:32:00 +0100] "GET /asf-logo.png HTTP/1.1" 200 17811 
127.0.0.1 - - [17/Dec/2016:19:32:00 +0100] "GET /bg-nav.png HTTP/1.1" 200 1401 
127.0.0.1 - - [17/Dec/2016:19:32:00 +0100] "v.png HTTP/1.1 " 400 - 
127.0.0.1 - - [17/Dec/2016:19:32:02 +0100] "GET /bg-upper.png HTTP/1.1" 200 3103 
127.0.0.1 - - [17/Dec/2016:19:32:03 +0100] "GET /bg-middle.png HTTP/1.1" 200 1918 
127.0.0.1 - - [17/Dec/2016:19:32:03 +0100] "GET /bg-button.png HTTP/1.1" 200 713 
127.0.0.1 - - [17/Dec/2016:19:32:08 +0100] "GET /examples HTTP/1.1" 302 - 
127.0.0.1 - - [17/Dec/2016:19:32:08 +0100] "GET /examples/ HTTP/1.1" 200 1156 
127.0.0.1 - - [17/Dec/2016:19:32:11 +0100] "GET /examples/servlets HTTP/1.1" 302 - 
127.0.0.1 - - [17/Dec/2016:19:32:13 +0100] "GET /examples/servlets/ HTTP/1.1" 200 6596 
127.0.0.1 - - [17/Dec/2016:19:32:18 +0100] "GET /examples/servlets/images/return.gif HTTP/1.1" 200 1231 
127.0.0.1 - - [17/Dec/2016:19:32:19 +0100] "GET /examples/servlets/images/code.gif HTTP/1.1" 200 292 
127.0.0.1 - - [17/Dec/2016:19:32:19 +0100] "GET /examples/servlets/images/execute.gif HTTP/1.1" 200 1242 
127.0.0.1 - - [17/Dec/2016:19:32:21 +0100] "GET /examples/servlets/servlet/HelloWorldExample HTTP/1.1" 200 400 
127.0.0.1 - - [17/Dec/2016:19:33:04 +0100] "GET / HTTP/1.1" 200 11485 
127.0.0.1 - - [17/Dec/2016:19:33:04 +0100] "GET /favicon.3 HTTP/1.1" 200 21630 
127.0.0.1 - - [17/Dec/2016:19:33:05 +0100] "GET /tomcat.css HTTP/1.1" 200 5926 
127.0.0.1 - - [17/Dec/2016:19:33:06 +0100] "GET /tomcat.png HTTP/1.1" 200 5103 
127.0.0.1 - - [17/Dec/2016:19:33:06 +0100] "GET /bg-upper.png HTTP/1.1" 200 3103 
127.0.0.1 - - [17/Dec/2016:19:33:06 +0100] "GET /asf-logo.png HTTP/1.1" 200 17811 
127.0.0.1 - - [17/Dec/2016:19:33:06 +0100] "GET /bg-nav.png HTTP/1.1" 200 1401 
127.0.0.1 - - [17/Dec/2016:19:33:06 +0100] "GET /bg-nav.png HTTP/1.1" 200 1401 
127.0.0.1 - - [17/Dec/2016:19:33:06 +0100] "-" 400 - 
127.0.0.1 - - [17/Dec/2016:19:33:08 +0100] "GET /bg-middle.png HTTP/1.1" 200 1918 
127.0.0.1 - - [17/Dec/2016:19:33:08 +0100] "GET /bg-button.png HTTP/1.1" 200 713 
127.0.0.1 - - [17/Dec/2016:19:33:15 +0100] "GET /examples HTTP/1.1" 302 - 
127.0.0.1 - - [17/Dec/2016:19:33:15 +0100] "GET /examples/ HTTP/1.1" 200 1156 
127.0.0.1 - - [17/Dec/2016:19:33:19 +0100] "GET /examples/servlets HTTP/1.1" 302 - 
127.0.0.1 - - [17/Dec/2016:19:33:20 +0100] "GET /examples/servlets/ HTTP/1.1" 200 6596 
127.0.0.1 - - [17/Dec/2016:19:33:21 +0100] "GET /examples/servlets/images/code.gif HTTP/1.1" 200 292 
127.0.0.1 - - [17/Dec/2016:19:33:22 +0100] "GET /examples/servlets/images/return.gif HTTP/1.1" 200 1231 
127.0.0.1 - - [17/Dec/2016:19:33:22 +0100] "GET /examples/servlets/images/execute.gif HTTP/1.1" 200 1242 
127.0.0.1 - - [17/Dec/2016:19:33:25 +0100] "GET /examples/servlets/servlet/HelloWorldExample HTTP/1.1" 200 400 
    
asked by Marc Blaje 02.01.2017 в 18:10
source

3 answers

1

You never clarified your problem very well. But there is a detail that will surely cause you problems, and it is with this sentence:

patron.clear();

In the previous sentence, you execute the crearpatrones method, to which you pass a reference to patron and that you use to assign it to HashMap .

Well, when you do the patron.clear() , you are working with the same object that you just assigned the HashMap . Both have a reference to the same object. They are not different copies. So, unintentionally, you are deleting the values within the ArrayList that you just assigned to HashMap .

One way to fix the problem is to create a new list instead of executing the clear method. This way you make sure you work with a different list than the one you just added to HashMap :

patron = new ArrayList<String>();
    
answered by 03.01.2017 в 03:23
1

If I understand you, your idea is to save HashMaps of requests in the AccessLog grouped by root access (your constant started ).

As sstan already explained, Java passes references to methods, not copies. So what your code does is save the same HashMap with each block of requests and empty it again with the next block ( patron.clear() ).

What you could do is instead of:

while(entrada.ready()){
    linea = entrada.readLine();
    linea = linea.substring(linea.indexOf("+0100]") + 8);


    if(!linea.contains(inici)){
        patron.add(linea);
    }else{

        cont++;
        crearpatrones(Patrones,patron,cont);    
        patron.clear();
    }
}

... is to use:

while(entrada.ready()){
    linea = entrada.readLine();
    linea = linea.substring(linea.indexOf("+0100]") + 8);


    if(linea.contains(inici)){
        // si hay un bloque anterior, guardalo
        if (patron.size()>0) crearpatrones(Patrones, patron, ++cont);
        patron = new ArrayList<String>();
    }else{
        patron.add(linea);
    }
}
// Fin de log, guarda el ultimo bloque
if (patron.size()>0) crearpatrones(Patrones, patron, ++cont);

On top of that I recommend you follow some conventions to make it easier for others to get their bearings in your code:

  • Constants are written in capital letters, words, separated by _ .% static String INICI = "..."; .

  • Classes start with capital letters, variables and methods with lowercase letters.
    HashMap<String, ArrayList<String>> patrones =... .

  • Words in classes, variables and methods, are separated by camelCasing .% public void crearPatrones(...){... .

  • answered by 03.01.2017 в 19:46
    0

    If the problem is in the order in which they are displayed in the loop, this is because the HashMap does not iterate in the order it is stored. If you want to iterate in the order you stored, use LinkedHashMap

    An example of the above would be:

    HashMap<Integer,String> map1 = new HashMap<>();
    map1.put(1,"uno");
    map1.put(2,"dos");
    map1.put(3,"tres");
    // La salida no es 1, 2, 3
    for(Entry<Integer,String> entry : map1.entrySet()){
        System.out.println(entry.getKey());
    }
    

    With LinkedHashMap:

    HashMap<Integer,String> map2 = new HashMap<>();
    map2.put(1,"uno");
    map2.put(2,"dos");
    map2.put(3,"tres");
    // La salida es 1, 2, 3
    for(Entry<Integer,String> entry : map2.entrySet()){
        System.out.println(entry.getKey());
    }
    
        
    answered by 06.01.2017 в 00:03