forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: Su, Xiaomeng <suxiaomeng1@huawei.com> Co-committed-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
140 lines
15 KiB
HTML
140 lines
15 KiB
HTML
<a name="dli_08_0431"></a><a name="dli_08_0431"></a>
|
|
|
|
<h1 class="topictitle1">Type Conversion Functions</h1>
|
|
<div id="body8662426"><div class="section" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_section9734433122713"><h4 class="sectiontitle">Syntax</h4><pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen19396104742710">CAST(value AS type)</pre>
|
|
</div>
|
|
<div class="section" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_section197791153142712"><h4 class="sectiontitle">Description</h4><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p125922369169">This function is used to forcibly convert types.</p>
|
|
</div>
|
|
<div class="section" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_section162182672816"><h4 class="sectiontitle">Precautions</h4><ul id="dli_08_0431__en-us_topic_0000001262495806_ul209071853175711"><li id="dli_08_0431__en-us_topic_0000001262495806_li7907653175713">If the input is <strong id="dli_08_0431__en-us_topic_0000001262495806_b1694911516341">NULL</strong>, <strong id="dli_08_0431__en-us_topic_0000001262495806_b394991516342">NULL</strong> is returned.</li><li id="dli_08_0431__en-us_topic_0000001262495806_li1337485613576">The <strong id="dli_08_0431__en-us_topic_0000001262495806_b20323205863717">cast</strong> function does not support converting a string to the JSON format.</li></ul>
|
|
</div>
|
|
<div class="section" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_section6708122502812"><h4 class="sectiontitle">Example 1: Convert the amount value to an integer.</h4><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_a3e8ecbf3e3934ed1b04601e1689e0518">The following example converts the <strong id="dli_08_0431__en-us_topic_0000001262495806_b148927249544337">amount</strong> value to an integer.</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_s41c7e3601df241b281c5c3557d53415c">insert into temp select cast(amount as INT) from source_stream;</pre>
|
|
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_table178184319481" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Examples of type conversion functions</caption><thead align="left"><tr id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_row681919314480"><th align="left" class="cellrowborder" valign="top" width="21.42%" id="mcps1.3.4.4.2.4.1.1"><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p1581993117483">Example</p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="40.62%" id="mcps1.3.4.4.2.4.1.2"><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p881943112487">Description</p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="37.96%" id="mcps1.3.4.4.2.4.1.3"><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p479454615014">Example</p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_row38191317486"><td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.3.4.4.2.4.1.1 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p781913194814">cast(v1 as string)</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="40.62%" headers="mcps1.3.4.4.2.4.1.2 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p13819531184820">Converts <strong id="dli_08_0431__en-us_topic_0000001262495806_b173975746244337">v1</strong> to a string. The value of <strong id="dli_08_0431__en-us_topic_0000001262495806_b205118173644337">v1</strong> can be of the numeric type or of the timestamp, date, or time type.</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.4.4.2.4.1.3 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p395613491194">Table T1:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen179216204108">| content (INT) |
|
|
| ------------- |
|
|
| 5 |</pre>
|
|
<p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p79562497911">Statement:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen12431554101019">SELECT
|
|
cast(content as varchar)
|
|
FROM
|
|
T1;</pre>
|
|
<p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p199573491599">Result:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen13419104151112">"5"</pre>
|
|
</td>
|
|
</tr>
|
|
<tr id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_row1917212119502"><td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.3.4.4.2.4.1.1 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p12173151185019">cast (v1 as int)</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="40.62%" headers="mcps1.3.4.4.2.4.1.2 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p4174171105011">Converts <strong id="dli_08_0431__en-us_topic_0000001262495806_b93050572544337">v1</strong> to the <strong id="dli_08_0431__en-us_topic_0000001262495806_b172010696244337">int</strong> type. The value of <strong id="dli_08_0431__en-us_topic_0000001262495806_b176969663844337">v1</strong> can be a number or a character.</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.4.4.2.4.1.3 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p18383418139">Table T1:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen116016486135">| content (STRING) |
|
|
| ------------- |
|
|
| "5" |</pre>
|
|
<p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p6323481312">Statement:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen65451837201411">SELECT
|
|
cast(content as int)
|
|
FROM
|
|
T1;</pre>
|
|
<p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p648345137">Result:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen3448717151">5</pre>
|
|
</td>
|
|
</tr>
|
|
<tr id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_row12267712165017"><td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.3.4.4.2.4.1.1 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p026851219507">cast(v1 as timestamp)</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="40.62%" headers="mcps1.3.4.4.2.4.1.2 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p14268512155012">Converts <strong id="dli_08_0431__en-us_topic_0000001262495806_b166431397444337">v1</strong> to the <strong id="dli_08_0431__en-us_topic_0000001262495806_b163993763044337">timestamp</strong> type. The value of <strong id="dli_08_0431__en-us_topic_0000001262495806_b19357682044337">v1</strong> can be of the <strong id="dli_08_0431__en-us_topic_0000001262495806_b111632707344337">string</strong>, <strong id="dli_08_0431__en-us_topic_0000001262495806_b98260950044337">date</strong>, or <strong id="dli_08_0431__en-us_topic_0000001262495806_b53732629444337">time</strong> type.</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.4.4.2.4.1.3 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p27431739192212">Table T1:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen145085582216">| content (STRING) |
|
|
| ------------- |
|
|
| "2018-01-01 00:00:01" |</pre>
|
|
<p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p117441639182213">Statement:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen83883113426">SELECT
|
|
cast(content as timestamp)
|
|
FROM
|
|
T1;</pre>
|
|
<p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p5744103922217">Result:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen21051924174215">1514736001000</pre>
|
|
</td>
|
|
</tr>
|
|
<tr id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_row77856825316"><td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.3.4.4.2.4.1.1 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p157872805310">cast(v1 as date)</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="40.62%" headers="mcps1.3.4.4.2.4.1.2 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p37879815318">Converts <strong id="dli_08_0431__en-us_topic_0000001262495806_b14219408644337">v1</strong> to the <strong id="dli_08_0431__en-us_topic_0000001262495806_b197018237744337">date</strong> type. The value of <strong id="dli_08_0431__en-us_topic_0000001262495806_b76147469944337">v1</strong> can be of the <strong id="dli_08_0431__en-us_topic_0000001262495806_b18939565744337">string</strong> or <strong id="dli_08_0431__en-us_topic_0000001262495806_b131443161944337">timestamp</strong> type.</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.4.4.2.4.1.3 "><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p1324241431">Table T1:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen86153834318">| content (TIMESTAMP) |
|
|
| ------------- |
|
|
| 1514736001000 |</pre>
|
|
<p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p232454104310">Statement:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen10337163214457">SELECT
|
|
cast(content as date)
|
|
FROM
|
|
T1;</pre>
|
|
<p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p5324144164318">Result:</p>
|
|
<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_screen54891750134716">"2018-01-01"</pre>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="note" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_note035781042910"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_p43681310162918">Flink jobs do not support the conversion of <strong id="dli_08_0431__en-us_topic_0000001262495806_b150066029244337">bigint</strong> to <strong id="dli_08_0431__en-us_topic_0000001262495806_b141111968144337">timestamp</strong> using CAST. You can convert it using <strong id="dli_08_0431__en-us_topic_0000001262495806_b155034347344337">to_timestamp</strong>.</p>
|
|
</div></div>
|
|
</div>
|
|
<div class="section" id="dli_08_0431__en-us_topic_0000001262495806_dli_08_0112_section14655163113329"><h4 class="sectiontitle">Example 2:</h4><ol id="dli_08_0431__en-us_topic_0000001262495806_ol154981922165916"><li id="dli_08_0431__en-us_topic_0000001262495806_li1049862220596">Create a Flink opensource SQL job by referring to <a href="dli_08_0386.html#dli_08_0386">Kafka Source Table</a> and <a href="dli_08_0399.html#dli_08_0399">Print Result Table</a>, enter the following job running script, and submit the job.<div class="p" id="dli_08_0431__en-us_topic_0000001262495806_p068291610113">Note: When creating a job, set Flink Version to 1.12 in the Running Parameters area on the job editing page, select Save Job Log, and set the OBS bucket for saving job logs to facilitate subsequent job log viewing. Change the values of the parameters in bold in the following script according to the actual situation.<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_screen375516121614">CREATE TABLE kafkaSource (
|
|
cast_int_to_string int,
|
|
cast_String_to_int string,
|
|
case_string_to_timestamp string,
|
|
case_timestamp_to_date timestamp
|
|
) WITH (
|
|
'connector' = 'kafka',
|
|
'topic' = '<em id="dli_08_0431__en-us_topic_0000001262495806_i4543155265320"><strong id="dli_08_0431__en-us_topic_0000001262495806_b1754316528535">Kafka</strong></em><em id="dli_08_0431__en-us_topic_0000001262495806_i15341759195319"><strong id="dli_08_0431__en-us_topic_0000001262495806_b5227105913538">Topic</strong></em>',
|
|
'properties.bootstrap.servers' = '<em id="dli_08_0431__en-us_topic_0000001262495806_i9319103215313"><strong id="dli_08_0431__en-us_topic_0000001262495806_b0322115133213">KafkaAddress1:KafkaPort,KafkaAddress2:KafkaPort</strong></em>',
|
|
'properties.group.id' = '<em id="dli_08_0431__en-us_topic_0000001262495806_i14535174011311"><strong id="dli_08_0431__en-us_topic_0000001262495806_b1466610178329">GroupId</strong></em>',
|
|
'scan.startup.mode' = 'latest-offset',
|
|
"format" = "json"
|
|
);
|
|
|
|
CREATE TABLE printSink (
|
|
cast_int_to_string string,
|
|
cast_String_to_int int,
|
|
case_string_to_timestamp timestamp,
|
|
case_timestamp_to_date date
|
|
) WITH (
|
|
'connector' = 'print'
|
|
);
|
|
|
|
insert into printSink select
|
|
cast(cast_int_to_string as string),
|
|
cast(cast_String_to_int as int),
|
|
cast(case_string_to_timestamp as timestamp),
|
|
cast(case_timestamp_to_date as date)
|
|
from kafkaSource;</pre>
|
|
</div>
|
|
</li><li id="dli_08_0431__en-us_topic_0000001262495806_li249822219590">Connect to the Kafka cluster and send the following test data to the Kafka topic:<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_screen7222143315544">{"cast_int_to_string":"1", "cast_String_to_int": "1", "case_string_to_timestamp": "2022-04-02 15:00:00", "case_timestamp_to_date": "2022-04-02 15:00:00"}</pre>
|
|
</li><li id="dli_08_0431__en-us_topic_0000001262495806_li9498172216599">View output.<ul id="dli_08_0431__en-us_topic_0000001262495806_ul133191514102210"><li id="dli_08_0431__en-us_topic_0000001262495806_li11970649381">Method 1:<ol type="a" id="dli_08_0431__en-us_topic_0000001262495806_ol4711275385"><li id="dli_08_0431__en-us_topic_0000001262495806_li5612141010388">Log in to the DLI management console and choose Job Management > Flink Streaming Jobs.</li><li id="dli_08_0431__en-us_topic_0000001262495806_li117110711383">Locate the row that contains the target Flink job, and choose More & > FlinkUI in the Operation column.</li><li id="dli_08_0431__en-us_topic_0000001262495806_li07833554385">On the Flink UI, choose Task Managers, click the task name, and select Stdout to view the job run logs.</li></ol>
|
|
</li><li id="dli_08_0431__en-us_topic_0000001262495806_li341910155285">Method 2: If you select Save Job Log for Running Parameters before submitting the job, perform the following operations:<ol type="a" id="dli_08_0431__en-us_topic_0000001262495806_ol864115198285"><li id="dli_08_0431__en-us_topic_0000001262495806_li10901621122819">Log in to the DLI management console and choose Job Management > Flink Streaming Jobs.</li><li id="dli_08_0431__en-us_topic_0000001262495806_li1912163912282">Click the name of the corresponding Flink job, choose Run Log, click OBS Bucket, and locate the folder of the corresponding log based on the job running date.</li><li id="dli_08_0431__en-us_topic_0000001262495806_li0641191914285">Go to the folder of the corresponding date, find the folder whose name contains taskmanager, download the taskmanager.out file, and view the result log.</li></ol>
|
|
</li></ul>
|
|
<div class="p" id="dli_08_0431__en-us_topic_0000001262495806_p1585111204015">The query result is as follows:<pre class="screen" id="dli_08_0431__en-us_topic_0000001262495806_screen109768515712">+I(1,1,2022-04-02T15:00,2022-04-02)</pre>
|
|
</div>
|
|
</li></ol>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0426.html">Built-In Functions</a></div>
|
|
</div>
|
|
</div>
|
|
|