Exemplo n.º 1
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            ErrorMessage.InnerText = "";
            if (hdnGroup.Value == "Word")
            {
                string path = string.Format("{0}\\..\\Data\\AdventureWorks\\AdventureWorks_Person_Contact.csv", Request.PhysicalPath.ToLower().Split(new string[] { "\\c# hbase samples" }, StringSplitOptions.None));
                try
                {
                    //Create a new document
                    WordDocument document = new WordDocument();

                    //Adding new table to the document
                    WTable doctable = new WTable(document);

                    //Adding a new section to the document.
                    WSection section = document.AddSection() as WSection;

                    //Set Margin of the section
                    section.PageSetup.Margins.All = 72;

                    //Set page size of the section
                    section.PageSetup.PageSize = new SizeF(800, 792);

                    //Create Paragraph styles
                    WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle;
                    style.CharacterFormat.FontName = "Calibri";
                    style.CharacterFormat.FontSize = 11f;

                    //Create a character format for declaring font color and style for the text inside the cell
                    WCharacterFormat charFormat = new WCharacterFormat(document);
                    charFormat.TextColor = System.Drawing.Color.White;
                    charFormat.Bold      = true;

                    #region creating connection
                    HBaseConnection con = new HBaseConnection("localhost", 10003);
                    con.Open();

                    #endregion creating connection

                    #region parsing csv input file

                    csv csvObj = new csv();
                    object[,] cells;
                    cells = null;

                    cells = csvObj.Table(path, false, ',');

                    #endregion parsing csv input file

                    #region creating table
                    String        tableName      = "AdventureWorks_Person_Contact";
                    List <string> columnFamilies = new List <string>();
                    columnFamilies.Add("info");
                    columnFamilies.Add("contact");
                    columnFamilies.Add("others");
                    if (!HBaseOperation.IsTableExists(tableName, con))
                    {
                        if (columnFamilies.Count > 0)
                        {
                            HBaseOperation.CreateTable(tableName, columnFamilies, con);
                        }
                        else
                        {
                            throw new HBaseException("ERROR: Table must have at least one column family");
                        }
                    }
                    # endregion

                    #region Inserting Values
                    string[] column = new string[] { "CONTACTID", "FULLNAME", "AGE", "EMAILID", "PHONE", "MODIFIEDDATE" };
                    Dictionary <string, IList <HMutation> > rowCollection = new Dictionary <string, IList <HMutation> >();
                    string rowKey;
                    for (int i = 0; i < cells.GetLength(0); i++)
                    {
                        List <HMutation> mutations = new List <HMutation>();
                        rowKey = cells[i, 0].ToString();
                        for (int j = 1; j < column.Length; j++)
                        {
                            HMutation mutation = new HMutation();
                            mutation.ColumnFamily = j < 3 ? "info" : j < 5 ? "contact" : "others";
                            mutation.ColumnName   = column[j];
                            mutation.Value        = cells[i, j].ToString();
                            mutations.Add(mutation);
                        }
                        rowCollection[rowKey] = mutations;
                    }
                    HBaseOperation.InsertRows(tableName, rowCollection, con);
                    #endregion Inserting Values

                    #region scan values

                    HBaseOperation.FetchSize = 100;
                    HBaseResultSet table = HBaseOperation.ScanTable(tableName, con);

                    //Adding headertext for the table
                    doctable.AddRow(true, false);
                    //Creating new cell
                    WTableCell cell = new WTableCell(document);
                    cell.AddParagraph().AppendText("ContactId").ApplyCharacterFormat(charFormat);
                    cell.Width = 100;
                    //Adding cell to the row
                    doctable.Rows[0].Cells.Add(cell);
                    cell = new WTableCell(document);
                    cell.AddParagraph().AppendText("contact:EmailId").ApplyCharacterFormat(charFormat);
                    cell.Width = 200;
                    doctable.Rows[0].Cells.Add(cell);
                    cell = new WTableCell(document);
                    cell.AddParagraph().AppendText("contact:PhoneNo").ApplyCharacterFormat(charFormat);
                    cell.Width = 150;
                    doctable.Rows[0].Cells.Add(cell);
                    cell = new WTableCell(document);
                    cell.AddParagraph().AppendText("info:Age").ApplyCharacterFormat(charFormat);
                    cell.Width = 100;
                    doctable.Rows[0].Cells.Add(cell);
                    cell = new WTableCell(document);
                    cell.AddParagraph().AppendText("info:FullName").ApplyCharacterFormat(charFormat);
                    cell.Width = 150;
                    doctable.Rows[0].Cells.Add(cell);
                    cell = new WTableCell(document);
                    cell.AddParagraph().AppendText("others:ModifiedDate").ApplyCharacterFormat(charFormat);
                    cell.Width = 200;
                    doctable.Rows[0].Cells.Add(cell);


                    //Reading each row from the fetched result
                    for (int i = 0; i < table.Count(); i++)
                    {
                        HBaseRecord records = table[i];
                        doctable.AddRow(true, false);

                        //Reading each data from the row
                        for (int j = 0; j < records.Count; j++)
                        {
                            Object fields = records[j];

                            //Adding new cell to the document
                            cell = new WTableCell(document);

                            //Adding each data to the cell
                            cell.AddParagraph().AppendText(fields.ToString());
                            if (j != 1 && j != 2 && j != 4 && j != 5)
                            {
                                cell.Width = 100;
                            }
                            else if (j == 2 || j == 4)
                            {
                                cell.Width = 150;
                            }
                            else
                            {
                                cell.Width = 200;
                            }

                            //Adding cell to the table
                            doctable.Rows[i + 1].Cells.Add(cell);
                            doctable.Rows[0].Cells[j].CellFormat.BackColor = Color.FromArgb(51, 153, 51);
                        }
                    }
                    //Adding table to the section
                    section.Tables.Add(doctable);
                    //Save as word 2007 format
                    if (rBtnWord2003.Checked == true)
                    {
                        document.Save("Sample.doc", FormatType.Doc, Response, HttpContentDisposition.Attachment);
                    }
                    else if (rBtnWord2007.Checked == true)
                    {
                        document.Save("Sample.docx", FormatType.Word2007, Response, HttpContentDisposition.Attachment);
                    }
                    //Save as word 2010 format
                    else if (rbtnWord2010.Checked == true)
                    {
                        document.Save("Sample.docx", FormatType.Word2010, Response, HttpContentDisposition.Attachment);
                    }
                    //Save as word 2013 format
                    else if (rbtnWord2013.Checked == true)
                    {
                        document.Save("Sample.docx", FormatType.Word2013, Response, HttpContentDisposition.Attachment);
                    }
                    #endregion scan values

                    #region close connection
                    //Closing the hive connection
                    con.Close();
                    #endregion close connection
                }
Exemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorMessage.InnerText = "";
            string path = string.Format("{0}\\..\\Data\\AdventureWorks\\AdventureWorks_Person_Contact.csv", Request.PhysicalPath.ToLower().Split(new string[] { "\\c# hbase samples" }, StringSplitOptions.None));

            try
            {
                #region creating connection

                HBaseConnection con = new HBaseConnection("localhost", 10003);
                con.Open();

                #endregion creating connection

                #region parsing csv input file

                csv csvObj = new csv();
                object[,] cells;
                cells = null;

                cells = csvObj.Table(path, false, ',');

                #endregion parsing csv input file

                #region creating table
                String        tableName      = "AdventureWorks_Person_Contact";
                List <string> columnFamilies = new List <string>();
                columnFamilies.Add("info");
                columnFamilies.Add("contact");
                columnFamilies.Add("others");
                if (!HBaseOperation.IsTableExists(tableName, con))
                {
                    if (columnFamilies.Count > 0)
                    {
                        HBaseOperation.CreateTable(tableName, columnFamilies, con);
                    }
                    else
                    {
                        throw new HBaseException("ERROR: Table must have at least one column family");
                    }
                }
                # endregion

                #region Inserting Values
                string[] column = new string[] { "CONTACTID", "FULLNAME", "AGE", "EMAILID", "PHONE", "MODIFIEDDATE" };
                Dictionary <string, IList <HMutation> > rowCollection = new Dictionary <string, IList <HMutation> >();
                string rowKey;
                for (int i = 0; i < cells.GetLength(0); i++)
                {
                    List <HMutation> mutations = new List <HMutation>();
                    rowKey = cells[i, 0].ToString();
                    for (int j = 1; j < column.Length; j++)
                    {
                        HMutation mutation = new HMutation();
                        mutation.ColumnFamily = j < 3 ? "info" : j < 5 ? "contact" : "others";
                        mutation.ColumnName   = column[j];
                        mutation.Value        = cells[i, j].ToString();
                        mutations.Add(mutation);
                    }
                    rowCollection[rowKey] = mutations;
                }
                HBaseOperation.InsertRows(tableName, rowCollection, con);
                #endregion Inserting Values

                #region scan values

                HBaseOperation.FetchSize = 100;
                HBaseResultSet table = HBaseOperation.ScanTable(tableName, con);


                StringWriter stringWriter = new StringWriter();
                //Htmlwriter for creating table to append the HiveResults
                using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Border, "1");
                    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
                    writer.RenderBeginTag(HtmlTextWriterTag.Table);

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                    writer.RenderBeginTag(HtmlTextWriterTag.Th);
                    writer.Write("ContactId");
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Th);
                    writer.Write("contact:EmailId");
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Th);
                    writer.Write("contact:PhoneNo");
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Th);
                    writer.Write("info:Age");
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Th);
                    writer.Write("info:FullName");
                    writer.RenderEndTag();

                    writer.RenderBeginTag(HtmlTextWriterTag.Th);
                    writer.Write("others:ModifiedDate");
                    writer.RenderEndTag();

                    writer.RenderEndTag();
                    int count = table.Count();
                    for (int i = 0; i < count; i++)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                        HBaseRecord records = table[i];

                        for (int j = 0; j < records.Count; j++)
                        {
                            Object fields = records[j];
                            writer.RenderBeginTag(HtmlTextWriterTag.Td);
                            writer.Write(fields);
                            writer.RenderEndTag();
                        }
                        writer.RenderEndTag();
                    }
                    writer.RenderEndTag();
                }
                //Binding the result to the RTE control
                string results = stringWriter.ToString();
                rteControl.RTEContent.InnerHtml = results;
                #endregion scan values

                #region close connection
                //Closing the hive connection
                con.Close();
                #endregion close connection
            }