Scala CSV file to Save data into HBase

+1 vote
I want to Create Schema in Hbase. Develop code to connect to Hbase, Read Data and Write Data. How to do this?
Jul 25, 2019 in Apache Spark by Lohit
1,263 views

1 answer to this question.

0 votes

Check the reference code mentioned below:

def main(args: Array[String]): Unit = {


    val spark = SparkSession.builder().appName("sparkToHive").enableHiveSupport().getOrCreate()

    import spark.implicits._


    val config = HBaseConfiguration.create()

    config.set("hbase.zookeeper.quorum", "ip's")

    config.set("hbase.zookeeper.property.clientPort","2181")

    config.set(TableInputFormat.INPUT_TABLE, "tableName")


    val newAPIJobConfiguration1 = Job.getInstance(config)

    newAPIJobConfiguration1.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, "tableName")

    newAPIJobConfiguration1.setOutputFormatClass(classOf[TableOutputFormat[ImmutableBytesWritable]])


    val df: DataFrame = Seq(("foo", "1", "foo1"), ("bar", "2", "bar1")).toDF("key", "value1", "value2")


    val hbasePuts= df.rdd.map((row: Row) => {

      val put = new Put(Bytes.toBytes(row.getString(0)))

      put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("value1"), Bytes.toBytes(row.getString(1)))

      put.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("value2"), Bytes.toBytes(row.getString(2)))

      (new ImmutableBytesWritable(), put)

    })


    hbasePuts.saveAsNewAPIHadoopDataset(newAPIJobConfiguration1.getConfiguration())

    }

Hope this helps!

If you want to know more about Apache Spark Scala, It's highly recommended to go for Spark certification course today.

Thanks!!

answered Jul 25, 2019 by Hari

Related Questions In Apache Spark

0 votes
1 answer

How to load data of .csv file in MySQL Database Table?

You can do it using a code ...READ MORE

answered Jul 22, 2019 in Apache Spark by Vishwa
1,656 views
+1 vote
1 answer

Scala: Convert text file data into ORC format using data frame

Converting text file to Orc: Using Spark, the ...READ MORE

answered Aug 1, 2019 in Apache Spark by Esha
3,308 views
0 votes
1 answer

where can i get spark-terasort.jar and not .scala file, to do spark terasort in windows.

Hi! I found 2 links on github where ...READ MORE

answered Feb 13, 2019 in Apache Spark by Omkar
• 69,210 points
1,125 views
0 votes
1 answer

Load CSV file data inot MySQL

If you are trying to load file ...READ MORE

answered Jul 25, 2019 in Apache Spark by Sita
819 views
+1 vote
2 answers
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
10,555 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
2,184 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
104,181 views
0 votes
1 answer

How to create RDD from an external file source in scala?

Hi, To create an RDD from external file ...READ MORE

answered Jul 4, 2019 in Apache Spark by Gitika
• 65,910 points
1,518 views
0 votes
1 answer

How to fit multiple assignment statements into one statement in Scala?

Hi, You can assign a Tuple to a ...READ MORE

answered Jul 29, 2019 in Apache Spark by Gitika
• 65,910 points
648 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP