Dec 14 2017 04:19 AM
Hi,
I am using hortonworks hadoop , i need to integrate c# application with hadoop to access tables stored in hive.
I wrote a code for it but getting some errors regarding Microsoft odbc drivers
but i have installed hortonworks drivers. still getting microsoft odbc error
i need to access data from hadoop usning .net
is it possible
if possible then how?
code is attached below
plz. have a look
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Odbc;
using System.Threading.Tasks;
namespace Hadoopclient
{
class Program
{
static void Main(string[] args)
{
// @"dsn=Hadoop ODBC"
var connectionString = @"DRIVER={Hortonworks Hive ODBC Driver};
Host=192.168.221.128;
Port=10000;
Schema=default;
HiveServerType=2;
ApplySSPWithQueries=1;
AsyncExecPollInterval=100;
HS2AuthMech=2;
UserName=sandbox;";
var createTableCommandText = "CREATE TABLE Searches(searchTerm STRING, userid BIGINT,userIp STRING) " +
"COMMENT 'Stores all searches for data' " +
"PARTITIONED BY(searchTime DATE) " +
"STORED AS SEQUENCEFILE;";
using (var connection = new OdbcConnection(connectionString))
{
using (var command = new OdbcCommand(createTableCommandText, connection))
{
try
{
connection.Open();
// Create a table.
command.ExecuteNonQuery();
// Insert row of data.
command.CommandText = "INSERT INTO TABLE Searches PARTITION (searchTime = '2015-02-08') " +
"VALUES ('search term', 1, '127.0.0.1')";
command.ExecuteNonQuery();
// Reading data from Hadoop.
command.CommandText = "SELECT * FROM Searches";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
for (var i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine(reader[i]);
}
}
}
}
catch (OdbcException ex)
{
Console.WriteLine(ex.Message);
throw;
}
finally
{
// Drop table
command.CommandText = "DROP TABLE Searches";
command.ExecuteNonQuery();
}
}
}
}
}
}