Category: Uncategorized

  • Flume: sending data via stream

    It is possible to capture streaming data in HDFS files. A tool to do this is Flume. The idea is that we have 3 elements: sources that provide a stream, a channel that transports the stream and a sink where the stream ends in a file.
    This can already be seen if we look at the config file:

    agent1.sources = netcat-source
    agent1.sinks = hdfs-sink
    agent1.channels = memory-channel
    
    # Describe/configure the source
    agent1.sources.netcat-source.type = netcat
    agent1.sources.netcat-source.bind = 192.168.2.60
    agent1.sources.netcat-source.port = 12345
    agent1.sources.netcat-source.channels = memory-channel
    # Describe the sink
    agent1.sinks.hdfs-sink.type = hdfs
    agent1.sinks.hdfs-sink.hdfs.path = /loudacre/webtom/
    agent1.sinks.hdfs-sink.channel = memory-channel
    agent1.sinks.hdfs-sink.hdfs.fileType = DataStream
    # Use a channel which buffers events in memory
    agent1.channels.memory-channel.type = memory
    agent1.channels.memory-channel.capacity = 10000
    agent1.channels.memory-channel.transactionCapacity = 10000
    

    It all starts with a source that is a netcat stream that is sent to port 12345. The source is labelled “sources”. Then we have a sink that is labelled as “hdfs-sink”. Finally, we have the channel that is labelled “memory-channel”. This “memory-channel” is also mentioned in the sources as the channel that is used to send the stream into and it is mentioned in the sink as the faucet that delivers the data.
    On another machine, we start the netcat stream with:

    type "C:\Program Files (x86)\netcat\readme.txt" |   "C:\Program Files (x86)\netcat\nc.exe"  192.168.2.60 12345
    

    This sends the content of a file as stream to a netcat proces that sends the stream to host 192.168.2.60 with port 12345. Exactly these sources were mentioned in the config file as the source of the stream.
    The flume process is started with

    flume-ng agent --conf /etc/flume-ng/conf --conf-file home/training/training_materials/dev1/exercises/flume/solution/bonus_netcat_tom.conf --name agent1 -Dflume.root.logger=INFO,console
    

    We may see the data being received in files of HDFS:

  • Partitioned Table in Hive

    It is possible to partition the tables in Hive. Remember the data are stored in files. So we expect the files to be partitioned. This is accomplished by a split of the files over different directories. One directory serves one partition, a second another partition etc.
    Let us take the example of 7 records that are split over 5 partitions according to their number. We have:

     

    We see in the directory that corresponds to the file location, that 5 directories are present, each directory corresponding to one partition. 

    Let us switch to partition 2 that stores (2, ine), (2,ellen) and (2, henkjan). When the directory with the data on partition 2 is investigated, we see a file with the data within that directory:

    With the data, the nummer that is used to create the partitions is omitted. The nummer is stored in the directory name.

    To create this table, we use this command:

    CREATE EXTERNAL TABLE fam_by_nummer(
    naam STRING)
    PARTITIONED BY (nummer INT)
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    LOCATION '/loudacre/fam_by_nummer';
    

    To load the table, we take a detour.
    First, the data are loaded to hdfs:

    sqoop import \
    --connect "jdbc:oracle:thin:@(description=(address=(protocol=tcp)(host=192.168.2.2)(port=1521))(connect_data=(service_name=orcl)))" \
    --username scott --password bunvegni \
    --table fam \
    --columns "NUMMER, NAAM" \
    --m 1 \
    --target-dir /loudacre/fam_by_nummer_temp1;
    

    Then a table is created:

    CREATE EXTERNAL TABLE fam_by_nummer_temp(
    nummer INT, naam STRING)
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    LOCATION '/loudacre/fam_by_nummer_temp1';
    

    And the data are loaded from this table into the partitioned table:

    insert into table fam_by_nummer
    partition (nummer)
    select naam, nummer from fam_by_nummer_temp;
    

    The exact sequence of the field wasn’t directly clear to me, but it could be derived from the Hue interface.
    Adding data to an existing partition is easy.
    First create a file and then upload it to the correct directory:

    hadoop dfs -put /tmp/extra /loudacre/fam_by_nummer/nummer=2
    

    Adding a new partition requires action on the Hive side:

    ALTER TABLE fam_by_nummer
    ADD PARTITION (nummer=6);
    

    This must be added by the creation of the directory in the proper environment:

    hadoop dfs -mkdir /loudacre/fam_by_nummer/nummer=6
    

    And the storage of a file with data in that directory:

    hadoop dfs -put /tmp/extra /loudacre/fam_by_nummer/nummer=6
    

     

  • Manipulating Avro

    Avro files are binary files that contain data and the description of the files. Thereby it is a very interesting file format. One may send this file to any application that is able to read Avro files. Just as an example: one may write the file is (say) PHP and send it to (say) Java. In previous posts I showed how such file could be written and read by PHP. See a post here.
    In this note I show one may use a jar file to create and to read an avro file. The jar file is avro-tools-1.8.1.jar. This jar file enables us to create an avro file from a schema definition and a json file. The schema file looks like:

    {
      "type" : "record",
      "name" : "twitter_schema",
      "namespace" : "com.miguno.avro",
      "fields" : [ {
        "name" : "username",
        "type" : "string",
        "doc"  : "Name of the user account on Twitter.com"
      }, {
        "name" : "tweet",
        "type" : "string",
        "doc"  : "The content of the user's Twitter message"
      }, {
        "name" : "timestamp",
        "type" : "long",
        "doc"  : "Unix epoch time in seconds"
      } ],
      "doc:" : "A basic schema for storing Twitter messages"
    }
    

    wheras the JSON data file looks like:

     {"username":"miguno","tweet":"Rock: Nerf paper, scissors is fine.","timestamp":1366150681}
    {"username":"BlizzardCS","tweet":"Works as intended.  Terran is IMBA.","timestamp":1366154481}
    

    This can then be combined in an avro files with:

    java -jar "C:/Program Files/Java/avro-tools-1.8.1.jar" fromjson --schema-file D:\Users\tmaanen\CloudStation\java\avro2\user.avsc D:\Users\tmaanen\CloudStation\java\avro2\user.json > D:\Users\tmaanen\CloudStation\java\avro2\user.avro
    

    We now have an avro file. This is a binary file. This file can translated to a json file with:

    java -jar "C:/Program Files/Java/avro-tools-1.8.1.jar" tojson D:\Users\tmaanen\CloudStation\java\avro2\user.avro > D:\Users\tmaanen\CloudStation\java\avro2\user2.json
    

    Likewise the scheme can be derived with:

    java -jar "C:/Program Files/Java/avro-tools-1.8.1.jar" getschema D:\Users\tmaanen\CloudStation\java\avro2\part-m-00000.avro > D:\Users\tmaanen\CloudStation\java\avro2\user2.avsc
    

    For me, this utility is very handy to investigate the result from a sqoop command. Roughly stated, such sqoop command may import the contents of a database table to an HDFS platform. Such command may look like:

    sqoop import \
    --connect "jdbc:oracle:thin:@(description=(address=(protocol=tcp)(host=192.168.2.2)(port=1521))(connect_data=(service_name=orcl)))" \
    --username scott --password binvegni \
    --table fam \
    --columns "NUMMER, NAAM" \
    --m 1 \
    --target-dir /loudacre/fam_avro \
    --null-non-string '\\N' \
    --as-avrodatafile
    

    The output from such command might be an avro file that might be called part-m-00000.avro. The question is: how do I know that this file contains the correct data? I could then import the avro file to Windows and translate it with:

    java -jar "C:/Program Files/Java/avro-tools-1.8.1.jar" tojson D:\Users\tmaanen\CloudStation\java\avro2\part-m-00000.avro > D:\Users\tmaanen\CloudStation\java\avro2\part-m-00000.json
    

    This provides me the confirmation that the avro file is correct.

  • Parquet format

    As we know, we may store table definitions in the metastore. These table definitions then refer to a location where the data are stored. The format of the data might be an ordinary text file or it might be an avro file. Another possibility is a parquet file. This parquet format is an example of a packed/ zipped format.
    To create such table is rather straightforward. First, we transfer a table to a parquet file on HDFS:

    sqoop import \
    --connect "jdbc:oracle:thin:@(description=(address=(protocol=tcp)(host=192.168.2.2)(port=1521))(connect_data=(service_name=orcl)))" \
    --username scott --password binvegni \
    --table fam \
    --columns "NUMMER, NAAM" \
    --m 1 \
    --target-dir /loudacre/fam_parquet \
    --as-parquetfile;
    

    This results in a file that can be found in directory /loudacre/fam_parquet. For some reason, the file is called 5fe8fcaa-6095-40ec-b499-d73d6d971b6f.parquet. From Impala, we may then define the table with:

    CREATE EXTERNAL TABLE fam_parquet
    LIKE PARQUET '/loudacre/fam_parquet/5fe8fcaa-6095-40ec-b499-d73d6d971b6f.parquet'
    STORED AS PARQUET
    LOCATION '/loudacre/fam_parquet/';
    
  • Avro format

    In Hive, we see a situation where a table definition is stored in a metastore. This table definition is linked to a directory where the data are stored. It is possible to use different formats here. One may think of a text format. But other formats are possible too. One example is the avro format. This format can be characterised by a combination of file definition and the actual data.
    Let us see how these data can be imported.
    A possibility is to use the scoop command to import data in avro format:

    sqoop import \
    --connect "jdbc:oracle:thin:@(description=(address=(protocol=tcp)(host=192.168.2.2)(port=1521))(connect_data=(service_name=orcl)))" \
    --username scott --password binvegni \
    --table fam \
    --columns "NUMMER, NAAM" \
    --m 1 \
    --target-dir /loudacre/fam_avro \
    --null-non-string '\\N' \
    --as-avrodatafile
    

    The result is composed of two parts:
    1: The directory where the table content is stored. This can be found on HDFS in directory in /loudacre/fam_avro. This can be verified with “hadoop dfs -ls /loudacre/fam_avro”. This provides an overview of the files where the data are stored. When this is opened, one sees:

    So, we have the data. The data definition is stored in the same directory as the directory where the scoop command was launched. To get this on HDFS platform, we issue ” hadoop dfs -put sqoop_import_fam.avsc /loudacre/”. This stored the definition on HDFS level. This can be investigated:

    If we have the data (in directory /loudacre/fam_avro) and the definition (in file /loudacre/sqoop_import_fam.avsc), we may proceed by a definition in hive. Then hive is started by: “beeline -u jdbc:hive2://192.168.2.60:10000 -n training -p training”, followed by:

    CREATE EXTERNAL TABLE fam_avro
    STORED AS AVRO
    LOCATION '/loudacre/fam_avro'
    TBLPROPERTIES ('avro.schema.url'=
    'hdfs:/loudacre/sqoop_import_fam.avsc');
    

    A check with “select * from fam_avro” showed the data as expected.

     

  • Create a Hive table – 3 ways

    In this little note, I want to show three different ways to create a table on Hive. The first one starts with a file on HDFS that is available and we create a table upon this file. This table is defined as an external file that is exposed as a table.

    The code to be executed in Hive is:

    CREATE EXTERNAL TABLE fam
    (nummer SMALLINT,
    name STRING)
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    LOCATION '/user/hdfs'
    

    And we may say the content of the table from, say SQL Developer:

     

    From the clause “LOCATION ‘/user/hdfs’”, we know that files that will deliver the data will be stored in directory /user/hdfs. If we want to add data to the table, we only have to copy files to that location. Let us start with two files:

    We then copy an additional file to this environment ( hadoop dfs -put /tmp/fam3 /user/hdfs ). This shows:

     

    When we then start the Hive environment: (  beeline -u jdbc:hive2://192.168.2.60:10000 -n training -p training ), we may issue the select command: select * from fam; . This provides the content of the fam table. One may see the content of the newly added fam3 table. This shows clearly how easy it is to add data to a Hadoop platform. The only thing we have to do is to add files to the correct environment. These files will be automatically recognised as part of a table.

    An alternative to adding data to a table is to move data within the HDFS environment. Suppose we have a file on HDFS that contains data that should be added to the table. First, we start Hive:

    beeline -u jdbc:hive2://192.168.2.60:10000 -n training -p training
    

    I then moved data into the table:

    LOAD DATA INPATH '/user/training/fam4' INTO TABLE fam;
    

    I then saw the file being added to the data environment:

     

     

    Another way is a direct import from, say Oracle, which creates on the fly a file on HDFS and which adds the table definition to Hive. The code is:

    sqoop import \
    --connect "jdbc:oracle:thin:@(description=(address=(protocol=tcp)(host=192.168.2.2)(port=1521))(connect_data=(service_name=orcl)))" \
    --username scott --password binvegni \
    --table fam \
    --columns "NUMMER, NAAM" \
    --m 1 \
    --fields-terminated-by ',' \
    --table device \
    --hive-import
    

    Which creates a file on hdfs:

    and a table in Hive that can be seen from, say Excel:

    A slightly different script is this. Load data from Oracle into a file on hdfs:

    sqoop import \
    --connect "jdbc:oracle:thin:@(description=(address=(protocol=tcp)(host=192.168.2.2)(port=1521))(connect_data=(service_name=orcl)))" \
    --table COUNTRIES \
    --columns "NAME, LOCAL"  \
    --num-mappers 1 \
    --compression-codec=snappy \
    --username ANONYMOUS \
    --password ine13758 \
    --warehouse-dir=/user/hive/warehouse 
    

    with a subsequent code in hive:

    CREATE EXTERNAL TABLE COUNTRIES ( 
        NAME STRING, 
        LOCAL STRING) 
    ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' 
    LOCATION '/user/hive/warehouse/COUNTRIES';
    

     

     

    It is good to realise that we must undertake two actions:

    1:An action to store the data (in a file, that is stored in a HDFS directory.

    2: An action to create table definitions that are stored in a metastore.

     

    A third manner is to use the HCatalog:

    hcat -e "CREATE TABLE prut \
    (id INT, company STRING) \
    ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' \
    LOCATION '/user/prut'"
    

     

  • Oracle ODI

    The successor to OWB is the Oracle Data Integrator. This tool has more functionalities than OWB. Next to that, it has an interface that more or less steers the user through a series of steps.
    The idea is that one starts with a technical view where the file locations, databases and schemes are declared. Once that is done, one creates a set of logical names that provide the user with an understandable name that can be used to create a so-called mapping. If the mapping is created, it can be run to see if it works.
    Let me first introduce the technical aspects. This can be done in a widget that is called “topology” where the technical architecture can be defined. One may see a set of technologies that allow to specify a technical object. As example, one may look at the technical indication of a file. The object file_tom provides a manner how a file is accessed. There, the driver is provided. In the object beneath, one may indicate the directory where the files are stored.

    untitled

     

    Similarly, the technology “Oracle” has an object ” Oratom”  where the JDBC driver is provided. Beneath, another object is created where the scheme is provided.

     

    Let us switch to the logical design. We have the possibility to define logical handles that link the technical concepts to logical handles. We have “File_Tom”  that is logically linked to the File_Tom that we have defined in the physical architecture. The same holds for “ODI_Tom”  that is the logical handle for the physical defined scheme.

    untitled

    A crucial link between the logical handles and the technical objects is thru the idea of ” context”. This provides a scheme where logical handles are linked to physical entities.

    untitled

     

    This is almost genial is its triviality.  If we have more contexts, we may create a development, test and production contexts. Promotion into production is then just a change in context.

    We are now in a situation where technical concepts like a scheme or a file location are translated into logical concepts. In a subsequent step actual files and tables can be accessed. This is done in the designer tab, under the models. We can reverse engineer the file definitions and table definitions from the physical environment. We then get something like this:

    untitled

    Here, we have the logical components (Names as file definition and names as table definition) that are added to the models. The file definition “names” is added to “File_Tom”  that is derived from the logical component that we created. Likewise the table definition for “Names”  is added to FlatFile2Table that is linked to the logical definition for the scheme that we created.

    The logical definitions for the file and table can be used in a mapping. We now have a logical definition for a source and a target. This can then be used in the mapping. In this example the mapping between source and target is almost trivial:

    untitled

    The mapping can be run in several ways. I believe there are about 4 ways to run a mapping. When a mapping is run, one has to fill out the LKM (Loading Knowledge Module) .

    untitled

     

    This can then be run and the content of the file is loaded into the table.

     

     

     

     

     

     

     

  • Docker container

    Only this weekend I downloaded a Docker package from https://docs.docker.com/docker-for-windows. This package allows you to run very small light weight containers on your server than act as components to perform a certain task. In a way, it looks like a virtual machine. It has no direct connect connection to the host machine and it runs in isolation.
    Once it is installed, one only notices a small icon in the task bar that shows its presence.
    untitled
    To turn it in something useful, one must create a directory with a so-called “Dockerfile” in it. In that dockerfile one may store a series of commands that indicate what the Dockerfile container is supposed to do. An example of such file can be found here On my machine, the directory looks like this:
    untitled
    The container is then created with:

    docker build -t mysql .
    

    The container can then be started from the command line with:

    docker run --name mysql -e MYSQL_ROOT_PASSWORD=bonvegni -d mysql:8
    

    To get access to the container, one uses this command:

    docker exec -it mysql bash
    

    untitled
    This is as far as I got. I have a MySql database running. I can access this database from within the container. That look normal. But the challenge is how to communicate with this database. Something to figure out.

  • Putting a file on HDFS

    Putting a file on HDFS is relatively easy. There are a few steps to take. Let us assume the file is on a linux system. The first step is to copy the file to an area where it can be stored with the hdfs user as its owner. On my system, I have /tmp that has 777 privs: here files with every owner can be stored. So I copy my file to that area:

    cp /root/uit /tmp
    

    Now, I have got the file in an area where the user as I use in my HDFS platform can be set as owner to the file.
    The user I have on my HDFS plaform is “hdfs”. I set the file with “hdfs” as owner with:

    chown hdfs:hdfs /tmp/uit
    

    Now the file uit has hdfs as owner. I can now put it on HDFS with:

    sudo -u hdfs hadoop fs -put /tmp/uit /Chapter5/uit
    
  • Estimating with Python

    It is relatively easy to do an estimate with a Python script. This is due to the fact that Python works with matrices and such matrices can be used as an input in a estimation procedure. I created an example where a dataset is retrieve from Oracle. Then the dataset is translated into a matrix. Subsequently some columns from that matrix are taken as dependent and independent variables, which are subsequently used as input to an OLS estimation procedure.
    The code:

    import cx_Oracle
    import numpy as np
    import statsmodels.api as sm
    con = cx_Oracle.connect('scott/bunvegni@van-maanen.com/orcl')
    cur = con.cursor()
    cur.execute('select * from departments where manager_id is not null')
    res = cur.fetchall()
    print res
    con.close()
    A = np.array(res)
    y= A[:,0].astype(np.float)
    x= A[:,[2,3]].astype(np.float)
    lmRegModel = sm.OLS(y,x)
    result = lmRegModel.fit()
    result.summary()
    

    I use the numpy library to translate the data as retrieved from Oracle into a matrix. I am then able to retrieve one column as the independent variable and 2 columns as independent variables. I explicitly cast them into floats. I am then able to start the estimation process.