Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The third of big data's Learning Series-detailed explanation of HBase Java Api Picture and text

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Introduction

In the previous article, the second of big data's learning series-HBase environment building (stand-alone), successfully built the Hadoop+HBase environment, this article is mainly about some operations on HBase using Java.

First, prepare in advance. Confirm that hadoop and hbase start successfully

two。 Confirm that the firewall closes the dependency shelf package org.apache.hadoop hadoop-common 2.8.2 org.apache.hadoop hadoop-client 2.8.2 org.apache.hadoop hadoop-hdfs required by 3.maven 2.8.2 org.apache.hadoop hadoop-mapreduce-client-core 2.8.2 org.apache.hadoop hadoop-yarn-common 2.8.2 org.apache.hbase hbase-hadoop-compat 1.3. 1 org.apache.hbase hbase-server 1.1.2 org.apache.hbase hbase-client 1.1.2 org.apache.hbase hbase-common 1.1.2 4. Modify hosts file (optional)

Modify the hosts file in the Windows C:\ Windows\ System32\ drivers\ etc directory, and add the host ip of hbase and the host name for relational mapping.

192.168.238.128 master

Note: if you don't use mapping, just change the hostname in the code to IP.

The principle of 5.HBase

This article introduces in great detail:

Http://blog.csdn.net/woshiwanxin102213/article/details/17584043

Second, test example 1. Create a tabl

Create two tables t_student, t_student_info, and add two column families

After the creation is successful, you can see it in the hbase shell and 16010 interfaces.

two。 Add data

After the tables are successfully created, insert data into both tables.

Because HBase is a dynamic database, columns can be added.

The addition and modification of HBase is a method, the data is the same, the later data will overwrite the previous one!

3. Query data

Query according to table name, row key, column family, and column

4. Delete data

Delete one of the pieces of data

Code sample tool class import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableName Import org.apache.hadoop.hbase.client.Admin;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.client.Table Import org.apache.hadoop.hbase.util.Bytes;import com.alibaba.fastjson.JSONObject;/** Title: HBaseUtil * Description: HBase tools * Version:1.0.0 * @ author pancm * @ date December 6, 2017 * / public class HBaseUtil {/ * hadoop connection * / private static Configuration conf = null; / * * hbase connection * / private static Connection con = null; / * * session * / private static Admin admin = null Private static String ip = "master"; private static String port = "2181"; private static String port1 = "9001"; / / initialize the connection static {/ / get the configuration file object conf = HBaseConfiguration.create (); / / set the configuration parameters conf.set ("hbase.zookeeper.quorum", ip); conf.set ("hbase.zookeeper.property.clientPort", port) / / if hbase is a cluster, this must be added / / the ip and port are conf.set configured in the hadoop/mapred-site.xml configuration file ("hbase.master", ip+ ":" + port1) } / * get the connection * * @ return * / public synchronized static Connection getConnection () {try {if (null = = con | | con.isClosed ()) {/ / get the connection object con = ConnectionFactory.createConnection (conf) }} catch (IOException e) {System.out.println ("failed to get connection!"); e.printStackTrace ();} return con } / * connection closed * / public static void close () {try {if (admin! = null) {admin.close ();} if (con! = null) {con.close () }} catch (IOException e) {System.out.println ("connection closed failed!") ; e.printStackTrace ();}} / * * create table * * @ param tableName * Table name * @ param columnFamily * column family * / public static void creatTable (String tableName, String [] columnFamily) {if (null==tableName | | tableName.length () = = 0) {return } if (null==columnFamily | | columnFamily.length==0) {return;} / / create the table name object TableName tn = TableName.valueOf (tableName); / / a. Determine whether there is a try {/ / get session admin = getConnection (). GetAdmin (); if (admin.tableExists (tn)) {System.out.println (tableName + "table exists, delete table."); / / first set the table to non-editable admin.disableTable (tn) / / delete table admin.deleteTable (tn); System.out.println ("table deleted successfully.");} / / create table structure object HTableDescriptor htd = new HTableDescriptor (tn) For (String str: columnFamily) {/ / create column family structure object HColumnDescriptor hcd = new HColumnDescriptor (str); htd.addFamily (hcd);} / / create table admin.createTable (htd); System.out.println (tableName + "table created successfully!") ;} catch (IOException e) {e.printStackTrace ();} finally {close () }} / * insert or update * * @ param tableName * Table name * @ param rowKey * Row key (primary key) * @ param family * column Family * @ param qualifier * column * @ param value * stored value * @ return * / public static void insert (String tableName String rowKey, String family, String qualifier, String value) {Table t = null Try {t = getConnection (). GetTable (TableName.valueOf (tableName)); Put put = new Put (Bytes.toBytes (rowKey)); put.addColumn (Bytes.toBytes (family), Bytes.toBytes (qualifier), Bytes.toBytes (value)); t.put (put); System.out.println (tableName + "updated successfully!") } catch (IOException e) {System.out.println (tableName + "update failed!"); e.printStackTrace ();} finally {close () Data batch insert or update * * @ param tableName * Table name * @ param list * hbase * @ return * / public static void insertBatch (String tableName, List list) {if (null = = tableName | | tableName.length () = = 0) {return } if (null = = list | | list.size () = 0) {return;} Table t = null; Put put = null; JSONObject json = null; List puts = new ArrayList (); try {t = getConnection (). GetTable (TableName.valueOf (tableName)); for (int I = 0, j = list.size (); I

< j; i++) { json = (JSONObject) list.get(i); put = new Put(Bytes.toBytes(json.getString("rowKey"))); put.addColumn(Bytes.toBytes(json.getString("family")), Bytes.toBytes(json.getString("qualifier")), Bytes.toBytes(json.getString("value"))); puts.add(put); } t.put(puts); System.out.println(tableName + " 更新成功!"); } catch (IOException e) { System.out.println(tableName + " 更新失败!"); e.printStackTrace(); } finally { close(); } } /** * 数据删除 * @param tableName 表名 * @param rowKey 行健 * @return */ public static void delete(String tableName, String rowKey) { delete(tableName,rowKey,"",""); } /** * 数据删除 * @param tableName 表名 * @param rowKey 行健 * @param family 列族 * @return */ public static void delete(String tableName, String rowKey, String family) { delete(tableName,rowKey,family,""); } /** * 数据删除 * @param tableName 表名 * @param rowKey 行健 * @param family 列族 * @param qualifier 列 * @return */ public static void delete(String tableName, String rowKey, String family, String qualifier) { if (null == tableName ||tableName.length()==0) { return; } if( null == rowKey || rowKey.length() == 0){ return; } Table t = null; try { t = getConnection().getTable(TableName.valueOf(tableName)); Delete del = new Delete(Bytes.toBytes(rowKey)); // 如果列族不为空 if (null != family && family.length() >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report