Skip to content

Instantly share code, notes, and snippets.

@saisgit
Last active April 27, 2020 10:04
Show Gist options
  • Save saisgit/b8af0c8060ab7120dcc2393a3377f85a to your computer and use it in GitHub Desktop.
Save saisgit/b8af0c8060ab7120dcc2393a3377f85a to your computer and use it in GitHub Desktop.
import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}
import org.apache.hadoop.client.{ConnectionFactory, Put}
import org.apache.hadoop.hbase.util.Bytes
import java.time.format.DateTimeFormatter
/**
Function to Write data to Hbase using Scala
Parameters: hbase-site.xml along with path(hbaseConfPath), HBase Table name, RowKey value, Column family name, Data to be stored
Returns: Nothing(unit)
**/
def writeToHbase(hbaseConfPath:String, hbaseTable:String, rowKey:String, columnFamily:String, data:String):Unit={
val hbaseconf = HBaseConfiguration.create()
hbaseconf.addResource(new File(hbaseConfPath).toURI.toURL)
val conn = ConnectionFactory.createConnection(hbaseconf)
val table = conn.getTable(TableName.valueOf(hbaseTable))
val put = new Put(rowKey.getBytes)
put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes("inputData"),Bytes.toBytes(data))
val indate = java.time.LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYYMMddHH"))
put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes("DateTime"),Bytes.toBytes(indate))
table.put(put)
conn.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment