Category: Allgemein

  • What is inside an AVRO file?

    In the Hadoop world, one may encounter AVRO files quite often. AVRO file can be used in more or less the same way as text files. They can be read, stored and written out. However, the AVRO files are binary files that make them difficult to read. However, help is there. I downloaded the avro-tools-1.8.2.jar from http://apache.cs.uu.nl/avro/avro-1.8.2/java/. See: avro-tools-1.8.2.jar.
    This opens the possibility to investigate what is inside an AVRO file. Let us assume, we have this AVRO file data.avro. Next commands provides an overview of the possibilities:

    java -jar avro-tools-1.8.2.jar
    

    Then, the avro file is written to a json file:

    java -jar avro-tools-1.8.2.jar tojson data.avro
    

    or the scheme is provides with:

    java -jar avro-tools-1.8.2.jar getschema data.avro
    
  • mounting drives

    Today, I played a bit with mounting external drives. Boy, that was more difficult than I thought.
    The situation was a follows. I had a linux desktop and I wanted to use network resources. I have a NAS, a samba drive and a few windows shares.
    A the end of the day, I only got the samba drive working. This was a directory on a raspberryPi. The directory is made available through samba. The terminal commands are:

    sudo mkdir /media/samba/startTom
    sudo chmod 777 -R /media/samba
    sudo mount -t cifs -o username=pi,password=bunvogni //192.168.2.15/share /media/samba/rasp
    

    One has to issue these commands every time the machine is booted.
    An alternative is to have it included in the /etc/fstab file that needs next line:

    //192.168.2.15/share /media/samba/rasp cifs rw,username=pi,password=bunvogni,uid=1000,gid=1000 0 0
    

    That is all I got. No access to windows shares, no access to my NAS. Hum, Windows is easier is this respect.

  • Sqoop

    Sqoop is used in an Hadoop environment to load data from a relational database upon an Hadoop platform. I have installed an Oracle 12.2 database. I tried to load data from that database to Hadoop. To do so, one needs to install a java jdbc driver to be able to access that database. For Oracle 12, two drivers are available: the ojdbc6.jar and the ojdbc7.jar. Both seem to work fine. I established this by putting both files on the /opt/cloudera/parcels/CDH/lib/sqoop/lib, which is used in a Cloudera environment to install the database drivers for sqoop. Subsequently, I verified the correct working with:

    sqoop list-tables \
    --connect jdbc:oracle:thin:@192.168.2.2:1521:orcl \
    --username c##hr \
    --password ine13258
    

    I also tried to use ojdbc8.jar, but this provided an error, saying “Unsupported major.minor version 52.0”.

    A similar exercise can be undertaken with mysql:

    sqoop list-tables \
    --connect jdbc:mysql://192.168.2.2/wordpress \
    --username tom \
    --password ine13258
    
  • Installing Cloudera Manager

    Cloudera Manager is a great tool to install a Big Data platform. I worked with this tool and let me share some findings. I realise that we have only a limited range of platforms that can be used to install the Cloudera Manager upon. I started with Ubuntu 16.04. In a first attempt, I used a newer version of Ubuntu but this was not supported.Therefore, I started with Ubuntu 16.04.
    During an attempt of installation, I was given a warning that ubuntu uses 127.0.1.1 as the host address. During that attempt, it was stated that 127.0.1.1 will lead to issues. In subsequent step, I encountered the promised issues. Therefore, in another attempt, I modified the /etc/hosts file:

    127.0.0.1	ubuntu
    127.0.0.1	localhost
    

    I also like to work with a fixed network address. This can be attained by including in /etc/network/interfaces these lines:

    auto ens33
    iface ens33 inet static
    address 192.168.2.25
    netmask 255.255.255.0
    gateway 192.168.2.254
    dns-nameservers 192.168.2.254
    

    In these lines, we see ens33 as the network interface. Previous versions of Ubuntu use eth0. The network interface can be known from the “ifconfig” statement.

    During the installation, use is made of a ssh connection. This connection will be used with a sudo command. However, during installing I discovered that the sudo command must be given without a password. So we have a series of steps to take. SSH can be installed with:

    sudo apt-get install openssh-server
    

    We then set up the possibility to connect to ssh via root. This can be done in file /etc/ssh/sshd_config. There we have a clause on PermitRootLogin. This must be set to “yes”:

    PermitRootLogin yes
    

    To allow a sudo without providing a password, we have to adjust the /etc/sudoers file. There we have to provide the permission for the root usage via tom:

    tom ALL =(ALL) NOPASSWD: ALL
    

    We are then ready to go. The Cloudera Manager can be downloaded via:

    wget http://archive.cloudera.com/cm5/installer/latest/cloudera-manager-installer.bin
    
    adjust the privs via:
    
    chmod u+x cloudera-manager-installer.bin
    
    start installing via:
    
    sudo ./cloudera-manager-installer.bin
    

    I noted that the web browser didn’t run. I got it running via sudo firefox -Profilemanager. I also had the privs with ~/.mozilla/firefox/ to make them readable.

    The installing was then straightforward. But above things have to settled.

  • SCD-2 and the Oracle Merge statement

    Yesterday I found a very nice script that allows processing records in a target table with a SCD-2 mechanism. The script is very elegant. It contains several logical steps that are knitted together in one SQL statement.

    Th mechanism of SCD2 is as follows. First, a comparison is made between a source table and a target table. One may encounter two situations: one whereby a situation/ record is provided that is completely new and a situation/ record that is in reality an update from a previous situation.

    When a new situation is provided, a new record must be inserted into the target table. When an update is provided, a record in the target table must be updated with an end date that indicates that the record can be seen as being a description of history. Moreover in case of an update a record must be inserted.

    Let us look at an example. We start with:

     

    Id stands for the identification of a situation, that can be updated. Naam is a field that can be updated. sk is a surrogate key that acts as a primary key. Startdate is the moment of an insert; enddate is the point in time until when the description is valid.

    Let us assume, we have new data that look like

    , we may derive that the final version will look like:

    We see tom and ine being unchanged. The Stella record is finished with an end date (21-02-2018). Paula and Tessa are inserted with a startdate and an end date that is set at 31-12-99.

     

    The script reads as:

    MERGE INTO mergedoel p
    USING 
     ( SELECT DECODE(s.scd_row_id,1,0,m.sk) as sk,
                       ID,
                       NAAM,
                       m.scd_row_id
       FROM    (SELECT dp.sk,
                       sp.ID,
                       sp.NAAM,
                       CASE
                         WHEN dp.id IS NULL
                           THEN 1
                         WHEN (dp.naam != sp.naam) 
                           THEN 2
                           ELSE 0 
                       END AS scd_row_id
                       FROM    mergebron sp
                LEFT JOIN mergedoel dp  
                ON (sp.id = dp.id and  dp.enddate > sysdate)
               ) m
       JOIN  scd_row_type s
       ON (s.scd_row_id <= m.scd_row_id)
     ) mp
    ON  (p.sk = mp.sk)
    when matched then
       update set P.enddate = sysdate
    when NOT matched then
       insert (P.ID,P.NAAM,P.SK, p.startdate , p.enddate)
       values (mp.id,mp.naam,S_MERGE.nextval,sysdate, to_date('2099-12-31 00:00:00','YYYY-MM-DD HH24:MI:SS'));
      commit;
    

    and the tables that we need are:

    CREATE TABLE SCD_ROW_TYPE
    (
    SCD_ROW_ID NUMBER NOT NULL,
    SCD_ROW_DESCRIPTION VARCHAR2(20 CHAR)
    );
    Insert into scd_row_type(scd_row_id,scd_row_description) values(1,'For Insert');
    Insert into scd_row_type(scd_row_id,scd_row_description) values(2,'For Update');
    
    
    
    CREATE TABLE "MERGEBRON" 
       (	"ID" NUMBER NOT NULL ENABLE, 
    	"NAAM" VARCHAR2(20 BYTE)
       );
    
    
    CREATE TABLE "MERGEDOEL" 
       (	"ID" NUMBER NOT NULL ENABLE, 
    	"NAAM" VARCHAR2(20 BYTE), 
    	"SK" NUMBER NOT NULL ENABLE, 
    	"STARTDATE" DATE, 
    	"ENDDATE" DATE
       ); 
    
    

    The logic of this procedure is that is a first step a comparison is made between the target table and the source table. If no match is found between the target table and source table (based on id), a record is inserted.
    If a match is found but the descriptions differ, two actions will be taken: records with end date are set at the system date and a new record are inserted.
     

  • Explain plan in Oracle

    I want to show the usage of an explain plan in Oracle. This will be shown in Oracle 12c. I will create a table and a sql statement. This sql statement will be analysed in a so-called explained plan that shows how Oracle will process the query. To do so, I first created a big table “Groot” in Oracle. Upon that I created an index “GrootIndex” with create index grootIndex on groot(n).
    The explain plan is created with (select * from C##HR.GROOT where n=600000; being the sql statement) :

    explain plan for
    select * from C##HR.GROOT
    where n=600000;
    

    The results are then shown with:

    select object_name, operation, options, object_type, optimizer, id 
    from (
    select object_name, operation, options, object_type, optimizer, id, dense_RANK() OVER (PARTITION BY OPERATION order by PLAN_ID DESC) rn
    from plan_table)A
    where rn=1
    order by id;

    The results are:

    OBJECT_NAME	OPERATION	    OPTIONS
    	        SELECT STATEMENT	
    GROOT	        TABLE ACCESS	    BY INDEX ROWID BATCHED
    GROOTINDEX	INDEX	            RANGE SCAN
    

    One might see that the sql uses the index. Based on the rowid =found herein, the sql accesses table Groot with the rid to retrieve the rows that were requested.

    Or – alternatively, you may use:

    select * from table(dbms_Xplan.display());

    that provides something like:

     

    A final remark: these results can also be seen in SQL developer. In the tool bar, one may notice small buttons that allow such direct analysis.

  • Installing on new versions of an OS

    I regularly install Oracle. In most cases, such installation is straightforward. Start the setup, provide a few data and click next, next, and next. Half an hour later, one has a fresh instance running. However, I recently got an error when installing Oracle 12c, version 2: installation was stopped as an error was encountered. This error puzzled me. I got some suggestions to circumvent this error but to no avail.
    Then I decided to look more closely at the build version of Windows10 that I was using. This was version 1709, a very new version that was released on 17 October 2017. The Oracle version that I tried to install came from March 2017, which is earlier. So it might be that Windows10 was modified after that date which prevented a smooth installation.
    As a second step, I decided to install Oracle on an old OS: I decided to use Windows 7, SP 1. This came from a date far before thhe release date of Oracle 12c vesrion 2 that I used. The installation went smoothly.
    After that I tried another (earlier) Windows 10 version. I used build 1703 that was released on 11 April 2017. This also worked smoothly.

    Problem solved.

    The take is not to always install products on the most recent version of an OS. Consider take a slightly outdated version.

  • Some spatial functions in Oracle

    Oracle has a wonderful spatial module. The spatial data are stored in a database as so-called SDO_GEOMETRY data type. The data in such field look like: (MDSYS.SDO_GEOMETRY(2001,90112,MDSYS.SDO_POINT_TYPE(121235,484926,NULL),NULL,NULL)). In this structure, we see the so-called SRID. This SRID is here 90112. This indicates how a geographical location can be translated in coordinates. In this case we need two coordinates to store one point. In this case, it is (121235,484926) that we need.
    These points can be shown in SQL Developer in the map viewer. We have a set of locations in Dutch speaking areas.

    This is already an interesting result. We are able to store and show the data in the database.

    For areas, we have a so-called polygon. This is a set of points that show a polygon on a map. The start of such polygon may look like: MDSYS.SDO_GEOMETRY(2003,28992,NULL,MDSYS.SDO_ELEM_INFO_ARRAY(1,1003,1),MDSYS.SDO_ORDINATE_ARRAY(100455.11,505440.83,100409.798,505244.882. This can also be shown in next diagramme.

    We can also make calculations on spatial data. Let me provide two examples. One example is a comparison where it is stated if one spatial object is located in another object. The question is whether objects in one table (TOPGRENZEN_GEM_ACTUEEL) is stored in objects in another table (COUNTRIES). Let me provide an example:

    SELECT b.name, a.gemeentena
    FROM "TOPGRENZEN_GEM_ACTUEEL" a, COUNTRIES b
    WHERE sdo_filter (a.geometry, b.geometry) = 'TRUE';
    

    In SQL Developer, this looks like:

    A final statement that might be useful. In some cases, we have polygons that are far too large: too many points. A solution is to simplify the polygon with:

    update topgrenzen_prov_actueel a
    set a.geosimpel = SDO_UTIL.SIMPLIFY(geometry, 1000, 100 );