Yang, Tong 3f5759eed2 MRS comp-lts 2.0.38.SP20 version
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
Co-authored-by: Yang, Tong <yangtong2@huawei.com>
Co-committed-by: Yang, Tong <yangtong2@huawei.com>
2023-01-19 17:08:45 +00:00

94 lines
15 KiB
HTML

<a name="mrs_01_1406"></a><a name="mrs_01_1406"></a>
<h1 class="topictitle1">CarbonData Quick Start</h1>
<div id="body8662426"><p id="mrs_01_1406__en-us_topic_0000001219029123_ae3ba0b2375534d618d3040836c6f88a8">This section describes how to create CarbonData tables, load data, and query data. This quick start provides operations based on the Spark Beeline client. If you want to use Spark shell, wrap the queries with <strong id="mrs_01_1406__en-us_topic_0000001219029123_b289155745314">spark.sql()</strong>.</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="mrs_01_1406__en-us_topic_0000001219029123_tc9e1ff32aac6410798846181cf7fb057" frame="border" border="1" rules="all"><caption><b>Table 1 </b>CarbonData Quick Start</caption><thead align="left"><tr id="mrs_01_1406__en-us_topic_0000001219029123_row9477582353"><th align="left" class="cellrowborder" valign="top" width="36.67%" id="mcps1.3.2.2.3.1.1"><p id="mrs_01_1406__en-us_topic_0000001219029123_p347816893518"><strong id="mrs_01_1406__en-us_topic_0000001219029123_b69916012493">Operation</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="63.33%" id="mcps1.3.2.2.3.1.2"><p id="mrs_01_1406__en-us_topic_0000001219029123_p12478984357"><strong id="mrs_01_1406__en-us_topic_0000001219029123_b16161912497">Description</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="mrs_01_1406__en-us_topic_0000001219029123_rca77e7c8a8ff4d369172d0072e9e7234"><td class="cellrowborder" valign="top" width="36.67%" headers="mcps1.3.2.2.3.1.1 "><p id="mrs_01_1406__en-us_topic_0000001219029123_a1f51fbebc1f14c49a0f43c0f67b9fd3b"><a href="#mrs_01_1406__en-us_topic_0000001219029123_s2af9b9318a0f44c48f3b0fa8217a12fe">Connecting to CarbonData</a></p>
</td>
<td class="cellrowborder" valign="top" width="63.33%" headers="mcps1.3.2.2.3.1.2 "><p id="mrs_01_1406__en-us_topic_0000001219029123_a252b8024092d429c876204b00744711f">Connect to CarbonData before performing any operations on CarbonData.</p>
</td>
</tr>
<tr id="mrs_01_1406__en-us_topic_0000001219029123_r8996ea6ada284683b0ce67ee3fb24d40"><td class="cellrowborder" valign="top" width="36.67%" headers="mcps1.3.2.2.3.1.1 "><p id="mrs_01_1406__en-us_topic_0000001219029123_a24a914c6ee09442598968b4a49d4a912"><a href="#mrs_01_1406__en-us_topic_0000001219029123_sffd808b54dab44fc8613a01cc8e39baf">Creating a CarbonData Table</a></p>
</td>
<td class="cellrowborder" valign="top" width="63.33%" headers="mcps1.3.2.2.3.1.2 "><p id="mrs_01_1406__en-us_topic_0000001219029123_a43dd7a9f9a054d93a5f481ebcd58611b">Create a CarbonData table to load data and perform query operations.</p>
</td>
</tr>
<tr id="mrs_01_1406__en-us_topic_0000001219029123_r4f97d98ae5bd49698b6d5d3dfa4e1de2"><td class="cellrowborder" valign="top" width="36.67%" headers="mcps1.3.2.2.3.1.1 "><p id="mrs_01_1406__en-us_topic_0000001219029123_acda32a2fac9b4aaaaa815adc739c4413"><a href="#mrs_01_1406__en-us_topic_0000001219029123_s79b594787a2a46819cf07478d4a0d81c">Loading Data to a CarbonData Table</a></p>
</td>
<td class="cellrowborder" valign="top" width="63.33%" headers="mcps1.3.2.2.3.1.2 "><p id="mrs_01_1406__en-us_topic_0000001219029123_a61096dff64ef4425b6b371ee89c17436">Load data from CSV to the created table.</p>
</td>
</tr>
<tr id="mrs_01_1406__en-us_topic_0000001219029123_reff67320c9a84bac8534a458e514346f"><td class="cellrowborder" valign="top" width="36.67%" headers="mcps1.3.2.2.3.1.1 "><p id="mrs_01_1406__en-us_topic_0000001219029123_a28fb1826868041b88297ca2bb0694dab"><a href="#mrs_01_1406__en-us_topic_0000001219029123_s68e9413d1b234b2d91557a1739fc7828">Querying Data from a CarbonData Table</a></p>
</td>
<td class="cellrowborder" valign="top" width="63.33%" headers="mcps1.3.2.2.3.1.2 "><p id="mrs_01_1406__en-us_topic_0000001219029123_aa9f561a86e874564b7648658a8355c3e">Perform query operations such as filters and groupby.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="mrs_01_1406__en-us_topic_0000001219029123_s2af9b9318a0f44c48f3b0fa8217a12fe"><a name="mrs_01_1406__en-us_topic_0000001219029123_s2af9b9318a0f44c48f3b0fa8217a12fe"></a><a name="en-us_topic_0000001219029123_s2af9b9318a0f44c48f3b0fa8217a12fe"></a><h4 class="sectiontitle">Connecting to CarbonData</h4><ul id="mrs_01_1406__en-us_topic_0000001219029123_u6ab77193f8f64f989853e2baf78058cb"><li id="mrs_01_1406__en-us_topic_0000001219029123_le269de8821ba405fa0c4bcb404486367">Use Spark SQL or Spark shell to connect to Spark and run Spark SQL commands.</li><li id="mrs_01_1406__en-us_topic_0000001219029123_l71ad3c644a0a44b09dc4cd2cc31acc19">Start the JDBCServer and use a JDBC client (for example, Spark Beeline) to connect to the JDBCServer.</li></ul>
</div>
<div class="note" id="mrs_01_1406__en-us_topic_0000001219029123_n325e32829ce64ae5885cfb42ab7f330e"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="mrs_01_1406__en-us_topic_0000001219029123_a052ece1bc795454a85d090a184e351b5">A user must belong to the data loading group for performing data loading operations. The default name of the data loading group is <span class="parmname" id="mrs_01_1406__en-us_topic_0000001219029123_pa1dd9a5f432841e39800a094dbd4a4f5"><b>ficommon</b></span>.</p>
</div></div>
<div class="section" id="mrs_01_1406__en-us_topic_0000001219029123_sffd808b54dab44fc8613a01cc8e39baf"><a name="mrs_01_1406__en-us_topic_0000001219029123_sffd808b54dab44fc8613a01cc8e39baf"></a><a name="en-us_topic_0000001219029123_sffd808b54dab44fc8613a01cc8e39baf"></a><h4 class="sectiontitle">Creating a CarbonData Table</h4><p id="mrs_01_1406__en-us_topic_0000001219029123_a132bc4ce9c8a4976a361e2a1ed30fbae">After connecting Spark Beeline with the JDBCServer, create a CarbonData table to load data and perform query operations. Run the following commands to create a simple table:</p>
<pre class="screen" id="mrs_01_1406__en-us_topic_0000001219029123_screen10116529191010">create table x1 (imei string, deviceInformationId int, mac string, productdate timestamp, updatetime timestamp, gamePointId double, contractNumber double) STORED AS carbondata TBLPROPERTIES ('SORT_COLUMNS'='imei,mac');</pre>
<p id="mrs_01_1406__en-us_topic_0000001219029123_adf903c5382e1440cb85d6abd32e9af67">The command output is as follows:</p>
<pre class="screen" id="mrs_01_1406__en-us_topic_0000001219029123_s0bde6cee8a13468b815e9b05096aadf2">+---------+--+
| result |
+---------+--+
+---------+--+
No rows selected (1.551 seconds)</pre>
</div>
<div class="section" id="mrs_01_1406__en-us_topic_0000001219029123_s79b594787a2a46819cf07478d4a0d81c"><a name="mrs_01_1406__en-us_topic_0000001219029123_s79b594787a2a46819cf07478d4a0d81c"></a><a name="en-us_topic_0000001219029123_s79b594787a2a46819cf07478d4a0d81c"></a><h4 class="sectiontitle">Loading Data to a CarbonData Table</h4><p id="mrs_01_1406__en-us_topic_0000001219029123_a6fc2a8f1d9c94f27bb97de59ccc4d822">After you have created a CarbonData table, you can load the data from CSV to the created table.</p>
<p id="mrs_01_1406__en-us_topic_0000001219029123_a8583524ef81f42cf95243fd0b7334618"><strong id="mrs_01_1406__en-us_topic_0000001219029123_b156313416519">Loading data from a CSV file to a CarbonData table</strong></p>
<p id="mrs_01_1406__en-us_topic_0000001219029123_a59f53a20e20b488e83ae5dd3926f704a">Run the following command with required parameters to load data from CSV. The column names of the CarbonData table must match the column names of the CSV file.</p>
<p id="mrs_01_1406__en-us_topic_0000001219029123_af01eb112638d4a25b3aca51b14b07995"><strong id="mrs_01_1406__en-us_topic_0000001219029123_b1485119505372">LOAD DATA inpath 'hdfs://hacluster/data/</strong><em id="mrs_01_1406__en-us_topic_0000001219029123_i75582513370">x1_without_header.csv</em><strong id="mrs_01_1406__en-us_topic_0000001219029123_b1018144183814">' into table </strong><em id="mrs_01_1406__en-us_topic_0000001219029123_i5922443386">x1</em><strong id="mrs_01_1406__en-us_topic_0000001219029123_b51817423817"> options('DELIMITER'=',', 'QUOTECHAR'='"','FILEHEADER'='imei, deviceinformationid,mac, productdate,updatetime, gamepointid,contractnumber');</strong></p>
<p id="mrs_01_1406__en-us_topic_0000001219029123_ac6c375c07b6c4fab83a1a81e64a6d106">In the preceding command, <span class="filepath" id="mrs_01_1406__en-us_topic_0000001219029123_f0f2197f7f5584322a4d7162988558437"><b>x1_without_header.csv</b></span> and <span class="filepath" id="mrs_01_1406__en-us_topic_0000001219029123_f84feff9a8a904f00b039794afb492369"><b>x1</b></span> are used as examples.</p>
<p id="mrs_01_1406__en-us_topic_0000001219029123_a574ab7d6706d46aca3d50ef661dee431">The CSV example file is as follows:</p>
<pre class="screen" id="mrs_01_1406__en-us_topic_0000001219029123_s3569e87c602c4d24bee8182682c18dd4">13418592122,1001,<em id="mrs_01_1406__en-us_topic_0000001219029123_i2238143711528"> MAC address</em>, 2017-10-23 15:32:30,2017-10-24 15:32:30,62.50,74.56
13418592123 1002, <em id="mrs_01_1406__en-us_topic_0000001219029123_i5329943135212"> MAC address</em>, 2017-10-23 16:32:30,2017-10-24 16:32:30,17.80,76.28
13418592124,1003,<em id="mrs_01_1406__en-us_topic_0000001219029123_i10560049165210"> MAC address</em>, 2017-10-23 17:32:30,2017-10-24 17:32:30,20.40,92.94
13418592125 1004, <em id="mrs_01_1406__en-us_topic_0000001219029123_i165578586521"> MAC address</em>, 2017-10-23 18:32:30,2017-10-24 18:32:30,73.84,8.58
13418592126,1005,<em id="mrs_01_1406__en-us_topic_0000001219029123_i1461010419532"> MAC address</em>, 2017-10-23 19:32:30,2017-10-24 19:32:30,80.50,88.02
13418592127 1006, <em id="mrs_01_1406__en-us_topic_0000001219029123_i8176990538"> MAC address</em>, 2017-10-23 20:32:30,2017-10-24 20:32:30,65.77,71.24
13418592128,1007,<em id="mrs_01_1406__en-us_topic_0000001219029123_i229301511533"> MAC address</em>, 2017-10-23 21:32:30,2017-10-24 21:32:30,75.21,76.04
13418592129,1008,<em id="mrs_01_1406__en-us_topic_0000001219029123_i128916200535"> MAC address</em>, 2017-10-23 22:32:30,2017-10-24 22:32:30,63.30,94.40
13418592130, 1009,<em id="mrs_01_1406__en-us_topic_0000001219029123_i659826145311"> MAC address</em>, 2017-10-23 23:32:30,2017-10-24 23:32:30,95.51,50.17
13418592131,1010,<em id="mrs_01_1406__en-us_topic_0000001219029123_i144547353535"> MAC address</em>, 2017-10-24 00:32:30,2017-10-25 00:32:30,39.62,99.13</pre>
<p id="mrs_01_1406__en-us_topic_0000001219029123_add5d2c8a3ea443da9dc289195d5a7bd1">The command output is as follows:</p>
<pre class="screen" id="mrs_01_1406__en-us_topic_0000001219029123_s238251f4d7e04e05be734d01d51d35f6">+---------+--+
| Result |
+---------+--+
+---------+--+
No rows selected (3.039 seconds)</pre>
</div>
<div class="section" id="mrs_01_1406__en-us_topic_0000001219029123_s68e9413d1b234b2d91557a1739fc7828"><a name="mrs_01_1406__en-us_topic_0000001219029123_s68e9413d1b234b2d91557a1739fc7828"></a><a name="en-us_topic_0000001219029123_s68e9413d1b234b2d91557a1739fc7828"></a><h4 class="sectiontitle">Querying Data from a CarbonData Table</h4><p id="mrs_01_1406__en-us_topic_0000001219029123_a424f8ab3e4bb46098ab6092f4cc066f8">After a CarbonData table is created and the data is loaded, you can perform query operations as required. Some query operations are provided as examples.</p>
<ul id="mrs_01_1406__en-us_topic_0000001219029123_u35a27d6f28644b0fbe301eae9f99aa4e"><li id="mrs_01_1406__en-us_topic_0000001219029123_l7f4c4fcb65674ed9923fce7f1497b97e"><strong id="mrs_01_1406__en-us_topic_0000001219029123_b1216027195415">Obtaining the number of records</strong><p id="mrs_01_1406__en-us_topic_0000001219029123_abda24e0f5e634590a77b909201390de1">Run the following command to obtain the number of records in the CarbonData table:</p>
<p id="mrs_01_1406__en-us_topic_0000001219029123_a2bdaf1ec9afd4ff8bb80a2d132f1de11"><b><span class="cmdname" id="mrs_01_1406__en-us_topic_0000001219029123_cmdname17438204511388">select count(*) from x1;</span></b></p>
</li><li id="mrs_01_1406__en-us_topic_0000001219029123_l233e7fe5540a4acab1a0f4182a2d2aa5"><strong id="mrs_01_1406__en-us_topic_0000001219029123_b1965443114548">Querying with the groupby condition</strong><p id="mrs_01_1406__en-us_topic_0000001219029123_a1f4a3640520c4acfb44b489c0abffac5">Run the following command to obtain the <strong id="mrs_01_1406__en-us_topic_0000001219029123_b1573158175420">deviceinformationid</strong> records without repetition in the CarbonData table:</p>
<p id="mrs_01_1406__en-us_topic_0000001219029123_a88b1b251a7cc43a7a9907c3790bb0b7e"><b><span class="cmdname" id="mrs_01_1406__en-us_topic_0000001219029123_cmdname19302194219380">select deviceinformationid,count (distinct deviceinformationid) from x1 group by deviceinformationid;</span></b></p>
</li><li id="mrs_01_1406__en-us_topic_0000001219029123_l68f35b253090447f84e1ba2c7c8e7fe5"><strong id="mrs_01_1406__en-us_topic_0000001219029123_b0116352557">Querying with Filter</strong><p id="mrs_01_1406__en-us_topic_0000001219029123_a4a76d68008a247a1b3b99eafcc6cc989">Run the following command to obtain specific <strong id="mrs_01_1406__en-us_topic_0000001219029123_b023833145512">deviceinformationid</strong> records:</p>
<p id="mrs_01_1406__en-us_topic_0000001219029123_aa1bcbe4e46584a23b3f9161350cb4380"><b><span class="cmdname" id="mrs_01_1406__en-us_topic_0000001219029123_cmdname7302034165320">select * from x1 where deviceinformationid='1010';</span></b></p>
</li></ul>
<div class="note" id="mrs_01_1406__en-us_topic_0000001219029123_n17bebcb97003447ea9a95ecf1cf1fd1f"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="mrs_01_1406__en-us_topic_0000001219029123_af1105a524b0b4a83a227063b91075966">If the query result has other non-English characters, the columns in the query result may not be aligned. This is because characters of different languages occupy different widths.</p>
</div></div>
</div>
<div class="section" id="mrs_01_1406__en-us_topic_0000001219029123_s1ab71eae69124272b091d392642421fc"><h4 class="sectiontitle">Using CarbonData on Spark-shell</h4><p id="mrs_01_1406__en-us_topic_0000001219029123_ae5cad4ad44ca4debb65181e12a57ba11">If you need to use CarbonData on a Spark-shell, you need to create a CarbonData table, load data to the CarbonData table, and query data in CarbonData as follows:</p>
<pre class="screen" id="mrs_01_1406__en-us_topic_0000001219029123_saaf0f2c736d54c8ab8c7b4376a24e5aa">spark.sql("CREATE TABLE x2(imei string, deviceInformationId int, mac string, productdate timestamp, updatetime timestamp, gamePointId double, contractNumber double) STORED AS carbondata")
spark.sql("LOAD DATA inpath 'hdfs://hacluster/data/x1_without_header.csv' into table x2 options('DELIMITER'=',', 'QUOTECHAR'='\"','FILEHEADER'='imei, deviceinformationid,mac, productdate,updatetime, gamepointid,contractnumber')")
spark.sql("SELECT * FROM x2").show()</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_01_1405.html">CarbonData Operation Guide</a></div>
</div>
</div>